0
点赞
收藏
分享

微信扫一扫

12 Vim Tips


这个Vim提示的汇编是转发了一些中级到高级Vim提示。 让我们回顾一下

1. 重复上一个Ex命令

在Vim中有3种命令行模式提示:


Ex命令提示符::something


搜索提示:/something


表达式提示(从插入模式类型<C-r> =):= 1 + 1 <cr>


假设我们从Ex命令提示符中运行一个spec:


:!rspec spec/models/code_spec.rb


​@:​

2. Sort properties in CSS

​:sort​

h4 {
font-size: 11px;
margin: 15px;
background: red;
}


3. Select yanked and pasted text

​gv​​ . And, select last pasted text with ​​gb​​ . ​​gb​​ is a custom mapping that we add to vimrc file and I find it ​​extremely​​  useful when doing extract method refactoring, a cut-paste and then we need to fix indentation. With ​​gb​​ we easily select the pasted text and we fix the indentation with  ​​<​​  or ​​>​​.

" select last paste in visual mode
nnoremap <expr> gb '`[' . strpart(getregtype(), 0, 1) . '`]'


4. Paste text while in insert mode

​<C-r>0​​. If yanked text contains new line characters, ​​<C-r><C-p>0​

5. Delete in insert mode

​Insert​​ mode, ​​Vim Command Line​​ mode or ​​Shell Command Line​

<C-h> " delete back one character (backspace)
<C-w> " delete back one word
<C-u> " delete back to start of line
<C-k> " delete forward to end of line


6. Run normal mode commands across a range

​;​

var element = $(this)
var tabName = element.data('tab')
var report = element.data('report')


​:normal A;​​  that will execute ​​A;​​ (append ;) for each line. Alternatively, we can run same on the whole content of the file with  ​​:%normal A;​​.

7. Repeat last change on multiple lines

​;​​ at the end of the line with ​​A;​​. We can repeat that command by selecting the lines 2-3 and running the dotcommand over visual selection with ​​:'<,'> normal .​

8. Replace in multiple files

​:args​

:args app/assets/javascripts/ext*.js


​:args​

:argdo %s/From/To/g


Once changes have been made in the files, we can save all the files in arguments list with:


:argdo update


9. Search and replace in multiple files

​vimgrep​​ command to find a pattern in files. ​​vimgrep​​ command creates quicklist with files matching the pattern which list we can see by opening it with ​​:copen​​ command. If we want quicklist to be useful, we need to convert it to arguments list by using the ​​:Qargs​​mapping which we have in our vimrc file:

command! -nargs=0 -bar Qargs execute 'args' QuickfixFilenames()

" populate the argument list with each of the files named in the quickfix list
function! QuickfixFilenames()
let buffer_numbers = {}
for quickfix_item in getqflist()
let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr'])
endfor
return join(map(values(buffer_numbers), 'fnameescape(v:val)'))
endfunction


Finally, here's an example of what needs to be run to do search & replace in files:


:vimgrep /CurrencyNumberHelper/ app/models/*.rb
:Qargs
:argdo %s/CurrencyNumberHelper/CurrencyHelper/g
:argdo update


Other option (pointed by buztard in the comments) is to use Ack-Grep to create the arguments:


:args `Ack-Grep -l CurrencyNumberHelper`
:argdo %s/CurrencyNumberHelper/CurrencyHelper/g
:argdo update


10. Edit already recorded macro

​a​​ . We can print the content of the macro in the current buffer using ​​:put a​​ and then edit the macro in Vim. Once macro is changed, we can select it and then yank it to register a with ​​"ay​​ . Then we are ready to execute the new macro from register a with ​​@a​

11. Execute macro in multiple files

We have already recorded a macro and we want to run it in few files. First we build arguments list with the files, for example let's load all models from a Rails app:


:args app/models/*.rb


​a​​) with:

:argdo normal @a


In the end we save all the buffers with:


:argdo update


12. Vi mode on command line

​emacs​​ and ​​vi​

set -o vi


​ESC​​ to go to ​​vi​​ editing mode and use vi's single line editing capabilities. While in ​​vi​​ editing mode on the command line, we can press ​​v​

If we need to go back to emacs mode, type:


set -o emacs


Here's a ​​cheatsheet​​ of what Vi commands we can run in bash shell vi mode.

Looking for more Vim tips? Checkout my ​​vimfiles​​ on Github.

举报

相关推荐

VIM Tips

12-vim程序编辑器-

mongodb tips

代码tips

hadoop tips

0 条评论