Vim Tips
Vim Tips
Contents
1 Text manipulation 4
1.1 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.1 Search for declaration of subroutine/function under cursor . . . . . . . . . . . . . . . . 4
1.1.2 Search for visually highlighted text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Filter all form elements into paste register . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.2 Substitue within substituion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.3 Substituting a visual area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Global command display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Global combined with substitute (power editing) . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Changing case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Reformatting text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.7 Deletion without destroying buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.8 Essential . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 File Manipulation 8
2.1 Exploring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Opening files & other tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3 Multiple files management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4 File-name manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Command over multiple files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.6 Sessions (set of files) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.7 Modelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.7.1 Creating your own GUI Toolbar entry . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.8 Markers & moving about . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.9 Editing/moving within insert mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.10 Abbreviations & maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.10.1 Display RGB colour under the cursor eg #445588 . . . . . . . . . . . . . . . . . . . . 11
1
CONTENTS CONTENTS
3 Registers 11
3.1 List your registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Appending to registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Using a register as a map (preload registers in .vimrc) . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Redirection & paste register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4.1 Copy full path name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 Useful tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Advanced 13
4.1 Command line tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 External programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3 Recording . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3.1 Operating a Recording on a Visual BLOCK . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 Quick jumping between splits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.5 Visual mode basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.6 vimrc essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6.1 Launching IE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6.2 Ftping from vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6.3 Autocmd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.7 Conventional shifting and indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.8 Pulling objects onto command/search line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.9 Capturing output of current script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.10 Inserting DOS carriage returns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.11 Perform an action on a particular file or file type . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.12 Inserting line number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.13 Numbering lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.14 Advanced incrementing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.14.1 Create list starting from 223 incrementing by 5 between markers a,b . . . . . . . . . . 17
4.14.2 Create a map for INC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.15 Digraphs (non alpha-numerics) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.16 Complex vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.17 Syntax highlighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.18 Preventions and security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.19 Taglist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.20 Folding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.21 Renaming files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.22 Reproducing lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.23 Reading MS-Word documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2
CONTENTS CONTENTS
4.24 Random functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.24.1 Save word under cursor to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.24.2 Delete duplicate lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.24.3 Columnise a CSV file for display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.25 Miscallaenous commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.26 Vim traps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.27 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Fun 20
5.1 Contact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1.1 Author (David Rayner) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1.2 Maintainer (Gavin Gilmour) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1.3 Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2 Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3
1.2 Substitution 1 TEXT MANIPULATION
1 Text manipulation
1.1 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>
1.2 Substitution
4
1.2 Substitution 1 TEXT MANIPULATION
:%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
5
1.3 Global command display 1 TEXT MANIPULATION
6
1.8 Essential 1 TEXT MANIPULATION
1.4 Global combined with substitute (power editing)
1.8 Essential
7
2.2 Opening files & other tricks 2 FILE MANIPULATION
. 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
2 File Manipulation
2.1 Exploring
8
2.5 Command over multiple files 2 FILE 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
9
2.8 Markers & moving about 2 FILE MANIPULATION
:bufdo %s/foo/bar/e
:windo %s/foo/bar/e
:argdo exe ’%!sort’|w! include an external command
2.7 Modelines
10
3 REGISTERS
q/ search history window (puts you in full edit mode)
q: commandline history window (puts you in full edit mode)
: history Window
3 Registers
3.1 List your registers
11
3.5 Useful tricks 3 REGISTERS
: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”
1. “a5yy
2. 10j
3. “A5yy
:let @m=”:’a,’bs/”
:let @s=”:%!sort -u”
12
4.3 Recording 4 ADVANCED
“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
4 Advanced
4.1 Command line tricks
4.3 Recording
13
4.5 Visual mode basics 4 ADVANCED
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
14
4.8 Pulling objects onto command/search line 4 ADVANCED
4.6 vimrc essentials
4.6.1 Launching IE
4.6.3 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)
15
4.11 Perform an action on a particular file or file type 4 ADVANCED
<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/
16
4.17 Syntax highlighting 4 ADVANCED
:’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.14.1 Create list starting from 223 incrementing by 5 between markers a,b
:let I=223
:’a,’bs/ˆ/\=INC(5)/
:set syntax=perl force Syntax coloring for a file that has no extension .pl
:set syntax off remove syntax coloring (useful for all sorts of reasons)
17
4.21 Renaming files 4 ADVANCED
: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
4.19 Taglist
4
4.20 Folding
18
4.25 Miscallaenous commands 4 ADVANCED
imap ] @@@hhkyWjl?@@@P/@@@3s reproduce previous line word by word
nmap ] i@@@hhkyWjl?@@@P/@@@3s reproduce previous line word by word
: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
19
5 FUN
4.27 Help
5 Fun
:h 42
:h holy-grail
:h!
vim -c “:%s%s*%Cyrnfr)fcbafbe[Oenz(Zbbyranne%|:%s)[[()])-)Ig|norm Vg?”
20
5.2 Links 5 FUN
5.1 Contact
5.1.1 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 available at: http://gavin.brokentrain.net/projects/vimtips/vimtips.pdf
5.1.3 Source
5.2 Links
21