====== VIM ====== ===== Compilar VIM ===== Descargar de [[http://www.vim.org/download.php|Download]] Descomprimir ./configure --with-features=huge --enable-multibyte --prefix=$HOME/local make install Eliminar luego directorio de fuentes ===== MAPPING ===== launching Win IE nmap ,f :update:silent !start c:\progra~1\intern~1\iexplore.exe file://%:p nmap ,i :update: !start c:\progra~1\intern~1\iexplore.exe MAKE IT EASY TO UPDATE/RELOAD .vimrc nmap ,s :source $VIM/.vimrc nmap ,v :e $VIM/.vimrc Pull word under cursor into LHS of a substitute nmap z :%s#\<=expand("")\># send output of previous global command to a new window nmap :redir @a:g//:redir END:new:put! a Pull word under cursor into LHS of a substitute nmap z :%s#\<=expand("")\># nmap ,t :s/.*/\L&/:s/\<./\u&/g [N] " titlise a line nmap ,z :redir @*sil exec 'g@<\(input\select\textarea\/\=form\)\>@p'redir END nmap p :let @* = substitute(@*,'[^[:print:]]','','g')"*p nnoremap :let @*=expand("%:p") :unix " pull full path name into paste buffer for attachment to email etc nnoremap :let @*=substitute(expand("%:p"), "/", "\\", "g") :win32 Following 4 maps enable text transfer between VIM sessions :map :'a,'bw! c:/aaa/x " save text to file x :map :r c:/aaa/x " retrieve text :map :.w! c:/aaa/xr " store current line :map :r c:/aaa/xr " retrieve current line :ab php " list of abbreviations beginning php :map , " list of maps beginning , allow use of F10 for mapping (win32) set wak=no " :h winaltkeys For use in Maps " carriage Return for maps " Escape " normally \ " | pipe " backspace " No hanging shell window type table,,, to get
### Cool ### imap ,,, bdwa<pa>pa>kA list current mappings of all your function keys :for i in range(1, 12) | execute("map ") | endfor [N] get the original vim command on a mapped key [N] unmap = list current mappings of all your function keys :for i in range(1, 12) | execute("map ") | endfor Pull Visually Highlighted text into LHS of a substitute :vmap z :%s/\<*\>/ insert date eg 31Jan11 [N] :inoremap \zd =strftime("%d%b%y") Titlise Visually Selected Text (map for .vimrc) vmap ,c :s/\<\(.\)\(\k*\)\>/\u\1\L\2/g Title Case A Line Or Selection (better) vnoremap :s/\%V\<\(\w\)\(\w*\)\>/\u\1\L\2/ge [N] titlise a line nmap ,t :s/.*/\L&/:s/\<./\u&/g [N] Pressing F5 lists all buffer, just type number :map :ls:e # Quick jumping between splits map j_ map k_ running file thru an external program (eg php) map :update:!c:/php/php.exe % map :update:!perl -c % visual shifting (builtin-repeat) :vnoremap < >gv Swap word with next word nmap gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/ [N] pull full path name into paste buffer for attachment to email etc nnoremap :let @*=expand("%:p") :unix nnoremap :let @*=substitute(expand("%:p"), "/", "\\", "g") :win32 reproduce previous line word by word imap ] @@@hhkyWjl?@@@P/@@@3s nmap ] i@@@hhkyWjl?@@@P/@@@3s Programming keys depending on file type :autocmd bufenter *.tex map :!latex % :autocmd bufenter *.tex map :!xdvi -hush %<.dvi& macro to comment out a block of code using #if 0 vmap out "zdmzO#if 0"zp'zi#endif visual searching :vmap // y/" " search for visually highlighted text with spec chars :vmap // y/=escape(@", '\\/.*$^~[]') search for declaration of subroutine/function under cursor :nmap gx yiw/^\(sub\function\)\s\+" ===== BUFFERS ===== :close " close a tab but leave the buffer *N* :tab sball " retab all files in buffer (repair) [N] :ls " list of buffers :bn " goto next buffer :bp " goto previous buffer :wn " save file and move to next (super) :wp " save file and move to previous :bd " remove file from buffer list (super) :bun " Buffer unload (remove window but not from list) :badd file.c " file from buffer list :b3 " go to buffer 3 [C] :b main " go to buffer with main in name eg main.c (ultra) :brew " buffer rewind :sball,:sb " Split all buffers (super) :scrollbind " in each split window :set hidden " Allows to change buffer w/o saving current buffer capturing output of current script in a separate buffer :new | r!perl # " opens new buffer,read other buffer :new! x.out | r!perl # " same with named file :new+read!ls create a new buffer, paste a register "q" into it, then sort new buffer :new +put q|%!sort ===== VIMDIFF ===== local and remote files via ssh vimdiff /path/to/file scp://remotehost//path/to/file vim -d file1 file2 " vimdiff (compare differences) dp " "put" difference under cursor to other file do " "get" difference under cursor from other file [c " jump backwards to change ]c " jump forwards to change :set diffopt+=icase " switch case sensitivity on and off complex diff parts of same file [N] :1,2yank a | 7,8yank b ===== Sorting ===== ==== Sorting with internal sort ==== [range]sort - sort the lines in the [range], or all lines if [range] is not given. e.g. '':'<,'>sort'' - sort the current visual selection :'a,'b!sort -u " use an external program to filter content !1} sort -u " sorts paragraph (note normal mode!!) :sort /.*\%2v/ " sort all lines on second column [N] ==== Sorting with external sort ==== :%!sort -u " use an external program to filter content :'a,'b!sort -u " use an external program to filter content !1} sort -u " sorts paragraph (note normal mode!!) :g/^$/;/^$/-1!sort " Sort each block (note the crucial ;) :let @s=":%!sort -u " :g/^$/;/^$/-1!sort " Sort each block (note the crucial ;) ===== Searching ===== ==== searching over multiple lines \_ means including newline ==== /begin\_.*end " search over possible multiple lines / " search for multiple line comments /fred\_s*joe/ " any whitespace including newline [C] ===== How to search for a URL without backslashing ===== ?joe " (first) search BACKWARDS!!! clever huh! /joe/e " cursor set to End of match 3/joe/e+1 " find 3rd joe cursor set to End of match plus 1 [C] /joe/s-2 " cursor set to Start of match minus 2 /joe/+3 " find joe move cursor 3 lines down /.*fred\&.*joe " Search for FRED AND JOE in any ORDER! /\/ " search for fred but not alfred or frederick [C] /\(^str.*\n\)\{2} " find 2 successive lines starting with str [I " show lines matching word under cursor (super) ===== multiple file search ===== :bufdo /searchstr/ " use :rewind to recommence search :bufdo %s/searchstr/&/gic " say n and then a to stop ===== Specify what you are NOT searching for (vowels) ===== /\c\v([^aeiou]&\a){4} " search for 4 consecutive consonants /\%>20l\%<30lgoat " Search for goat between lines 20 and 30 [N] /^.\{-}home.\{-}\zshome/e " match only the 2nd occurence in a line of "home" [N] :%s/home.\{-}\zshome/alone " Substitute only the 2nd occurrence of home in any line [U] ===== find str but not on lines containing tongue ===== ^\(.*tongue.*\)\@!.*nose.*$ \v^((tongue)@!.)*nose((tongue)@!.)*$ .*nose.*\&^\%(\%(tongue\)\@!.\)*$ :v/tongue/s/nose/&/gic ===== Substitute ===== ==== Substitute range and search conditional ==== :'a,'bg/fred/s/dick/joe/igc " VERY USEFUL ==== substitute using a register ==== :s/fred/a/g " sub "fred" with contents of register "a" :s/fred/asome_texts/g :s/fred/\=@a/g " better alternative as register not displayed (not *) [C] ==== substitute string in column 30 [N] ==== :%s/^\(.\{30\}\)xx/\1yy/ ==== find replacement text, put in memory, then use \zs to simplify substitute ==== :%s/"\([^.]\+\).*\zsxx/\1/ ==== substitute singular or plural ==== :'a,'bs/bucket\(s\)*/bowl\1/gic [N] ==== Same thing ==== :s#all/\zs.*#\=substitute(submatch(0), '/', '_', 'g')# ==== Substitute by splitting line, then re-joining ==== :s#all/#&^M#|s#/#_#g|-j! ==== Substitute inside substitute ==== :%s/.*/\='cp '.submatch(0).' all/'.substitute(submatch(0),'/','_','g')/ ==== perform a substitute on every other line ==== :g/^/ if line('.')%2|s/^/zz / ==== Best-Global-combined-with-substitute (*power-editing*) ==== :'a,'bg/fred/s/joe/susan/gic " can use memory to extend matching :/biz/,/any/g/article/s/wheel/bucket/gic: non-line based [N] ==== Do a substitute on last visual area [N] ==== :%s/\%Vold/new/g ==== duplicating columns ==== :%s= [^ ]\+$=&&= " duplicate end column :%s= \f\+$=&&= " Dupicate filename :%s= \S\+$=&& " usually the same ==== Working with Columns sub any str1 in col3 ==== :%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2: ==== Swapping first & last column (4 columns) ==== :%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1: ==== Substitute string ==== :%s/^\(.\{30\}\)xx/\1yy/ " substitute string in column 30 [N] ==== best-substitution ==== :%s/fred/joe/igc " general substitute command :%s//joe/igc " Substitute what you last searched for [N] :%s/~/sue/igc " Substitute your last replacement string [N] :%s/\r//g " Delete DOS returns ^M ==== non-greedy matching \{-} ==== :%s/^.\{-}pdf/new.pdf/ " delete to 1st occurence of pdf only (non-greedy) %s#^.\{-}\([0-9]\{3,4\}serial\)#\1#gic " delete up to 123serial or 1234serial [N] ===== Is your Text File jumbled onto one line? use following ===== :%s/\r/\r/g " Turn DOS returns ^M into real returns :%s= *$== " delete end of line blanks :%s= \+$== " Same thing :%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 (non-greedy) :%s#<\_.\{-1,}>##g " delete html tags possibly multi-line (non-greedy) :%s#.*\(\d\+hours\).*#\1# " Delete all but memorised string (\1) [N] ===== Deleting non-ascii characters (some invisible) ===== :%s/[\x00-\x1f\x80-\xff]/ /g " type this as you see it :%s/[128-255]//gi " where you have to type the Control-V :%s/[€-ÿ]//gi " Should see a black square & a dotted y :%s/[128-25501-31]//gi " All pesky non-asciis :exec "norm /[\x00-\x1f\x80-\xff]/" " same thing ===== parse xml/soap ===== %s#><\([^/]\)#>\r<\1#g " split jumbled up XML file into one tag per line [N] %s#><\([^/]\)#>\r<\1#g " split jumbled up XML file into one tag per line [N] %s/ ===== Copy a set of columns using VISUAL BLOCK (NOT BY ordinary v command) ===== then select "column(s)" with motion commands (win32 ) then c,d,y,r etc ===== columnise a csv file for display only as may crop wide columns ===== :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 ===== Highlight a particular csv column (put in .vimrc) ===== function! CSVH(x) execute 'match Keyword /^\([^,]*,\)\{'.a:x.'}\zs[^,]*/' execute 'normal ^'.a:x.'f,' endfunction command! -nargs=1 Csv :call CSVH() ===== call with ===== :Csv 5 " highlight fifth column zf1G " fold everything before this line [N] ===== memory ===== :%s#example#& = &#gic " duplicate entire matched string [N] :%s#.*\(tbl_\w\+\).*#\1# " extract list of all strings tbl_* from text [NC] :s/\(.*\):\(.*\)/\2 : \1/ " reverse fields separated by : :%s/^\(.*\)\n\1$/\1/ " delete duplicate lines :%s/^\(.*\)\(\n\1\)\+$/\1/ " delete multiple duplicate lines [N] ===== use of optional atom \? ===== :%s#\<[zy]\?tbl_[a-z_]\+\>#\L&#gc " lowercase with optional leading characters ===== over possibly many lines ===== :%s/// " delete possibly multi-line comments :help /\{-} " help non-greedy ===== multiple commands on one line ===== :%s/\f\+\.gif\>/\r&\r/g | v/\.gif$/d | %s/gif/jpg/ :%s/a/but/gie|:update|:next " then use @: to repeat ===== ORing ===== :%s/goat\|cow/sheep/gc " ORing (must break pipe) :'a,'bs#\[\|\]##g " remove [] from lines between markers a and b [N] :%s/\v(.*\n){5}/&\r " insert a blank line every 5 lines [N] ===== Calling a VIM function ===== :s/__date__/\=strftime("%c")/ " insert datestring ===== format a mysql query ===== :%s#\\|\\|\\|\<\inner join\>#\r&#g ===== filter all form elements into paste register ===== :redir @*|sil exec 'g#<\(input\|select\|textarea\|/\=form\)\>#p'|redir END :nmap ,z :redir @*sil exec 'g@<\(input\select\textarea\/\=form\)\>@p'redir END ===== decrement numbers by 3 ===== :%s/\d\+/\=(submatch(0)-3)/ ===== increment numbers by 6 on certain lines only ===== :g/loc\|function/s/\d/\=submatch(0)+6/ ===== better ===== :%s#txtdev\zs\d#\=submatch(0)+1#g :h /\zs ===== increment only numbers gg\d\d by 6 (another way) ===== :%s/\(gg\)\@<=\d\+/\=submatch(0)+6/ :h zero-width ===== rename a string with an incrementing number ===== :let i=10 | 'a,'bg/Abc/s/yy/\=i/ |let i=i+1 # convert yy to 10,11,12 etc ===== as above but more precise ===== :let i=10 | 'a,'bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 # convert xxyy to xx11,xx12,xx13 ===== best-global command ===== :g/joe/,/fred/d " not line based (very powerfull) :g/fred/,/joe/j " Join Lines [N] :g/-------/.-10,.d " Delete string & 10 previous lines :g/{/ ,/}/- s/\n\+/\r/g " Delete empty lines but only between {...} :v/\S/d " Delete empty lines (and blank lines ie whitespace) :v/./,/./-j " compress empty lines :g/^$/,/./-j " compress empty lines :g/^/t. " duplicate every line :g/fred/t$ " copy (transfer) lines matching fred to EOF :g/stage/t'a " copy (transfer) lines matching stage to marker a (cannot use .) [C] :g/^Chapter/t.|s/./-/g " Automatically underline selecting headings [N] :g/\(^I[^^I]*\)\{80}/d " delete all lines containing at least 80 tabs ===== copy after line containing "otherstr" ===== :'a,'bg/somestr/co/otherstr/ " co(py) or mo(ve) ===== as above but also do a substitution ===== :'a,'bg/str1/s/str1/&&&/|mo/str2/ :%norm jdd " delete every other line ===== incrementing numbers (type as 5 characters) ===== :.,$g/^\d/exe "norm! \": increment numbers :'a,'bg/\d\+/norm! ^A " increment numbers ===== storing glob results (note must use APPEND) you need to empty reg a first with qaq. ===== :g/fred/y A " append all lines fred to register a :g/fred/y A | :let @*=@a " put into paste buffer :g//y A | :let @*=@a " put last glob into paste buffer [N] :let @a=''|g/Barratt/y A |:let @*=@a ===== filter lines to a file (file must already exist) ===== :'a,'bg/^Error/ . w >> errors.txt ===== duplicate every line in a file wrap a print '' around each duplicate ===== :g/./yank|put|-1s/'/"/g|s/.*/Print '&'/ ===== replace string with contents of a file, -d deletes the "mark" ===== :g/^MARK$/r tmp.txt | -d ===== display prettily ===== :g//z#.5 " display with context :g//z#.5|echo "==========" " display beautifully ===== Combining g// with normal mode commands ===== :g/|/norm 2f|r* " replace 2nd | with a star ===== Find fred before beginning search for joe ===== :/fred/;/joe/-2,/sid/+3s/sally/alley/gIC ===== create a new file for each line of file eg 1.txt,2.txt,3,txt etc ===== :g/^/exe ".w ".line(".").".txt" ===== chain an external command ===== :.g/^/ exe ".!sed 's/N/X/'" | s/I/Q/ [N] ===== Operate until string found [N] ===== d/fred/ "delete until fred y/fred/ "yank until fred c/fred/e "change until fred end v12| " visualise/change/delete to column 12 [N] ===== Summary of editing repeats [N] ===== . last edit (magic dot) :& last substitute :%& last substitute every line :%&gic last substitute every line confirm g% normal mode repeat last substitute g& last substitute on all lines @@ last recording @: last command-mode command :!! last :! command :~ last substitute :help repeating ===== Summary of repeated searches ===== ; last f, t, F or T , last f, t, F or T in opposite direction n last / or ? search N last / or ? search in opposite direction ===== Absolutely-essential ===== * # g* g# " find word under cursor () (forwards/backwards) % " match brackets {}[]() . " repeat last modification @: " repeat last : command (then @@) matchit.vim " % now matches tags