Vim Tips
Vim Tips
Vim Tips
Searching
Command Description
/joe/e cursor set to end of match
/joe/e+1 cursor set to end of match plus 1
/joe/s-2 cursor set to start of match minus 2
/ˆjoe.*fred.*bill/ normal
/ˆ[A-J]\+/ search for lines beginning with one or more A-J
/begin\ .*end search over possible multiple lines
/fred\ s*joe/i any whitespace including newline
/fred\|joe search for fred or joe
/.*fred\&.*joe search for fred and joe in any order
/\<fred\>/i search for fred but not alfred or frederick
/\<\d\d\d\d\> search for exactly 4 digit numbers
/\D\d\d\d\d\D search for exactly 4 digit numbers
/\<\d\{4}\> same thing
/\([ˆ0-9]\|ˆ\)%.*% search for absence of a digit or beginning of line
/ˆ\n\{3} find 3 empty lines
/\(fred\).*\(joe\).*\2.*\1 using rexexp memory in a search
/ˆ\([ˆ,]*,\)\{8} using rexexp memory in a search
/<\zs[ˆ>]*\ze> search for tag contents, ignoring chevrons (:h /\zs)
/<\@<=[ˆ>]*>\@= search for tag contents, ignoring chevrons
/<\@<=\ [ˆ>]*>\@= search for tags across possible multiple lines
/¡!– p{-}–¿ search for multiple line comments
/fred\ s*joe/i any whitespace including newline (\ )
/bugs\(\ .\)*bunny bugs followed by bunny anywhere in file
:h \ help
:bufdo /searchstr/ multiple file search (use :rewind to recommence search)
:bufdo %s/searchstr/&/gic multiple file search better but cheating (n, then a to stop)
?http://www.vim.org/ search backwards for a URL without backslashing
/\c\v([ˆaeiou]&\a){4} search for 4 consecutive consonants
:nmap gx yiw/\(sub\<bar>function\)\s\+<C-R>”<CR>
Substitution
1
:%s/\r//g delete dos returns ˆM
:%s/\r/\r/g turn dos returns ˆM into real returns (fixes joined lines)
:%s= *$== delete end of line blanks
:%s= \+$== same as above
:%s#\s*\r\?$## clean both trailing spaces AND dos returns
:%s#\s*\r*$## same thing deleting empty lines
:%s/ˆ\n\{3}// delete blocks of 3 empty lines
:%s/ˆ\n\+/\r/ compressing empty lines
:%s#<[ˆ>]\+>##g delete html tags, leave text
:’a,’bg/fred/s/dick/joe/igc VERY USEFUL
:%s= [ˆ ]\+$=&&= duplicate end column
:%s= \f\+$=&&= same as above
:%s= \S\+$=&& usually the same
:s/\(.*\):\(.*\)/\2 : \1/ reverse fields separated by :
:%s/ˆ\(.*\)\n\1$/\1/ delete duplicate lines
:%s/ˆ.\{-}pdf/new.pdf/ delete to 1st pdf only
:%s#\<[zy]\?tbl [a-z ]\+\>#\L&#gc lowercase with optional leading characters
:%s/// delete possibly multi-line comments
:help /\{-} help non-greedy
:s/fred/a/g sub “fred” with contents of register “a”
:s/fred/\=@a/g better alternative as register not displayed
:%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/ multiple commands on one line
:%s/a/but/gie|:update|:next then use @: to repeat
:%s/suck\|buck/loopy/gc ORing (must break pipe)
:s/ date /\=strftime(“%c”)/ insert datestring
:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2: working with columns sub any str1 in col3
:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1: swapping first and last column (4 columns)
:%s/\d\+/\=(submatch(0)-3)/ decrement numbers by 3
:g/loc\|function/s/\d/\=submatch(0)+6/ increment numbers by 6 on certain lines only
:%s#txtdev\zs\d#\=submatch(0)+1#g better version of above
:%s/\(gg\)\@<=\d\+/\=submatch(0)+6/ increment only numbers gg\d\d by 6 (another way)
:let i=10 | ’a,’bg/Abc/s/yy/\=i/ |let i=i+1 convert yy to 10,11,12 etc
:let i=10 | ’a,’bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 convert xxyy to xx11,xx12,xx13 (more presise)
:%s/”\([ˆ.]\+\).*\zsxx/\1/ find replacement text, use \zs to simplify substitute
:nmap z :%s#\<=expand(“”)\># pull word under cursor into LHS of a substitute
:vmap z :%s/\<*\>/ pull visually highlighted text into LHS of a substitute
2
Substituting a visual area
3
Global combined with substitute (power editing)
Changing case
Reformatting text
Essential
4
. repeat last modification
@: repeat last : command (then @@)
<C-N><C-P> word completion in insert mode
<C-X><C-L> line complete SUPER USEFUL
/<C-R><C-W> pull onto search/command line
/<C-R><C-A> pull onto search/command line
:set ignorecase you nearly always want this
:syntax on colour syntax in perl, HTML, PHP etc.
:h regexp list all help topics containing regexp (TAB to step through list)
:nmap ,s :source $VIM/ vimrc read from vimrc
:nmap ,v :e $VIM/ vimrc open and edit local vimrc
:vmap sb ”zdi<b><C-R>z</b><ESC> wrap <b></b> around VISUALLY selected text
:vmap st ”zdi<?= <C-R>z ?><ESC> wrap <?= ?> around VISUALLY selected text
File Manipulation
Exploring
5
Multiple files management
File-name manipulation
:h filename-modifiers help
:w % write to current file name
:w %:r.cfm change file extention to .cfm
:!echo %:p full path & file name
:!echo %:p:h full path only
<C-R>% insert filename (insert mode)
“%p insert filename (normal mode)
/<C-R>% search for file name in text
6
:bufdo %s/foo/bar/e
:windo %s/foo/bar/e
:argdo exe ’%!sort’|w! include an external command
Modelines
7
q/ search history window (puts you in full edit mode)
q: commandline history window (puts you in full edit mode)
: history Window
Registers
List your registers
8
:let @y=’yy@”’ pre-loading registers (put in .vimrc)
qqq empty register “q”
:let @a=@ clear register a
:let @a=”“ clear register a
:let @*=@a copy register a to paste buffer
map <F11> “qyy:let @q=@q.”zzz”
Appending to registers
Yank 5 lines into “a” then add a further 5
1. “a5yy
2. 10j
3. “A5yy
:let @m=”:’a,’bs/”
:let @s=”:%!sort -u”
Useful tricks
9
“ayy@a execute “vim command” in a text file
yy@” same thing using unnamed register
u@. execute command JUST typed in
:norm qqy$jq paste “normal commands” without entering insert mode
Advanced
Command line tricks
External programs
Recording
10
qq record to q
q end recording
@q to execute
@@ to repeat
5@@ to repeat 5 times
“qp display contents of register q (normal mode)
<ctrl-R>q display contents of register q (insert mode)
”qdd put changed contacts back into q
@q execute recording/register q
nnoremap ] @l:wbd combining a recording with a map (to end up in command mode)
1. define recording/register
qq:s/ to/ from/gˆMq
4. Complete as follows
:’<,’>norm @q
11
vimrc essentials
Launching IE
Autocmd
autocmd bufenter *.tex map <F1> :!latex % programming keys depending on file type
autocmd bufenter *.tex map <F2> :!xdvi -hush %<.dvi& launch xdvi with current file dvi
autocmd BufRead * silent! %s/[\r \t]\+$// automatically delete whitespace, trailing dos returns
autocmd BufEnter *.php :%s/[ \t\r]\+$//e same but only for php files
<C-R><C-W> pull word under the cursor into a command line or search
<C-R><C-A> pull WORD under the cursor into a command line or search
<C-R>- pull small register (also insert mode)
12
<C-R>[0-9a-z] pull named registers (also insert mode)
<C-R>% pull file name (also #) (also insert mode)
<C-R>=somevar pull contents of a variable (eg :let sray=”ray[0-9]”)
“ Retrieving last command line command for copy & pasting into text
i:
“ Retrieving last Search Command for copy & pasting into text
i/
Numbering lines
13
:’a,’b!perl -pne ’BEGIN{$a=223} substr($ ,2,0)=$a++’ number lines starting from arbitrary number
qqmnYP‘nˆAq in recording q repeat with @q
:.,$g/ˆ\d/exe “normal! \” increment existing numbers to end of file
o23qqYpq40@q generate a list of numbers 23-64
4
Advanced incrementing
let g:I=0
function! INC(increment)
let g:I =g:I + a:increment
return g:I
end function
:let I=223
:’a,’bs/ˆ/\=INC(5)/
Complex vim
Syntax highlighting
:set syntax=perl force Syntax coloring for a file that has no extension .pl
4 Script required: increment.vim (http://vim.sourceforge.net/tip\_view.php?tip\_id=156)
14
:set syntax off remove syntax coloring (useful for all sorts of reasons)
:colorscheme blue change coloring scheme (any file in ∼vim/vim??/colors)
vim:ft=html: force HTML Syntax highlighting by using a modeline
:syn match DoubleSpace “ “ example of setting your own highlighting
:hi def DoubleSpace guibg=#e0e0e0 sets the editor background
5
Taglist
Folding
Renaming files
Rename files without leaving vim
:r! ls *.c
:%s/\(.*\).c/mv & \1.bla
:w !sh
:q!
Reproducing lines
15
nmap ] i@@@hhkyWjl?@@@P/@@@3s reproduce previous line word by word
6
Reading MS-Word documents
:autocmd BufReadPre *.doc set ro
:autocmd BufReadPre *.doc set hlsearch!
:autocmd BufReadPost *.doc %!antiword “%”
Random functions
Save word under cursor to a file
function! SaveWord()
normal yiw
exe ’:!echo ’.@0.’ $>$$>$ word.txt’
endfunction
:let width = 20
:let fill=’ ’ | while strlen(fill) < width | let fill=fill.fill | endwhile
:%s/\([ˆ;]*\);\=/\=strpart(submatch(1).fill, 0, width)/ge
:%s/\s\+$//ge
function! CSVH(x)
execute ’match Keyword /\^{}$\backslash$([\^{},]*,$\backslash$)$\backslash$\{’.a:x.’\}$\backslash$zs[\^{},]*/’
execute ’normal \^{}’.a:x.’f,’
endfunction
Miscallaenous commands
16
Vim traps
In regular expressions you must backslash + (match 1 or more)
In regular expressions you must backslash | (or)
Help
Fun
:h 42
:h holy-grail
:h!
vim -c “:%s%s*%Cyrnfr)fcbafbe[Oenz(Zbbyranne%|:%s)[[()])-)Ig|norm Vg?”
17
Further Information
If you liked these please visit http://vim.sourceforge.net/tip_view.php?tip_id=305 and vote for this
tip!
Contact
Author (David Rayner)
Please email any comments, suggestions including spelling, grammatical and/or formatting issues with this
document to gavin(at)brokentrain.net
Updated version at http://gav.brokentrain.net/projects/vimtips/vimtips.pdf
Links
18