Best Vim Tips(2)
Markers and moving about
'. : jump to last modification line (SUPER)
`. : jump to exact spot in last modification line
<C-O> : retrace your movements in file (backward)
<C-I> : retrace your movements in file (forward)
:ju(mps) : list of your movements {{help|jump-motions}}
:history : list of all your commands
Abbreviations and maps
:map <F7> :'a,'bw file " Write the lines from mark a to mark b to 'file'
:map <F8> :.w file<CR> " Write the current line to 'file'
:map <F9> :r file " Read text from 'file' and insert it below the current line
:map <F10> :w<CR>:!php %<CR> " Write the file and run it through php
:ab php " list abbreviations beginning with php
:map \ " list maps beginning with \
For use in maps
<CR> : carriage Return for maps
<Esc> : Escape
<Leader> : normally \ change with :let mapleader = ","
<Bar> : | pipe
List registers
:reg : display contents of all registers
"1p : paste from register 1
Execute command from buffer contents
"ayy@a : execute the Vim command in the current line
yy@" : same
Get output from shell commands
These use external programs – ls, grep, date, sort, … (see :help :sort to learn how to use Vim's built-in sort).
:r!ls : reads in output of ls (use dir on Windows)
:r !grep "^ebay" file.txt : read output of grep
:20,25 !rot13 : rot13 lines 20 to 25
:r!date : insert date (use date /T on Windows)
:.!sh : execute contents of current line in buffer and capture the output
Sorting with external sort
:%!sort -u : contents of the current file is sorted and only unique lines are kept
:'v,'w!sort : sort from line marked v thru lines marked w
:g/^$/;,/^$/-1!sort : sort each block (note the crucial ;)
!1} sort : sorts paragraph; this is issued from normal mode!)
Entering !! in normal mode is translated to :.!
Appending a command sends the current line to the command replacing it with command's result
!!date : Replace current line with date
!!which command : Replace current line with the absolute path to command
!!tr -d AEIO : translate current line deleting As, Es, Is, and Os from the current line
You can also use ! on a visual selection. Select an area with one of the visualmode
commands, and then type !command to pipe the whole selection through command.
This is equivalent to :'<,'>!command.
For example, after selecting multiple lines with visualmode:
!sort : sort selected lines
!grep word : keep only lines containing 'word' in the selected range.
Multiple files
:wn : write file and move to next (SUPER)
:bd : remove file from buffer list (SUPER)
:sav php.html : Save current file as php.html and "move" to php.html
:sp fred.txt : open fred.txt into a split
:e! : return to unmodified file
:w /some/path/%:r : save file in another directory, but with the same name
:e # : edit alternative file
:args : display argument list
:n : next file in argument list
:prev : previous file in argument list
:rew : rewind to first file in argument list
:ls : display buffer list
:bn : next buffer
:bp : previous buffer
:brew : rewind to first buffer in buffer list
:tabe : open new tab page (Ctrl-PgUp, Ctrl-PgDown for next/previous tab)
:tabm n : move tab to position n (0=leftmost position)
Recording
qa : record keystrokes to register a
your commands
q : quit recording
@a : execute commands again
@@ : repeat
# editing a register/recording
"ap
<you can now see register contents, edit as required>
"add
@a
:%normal @a #execute the macro recorded in register a on all lines of the current file.
#or, with a visually selected set of lines:
:normal @a
vimrc essentials
:set incsearch : jumps to search word as you type (annoying but excellent)
:set wildignore=*.o,*.obj,*.bak,*.exe
:syntax on : display syntactical elements by color based on filetype ( extension )
Launching programs under Windows
There are a number of options to run applications on the Windows platform.
This causes Windows to launch the program associated with the file extension. It also restores the paste buffer to its original value:
" This command will execute the file, for example, if this is an
" HTML file, it will run:
" start c:\absolute\filename.html
nnoremap <silent> <C-F6> :let old_reg=@"<CR>:let @"=substitute(expand("%:p"), "/", "\\", "g")<CR>:silent!!cmd /cstart <C-R><C-R>"<CR><CR>:let @"=old_reg<CR>
You can also use Windows rundll32.exe for some options:
" vmap <silent> <C-F5> :<C-U>let old_reg=@"<CR>gvy:silent!!start rundll32.exe url.dll,FileProtocolHandler <C-R><C-R>"<CR><CR>:let @"=old_reg<CR>
Or, for example, you can launch Internet Explorer directly:
:nmap <Leader>f :update<CR>:silent !start c:\progra~1\intern~1\iexplore.exe file://%:p<CR>
:nmap <Leader>i :update<CR>:!start c:\progra~1\intern~1\iexplore.exe <cWORD><CR>
FTP from Vim
cmap <Leader>r :Nread ftp://209.51.134.122/public_html/index.html
cmap <Leader>w :Nwrite ftp://209.51.134.122/public_html/index.html
gvim ftp://209.51.134.122/public_html/index.html
: For ascii file transfers add the following line to your .vimrc
let g:netrw_ftpmode="ascii"
Appending to registers
# yank 5 lines into "a" then add a further 5
"a5yy
10j
"A5yy
[I : show lines matching word under cursor <cword>
Conventional shifting
:'a,'b>>
# visual shifting (builtin-repeat)
:vnoremap < <gv
:vnoremap > >gv
Searching
/^fred.*joe.*bill : line beginning with fred, followed by joe then bill
/^[A-J] : line beginning A-J
/^[A-J][a-z]\+\s : line beginning A-J then one or more lowercase characters then space or tab
/fred\_.\{-}joe : fred then anything then joe (over multiple lines)
/fred\_s\{-}joe : fred then any whitespace (including newlines) then joe
/fred\|joe : fred OR joe
Substitution
:%s/fred/joe/igc : general substitute command
:%s/\r//g : delete DOS Carriage Returns (^M)
:'a,'bg/fred/s/dick/joe/gc : VERY USEFUL
:s/\(.*\):\(.*\)/\2 : \1/ : reverse fields separated by :
# non-greedy matching \{-}
:%s/^.\{-}pdf/new.pdf/ : to first pdf)
:s/fred/<c-r>a/g : substitute "fred" with contents of register "a"
:%s/^\(.*\)\n\1/\1$/ : delete duplicate lines
:help /\{-}
# multiple commands
:%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/
:%s/suck\|buck/loopy/gc : ORing
:s/__date__/\=strftime("%c")/ : insert datestring
Replace FIX delimiter for a 'caret':
'01' is the hex for the FIX protocol delimiter. You can move your cursor over a character and press 'ga' to see a character's hex value.
:%s/\%x01/^/g
Global command
:g/one\|two/ : list lines containing "one" or "two"
:g/^\s*$/d : delete all blank lines
:g/green/d : delete all lines containing "green"
:v/green/d : delete all lines not containing "green"
:g/one/,/two/d : not line based
:v/./.,/./-1join : compress empty lines
Between lines with marks a and b (inclusive), append each line starting with "Error" to a file:
:'a,'b g/^Error/ .w >> errors.txt
Delete all lines containing "green" but not "red" or "pink". Command :g/^/ matches every line; the current line is copied into variable x; if any part of x matches (case sensitive) "green" and not "red" and not "pink", the line is deleted. Replace # with ? for case insensitive.
:g/^/let x=getline('.') | if x=~#'green' && x!~#'red' && x!~#'pink' | d | endif
Paste register *
:redir @* : redirect commands to paste
:redir END
"*yy : yank to paste
"*p : insert paste buffer
Formatting text
gq<CR>
gqap (a is motion p paragraph (visual mode))
Operate command over multiple files
:argdo %s/foo/bar/
:bufdo %s/foo/bar/
:windo %s/foo/bar/
:tabdo %s/foo/bar/
Command line tricks
gvim -h
ls | gvim - : edit a PIPE!
# vg.ksh (shell script)
# vi all files in directory containing keyword $1 and jump to $1
gvim.exe -c "/$1" $(grep -isl "$1" *) &
Preview in web browser
#add this to your .vimrc:
command Preview :!firefox %<CR>
#if you are using windows, you will need to adjust your PATH to include the path to your browser.
Comments
The Buffer Explorer scripts mentioned above (\be \bs) rely on the popular script bufexplorer.vim.
Have recently started to appreciate taglist.vim (the most popular Vim script) it really comes into it's own with very long programs containting lots of subroutines/functions as it shows which function/sub you're in etc script#273.
Vim traps
In regular expressions you must backslash + (match 1 or more).
/fred\+/ : matches fred/freddy but not free
\v (very magic) reduces backslashing
/codes\(\n\|\s\)*where : normal regexp
/\vcodes(\n|\s)*where : very magic
You must Sign up as a member of Effecthub to view the content.
A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_ACCEPT_LANGUAGE
Filename: helpers/time_helper.php
Line Number: 22
2167 views 0 comments
You must Sign up as a member of Effecthub to join the conversation.