Vim Tips: David Rayner, Gavin Gilmour August 15, 2010
Vim Tips: David Rayner, Gavin Gilmour August 15, 2010
Vim Tips: David Rayner, Gavin Gilmour August 15, 2010
Contents
1 Text manipulation 1.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.3 1.4 1.5 1.6 1.7 1.8 Search for declaration of subroutine/function under cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Search for visually highlighted text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Filter all form elements into paste register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substitue within substituion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Substituting a visual area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5 6 6 6 7 7 8 8 9 9 10 10 10 12 12
Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Global command display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Global combined with substitute (power editing) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Changing case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reformatting text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deletion without destroying buer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Essential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS CONTENTS 2.2 Opening les & other tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 2.4 2.5 2.6 2.7 2.8 2.9 Multiple les management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . File-name manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Command over multiple les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sessions (set of les) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.7.1 Creating your own GUI Toolbar entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Markers & moving about . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Editing/moving within insert mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.10.1 Display RGB colour under the cursor eg #445588 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Registers 3.1 3.2 3.3 3.4 3.5 List your registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Appending to registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a register as a map (preload registers in .vimrc) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Redirection & paste register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4.1 Copy full path name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Useful tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 13 14 14 14 15 15 15 16 16 17 17 17 17 17 18 18 19 19 19 20 20 21 21 21
4 Advanced 4.1 4.2 4.3 4.4 4.5 4.6 Command line tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . External programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Recording . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3.1 Operating a Recording on a Visual BLOCK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Quick jumping between splits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Visual mode basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vimrc essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Conventional shifting and indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pulling objects onto command/search line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Capturing output of current script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.10 Inserting DOS carriage returns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.11 Perform an action on a particular le or le type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.12 Inserting line number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.13 Numbering lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.14 Advanced incrementing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.14.1 Create list starting from 223 incrementing by 5 between markers a,b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.14.2 Create a map for INC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.15 Digraphs (non alpha-numerics) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.16 Complex vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.17 Syntax highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.18 Preventions and security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.19 Taglist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.20 Folding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.21 Renaming les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.22 Reproducing previous line word by word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.23 Reading MS-Word documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.24 Random functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.24.1 Save word under cursor to a le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.24.2 Delete duplicate lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.24.3 Columnise a CSV le for display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.24.4 Highlight a particular csv column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 29 29 31 32 32 32 32 32
4.26 Vim traps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.27 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Fun 6 Contact 6.1 6.2 6.3 6.4 Author (David Rayner) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Maintainer (Gavin Gilmour) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1
Searching
TEXT MANIPULATION
1
1.1
Text manipulation
Searching
/joe/e 3/joe/e+1 /joe/s-2 /^joe.*fred.*bill/ /^[A-J]\+/ /begin\_.*end /fred\_s*joe/ /fred\|joe /.*fred\&.*joe /\<fred\>/i /\<\d\d\d\d\> /\D\d\d\d\d\D /\<\d\{4}\> /\([^0-9]\|^\)%.*% /^\n\{3} /\(fred\).*\(joe\).*\2.*\1 /^\([^,]*,\)\{8} /<\zs[^>]*\ze> /<\@<=[^>]*>\@= /<\@<=\_[^>]*>\@= /<!--\_p{-}--> /fred\_s*joe/ /bugs\(\_.\)*bunny :h \_ :bufdo /searchstr/ :bufdo %s/searchstr/&/gic ?http://www.vim.org/ /\c\v([^aeiou]&\a){4} cursor set to end of match nd 3rd joe cursor set to End of match plus 1 cursor set to start of match minus 2 normal search for lines beginning with one or more A-J search over possible multiple lines any whitespace including newline search for fred or joe search for fred and joe in any order search for fred but not alfred or frederick search for exactly 4 digit numbers search for exactly 4 digit numbers same thing search for absence of a digit or beginning of line nd 3 empty lines using rexexp memory in a search repeating the regexp (rather than what the regexp nds) search for tag contents, ignoring chevrons (:h /\zs) search for tag contents, ignoring chevrons search for tags across possible multiple lines search for multiple line comments any whitespace including newline (\_) bugs followed by bunny anywhere in le help multiple le search (use :rewind to recommence search) multiple le search better but cheating (n, then a to stop) search backwards for a URL without backslashing search for 4 consecutive consonants
1.2 Substitution 1.1.1 Search for declaration of subroutine/function under cursor :nmap gx yiw/^\(sub\<bar>function\)\s\+<C-R>"<CR> 1.1.2 Search for visually highlighted text
TEXT MANIPULATION
1.2
Substitution
:%s/fred/joe/igc :%s/\r//g :%s/\r/\r/g :%s= *\$== :%s= \+\$== :%s#\s*\r\?\$## :%s#\s*\r*\$## :%s/^\n\{3}// :%s/^\n\+/\r/ :%s#<[^>]\+>##g :a,bg/fred/s/dick/joe/igc :%s= [^ ]\+\$=&&= :%s= \f\+\$=&&= :%s= \S\+\$=&& :s/\(.*\):\(.*\)/\2 : \1/ :%s/^\(.*\)\n\1\$/\1/ :%s/^.\{-}pdf/new.pdf/ :%s#\<[zy]\?tbl\_[a-z\_]\+\>#\L&#gc :%s/// :help /\{-} :s/fred/a/g general substitute command delete dos returns ^M turn dos returns ^M into real returns (xes joined lines) delete end of line blanks same as above clean both trailing spaces AND dos returns same thing deleting empty lines delete blocks of 3 empty lines compressing empty lines delete html tags, leave text VERY USEFUL duplicate end column same as above usually the same reverse elds separated by : delete duplicate lines delete to 1st pdf only lowercase with optional leading characters delete possibly multi-line comments help non-greedy sub fred with contents of register a
1.2
Substitution :s/fred/\=@a/g :%s/\f\+\.gif\>/\r&\r/g | v/\.gif\$/d | %s/gif/jpg/ :%s/a/but/gie|:update|:next :%s/suck\|buck/loopy/gc :s/\_\_date\_\_/\=strftime("%c")/ :%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2: :%s:\(\w\+\)\(.*\s\+\)\(\w\+\)\$:\3\2\1: :%s/\d\+/\=(submatch(0)-3)/ :g/loc\|function/s/\d/\=submatch(0)+6/ :%s#txtdev\zs\d#\=submatch(0)+1#g :%s/\(gg\)\@<=\d\+/\=submatch(0)+6/ :let i=10 | a,bg/Abc/s/yy/\=i/ |let i=i+1 :let i=10 | a,bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 :%s/"\([^.]\+\).*\zsxx/\1/ :nmap <leader>z :%s#\<<c-r>=expand("<cword>")<cr>\># :vmap <leader>z :<C-U>%s/\<<c-r>*\>/
1 TEXT MANIPULATION better alternative as register not displayed multiple commands on one line then use @: to repeat ORing (must break pipe) insert datestring working with columns sub any str1 in col3 swapping rst and last column (4 columns) decrement numbers by 3 increment numbers by 6 on certain lines only better version of above increment only numbers gg\d\d by 6 (another way) convert yy to 10,11,12 etc convert xxyy to xx11,xx12,xx13 (more presise) nd replacement text, use \zs to simplify substitute pull word under cursor into LHS of a substitute pull visually highlighted text into LHS of a substitute
1.2.1
:redir @*|sil exec g#<\(input\|select\|textarea\|/\=form\)\>#p|redir END :nmap ,z :redir @*<Bar>sil exec \ g@<\(input\<Bar>select\<Bar>textarea\<Bar>/\=form\)\>@p<Bar>redir END<CR> 1.2.2 Substitue within substituion :%s,\(all/.*\)\@<=/,_,g :s#all/\zs.*#\=substitute(submatch(0), /, _, g)# :s#all/#&^M#|s#/#_#g|-j! :%s/.*/\=cp .submatch(0). all/.substitute(submatch(0),/,_,g)/ replace all / with AFTER all/ same thing sub by splitting line, re-joining sub inside sub
1.3 Global command display 1.2.3 Substituting a visual area :<,>s/Emacs/Vim/g gv remember you DONT type the <.> re-select the previous visual area (ULTRA)
TEXT MANIPULATION
1.3
:g/gladiolli/# :g/fred.*joe.*dick/ & display all lines fred,joe :g/\<fred\>/ :g/^\s*\$/d :g!/^dd/d :v/^dd/d :g/fred/,/joe/d :g/-------/.-10,.d & delete string :g/{/ ,/}/- s/\n\+/\r/g :v/\S/d :v/./,/./-j :g/^\$/,/./-j :g/<input\|<form/p :g/^/put_ :g/^/m0 :a,b/^/mb :g/^/t. :g/fred/t\$ :g/stage/ta :g/\(^I[^^I]*\)\{80}/d :a,bg/somestr/co/otherstr/ :a,bg/str1/s/str1/&&&/|mo/str2/ :%norm jdd :.,\$g/^\d/exe "norm! \" :a,bg/\d\+/norm! ^A :g/fred/y A
1.5 Changing case :g/fred/y A | :let @*=@a :let @a="|g/Barratt/y A |:let @*=@a :a,b g/^Error/ . w >> errors.txt :g/./yank|put|-1s//"/g|s/.*/Print &/ :g/^MARK\$/r tmp.ex | -d :g/<pattern>/z#.5 :g/<pattern>/z#.5|echo "==========" :g/|/norm 2f|r* :nmap <f3> :redir @a<cr>:g//<cr>:redir END<cr>:new<cr>:put! a<cr><cr>
TEXT MANIPULATION
write out to errors.txt duplicate every line in a le wrap a print around each duplicate replace string with contents of a le, -d deletes the mark display with context display beautifully replace 2nd with a star send output of previous global command to a new window
1.4
1.5
Changing case
guu gUU Vu VU g$\sim$$\sim$ vEU vE$\sim$ ggguG vmap ,c :s/\<\(.\)\(\k*\)\>/\u\1\L\2/g :%s/[.!?]\_s\+\a/\U&\E/g g<C-G> lowercase line uppercase line lowercase line uppercase line ip case line upper case word ip case word lowercase entire le titlise visually selected text (map for .vimrc) uppercase rst letter of sentences count words in text le
1.8
Essential
TEXT MANIPULATION
1.6
Reformatting text
gq} gqap ggVGgq Vgq :s/.\{,69\};\s*\|.\{,69\}\s\+/&\r/g format a paragraph format a paragraph reformat entire le current line break lines at 70 chars, if possible after a ;
1.7
1.8
Essential
* # g* g# % . @: <C-N><C-P> <C-X><C-L> /<C-R><C-W> /<C-R><C-A> :set ignorecase :syntax on :h regexp :nmap ,s :source \$VIM/_vimrc nd word under cursor () (forwards/backwards) match brackets and tags {}, [], (), etc. repeat last modication repeat last : command (then @@) word completion in insert mode line complete SUPER USEFUL pull onto search/command line pull onto search/command line you nearly always want this colour syntax in perl, HTML, PHP etc. list all help topics containing regexp (TAB to step through list) read from vimrc 10
1.8
Essential :nmap ,v :e \$VIM/_vimrc :vmap sb "zdi<b><C-R>z</b><ESC> :vmap st "zdi<?= <C-R>z ?><ESC> open and edit local vimrc wrap <b></b> around VISUALLY selected text wrap <?= ?> around VISUALLY selected text
TEXT MANIPULATION
11
2.2
FILE MANIPULATION
2
2.1
File Manipulation
Exploring
:Exp(lore) :Sex(plore) :ls :cd .. :args :lcd %:p:h :autocmd BufEnter * lcd %:p:h \be \bs le explorer (note: capital E) le explorer in split window list of buers move to parent directory list of les change to directory of current le change to directory of current le automatically buer explorer list of buers buer explorer (split window)
2.2
1 Script
12
2.4
File-name manipulation
FILE MANIPULATION
2.3
2.4
File-name manipulation
:h filename-modifiers :w % :w %:r.cfm help write to current le name change le extention to .cfm 13
2.7
Modelines :!echo %:p :!echo %:p:h <C-R>% "%p /<C-R>% full path & le name full path only insert lename (insert mode) insert lename (normal mode) search for le name in text
FILE MANIPULATION
2.5
2.6
2.7
Modelines
vim:noai:ts=2:sw=4:readonly: vim:ft=html: :h modeline makes readonly says use HTML syntax highlighting help with modelines
14
2.9
FILE MANIPULATION
2.7.1 amenu
Creating your own GUI Toolbar entry Modeline.Insert\ a\ VIM\ modeline \ <esc><esc>ggOvim:ff=unix ts=4 ss=4<CR>vim60:fdm=marker<esc>gg
2.8
2.9
2 (new 3 (new
15
2.10
Abbreviations & maps <C-W> <HOME><END> <C-LEFTARROW><C-RIGHTARROW> <C-X><C-E>,<C-X><C-Y> delete last word beginning/end of line jump one word backwards/forwards scroll while staying put in insert
FILE MANIPULATION
2.10
:map :map :map :map
");
2.10.1
16
3.2
Appending to registers
REGISTERS
3
3.1
Registers
List your registers
:reg :reg a "1p.... :let @y=yy@" qqq :let @a=@_ :let @a="" :let @*=@a display contents of all registers display content of individual registers retrieve numeric registers one by one pre-loading registers (put in .vimrc) empty register q clear register a clear register a copy register a to paste buer
3.2
Appending to registers
3.3
3.4
3.5
Useful tricks "*yy "*p :a,by* :%y* :.y* :nmap p :let @* = substitute(@*,[^[:print:]],,g)"*p yank to paste insert from paste buer yank range into paste yank whole buer into paste yank current line to paster lter non-printable characters
REGISTERS
3.4.1 unix:
nnoremap <F2> :let @*=expand("%:p")<cr> windows: nnoremap <F2> :let @*=substitute(expand("%:p"), "/", "\\", "g")<cr>
3.5
Useful tricks
"ayy@a yy@" u@. :norm qqy$jq execute vim command in a text le same thing using unnamed register execute command JUST typed in paste normal commands without entering insert mode
18
4.2
External programs
ADVANCED
4
4.1
Advanced
Command line tricks
cat xx | gvim - -c "v/^\d\d\|^[3-9]/d" ls | gvim gvim ftp://www.somedomain.com/index.html gvim -h gvim -o file1 file2 gvim -c "/main" joe.c & open joe.c gvim -c "%s/ABC/DEF/ge | update" file1.c gvim -c "argdo %s/ABC/DEF/ge | update" *.c gvim -c "argdo /begin/+1,/end/-1g/^/d | update" *.c gvim -s "convert.vim" file.c gvim -u NONE -U NONE -N gvim -c normal ggdG"*p c:/aaa/xp gvim -c s/^/\=@*/|hardcopy!|q! gvim -d file1 file2 dp do :grep somestring *.php :h grep lter a stream edit a stream! uses netrw.vim help open into a split jump to main execute multiple command on a single le execute multiple command on a group of les remove blocks of text from a series of les automate editing of a le (ex commands in convert.vim) load vim without .vimrc and plugins (clean vim) access paste buer contents (put in a script/batch le) print paste contents to default printer vimdi (compare dierences) put dierence under cursor to other le get dierence under cursor from other le internal grep creates a list of all matching les use :cn(ext) :cp(rev) to navigate list
4.2
External programs
:r!ls.exe !!date :%!sort -u :a,b!sort -u !1} sort -u map <F9> :w:!c:/php/php.exe % map <F2> :w:!perl -c % :runtime! syntax/2html.vim reads in output of ls same thing (but replaces/lters current line) sort unique content as above sorts paragraph (note normal mode!!) run le through php run le through perl convert txt to html 19
4.3
Recording
ADVANCED
4.3
Recording
qq q @q @@ 5@@ "qp <ctrl-R>q "qdd @q nnoremap ] @l:wbd record to q end recording to execute to repeat to repeat 5 times display contents of register q (normal mode) display contents of register q (insert mode) put changed contacts back into q execute recording/register q combining a recording with a map (to end up in command mode)
4.3.1
dene recording/register qq:s/ to/ from/g^Mq dene Visual BLOCK V hit : and the following appears :<,> complete as follows :<,>norm @q
20
4.6
vimrc essentials
ADVANCED
4.4
4.5
4.6
vimrc essentials
set set set set set incsearch wildignore=*.o,*.obj,*.bak,*.exe shiftwidth=3 vb t_vb=". browsedir=buffer jumps to search word as you type tab complete now ignores these for shift/tabbing set silent (no beep!) make open directory use current directory
4.6.1
Launching IE
4.8 Pulling objects onto command/search line :nmap ,i :update<CR>: !start c:\progra~1\intern~1\iexplore.exe <cWORD><CR> 4.6.2 FTPing from vim
ADVANCED
cmap ,r :Nread ftp://209.51.134.122/public_html/index.html cmap ,w :Nwrite ftp://209.51.134.122/public_html/index.html gvim ftp://www.somedomain.com/index.html # uses netrw.vim 4.6.3 Autocmd autocmd autocmd autocmd autocmd bufenter *.tex map <F1> :!latex % bufenter *.tex map <F2> :!xdvi -hush %<.dvi& BufRead * silent! %s/[\r \t]\+\$// BufEnter *.php :%s/[ \t\r]\+\$//e programming keys depending on le type launch xdvi with current le dvi automatically delete whitespace, trailing dos returns same but only for php les
4.7
4.8
4.11
Perform an action on a particular le or le type <C-R><C-A> pull <C-R>pull <C-R>[0-9a-z] pull <C-R>% pull <C-R>=somevar pull
4 WORD under the cursor into a command line or search small register (also insert mode) named registers (also insert mode) le name (also #) (also insert mode) contents of a variable (eg :let sray="ray[0-9]")
ADVANCED
4.9
4.10
4.11
autocmd VimEnter c:/intranet/note011.txt normal! ggVGg? autocmd FileType *.pl exec(set fileformats=unix) Retrieving last command line command for copy & pasting into text
23
4.14 Advanced incrementing i<c-r>: Retrieving last Search Command for copy & pasting into text i<c-r>/
ADVANCED
4.12
4.13
Numbering lines
:set number :map <F12> :set number!<CR> :%s/^/\=strpart(line(.)." ",0,&ts) :a,b!perl -pne BEGIN{$a=223} substr($_,2,0)=$a++ qqmnYPn^Aq :.,$g/^\d/exe "normal! \<c-a>" show line numbers map to toggle line numbers number lines starting from arbitrary number in recording q repeat with @q increment existing numbers to end of le
4.14
Advanced incrementing
let g:I=0 function! INC(increment) let g:I =g:I + a:increment return g:I end function 4.14.1 Create list starting from 223 incrementing by 5 between markers a,b
:let I=223 24
ADVANCED
4.15
4.16
Complex vim
:%s/\<\(on\|off\)\>/\=strpart("offon", 3 * ("off" == submatch(0)), 3)/g :vnoremap <C-X> <Esc>."gvP"P swap two words swap two words
4.17
Syntax highlighting
:set syntax=perl force Syntax coloring for a le that has no extension .pl 25
4.20
Folding :set syntax off :colorscheme blue vim:ft=html: :syn match DoubleSpace " " :hi def DoubleSpace guibg=#e0e0e0 remove syntax coloring (useful for all sorts of reasons) change coloring scheme (any le in vim/vim??/colors) force HTML Syntax highlighting by using a modeline example of setting your own highlighting sets the editor background
ADVANCED
4.18
4.19
Taglist
:Tlist <C-]>
4.20
Folding
zf} v}zf zfa zo zc fold paragraph using motion fold paragraph using visual fold to mark open fold re-close fold
4 Script
26
4.21
Renaming les
ADVANCED
4.21
Renaming les
Rename les without leaving vim :r! ls *.c :%s/\(.*\).c/mv & \1.bla :w !sh :q!
4.22
4.23
:autocmd BufReadPre *.doc set ro :autocmd BufReadPre *.doc set hlsearch! :autocmd BufReadPost *.doc %!antiword "%"
4.24
4.24.1
Random functions
Save word under cursor to a le
function! SaveWord() normal yiw exe :!echo .@0. >> word.txt endfunction
5 Program
27
4.25 Miscallaenous commands 4.24.2 Delete duplicate lines function! Del() if getline(".") == getline(line(".") - 1) norm dd endif endfunction 4.24.3 Columnise a CSV le for display
ADVANCED
: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 4.24.4 Highlight a particular csv column
function! CSVH(x) execute match Keyword /^\([^,]*,\){.a:x.}\zs[^,]*/ execute normal ^.a:x.f, endfunction command! -nargs=1 Csv :call CSVH(<args>) Call with :Csv 5 to highlight fth column
4.25
Miscallaenous commands
:scriptnames :verbose set history? :function :func SearchCompl list all plugins, .vimrcs loaded (super) reveals value of history and where set list functions List particular function
28
4.27
Help
ADVANCED
4.26
Vim traps
In regular expressions you must backslash + (match 1 or more) In regular expressions you must backslash (or) In regular expressions you must backslash ( (group) In regular expressions you must backslash { (count) /fred\+/ /\(fred\)\{2,3}/ /codes\(\n\|\s\)*where /\vcodes(\n|\s)*where matches fred/freddy but not free note what you have to break normal regexp very magic
4.27
Help
:h quickref :h tips :h visual<C-D><TAB> :h ctrl<C-D> :helpg uganda :h :r :h CTRL-R :h /\r :h \\zs :h i_CTRL-R :h c_CTRL-R :h v_CTRL-V :h tutor <C-[>, <C-T> gvim -h :helptags /vim/vim64/doc :help add-local-help vim quick reference sheet (ultra) vims own tips help obtain list of all visual help topics list help of all control keys grep help les use :cn, :cp to nd next help for :ex command normal mode whats \r in a regexp (matches a CR) double up backslash to nd \zs in help help for say C-R in insert mode help for say C-R in command mode visual mode vim tutor Move back & forth in help history vim command line help rebuild all *.txt help les in /doc
29
4.27
Help
ADVANCED
30
FUN
Fun
31
6.4
Links
CONTACT
6
6.1
Contact
Author (David Rayner)
Please email any errors or further tips etc to david(at)rayninfo.co.uk Updated version at http://www.rayninfo.co.uk/vimtips.html
6.2
Please email any comments, suggestions including spelling, grammatical and/or formatting issues with this document to gavin(at)brokentrain.net Updated version available at: http://gavin.brokentrain.net/projects/vimtips/vimtips.pdf
6.3
Source
6.4
Links
http://www.vim.org/ http://chronos.cs.msu.su/vim/newsgroup.html http://groups.yahoo.com/group/vim http://u.webring.com/hub?ring=vim http://www.truth.sk/vim/vimbook-OPL.pdf http://vimdoc.sourceforge.net/ http://www.faqs.org/faqs/editor-faq/vim/ Ocial site Newsgroup and Usenet Specic newsgroup VIM Webring Vim Book Searchable VIM Doc VIM FAQ
32