diff --git a/fortunes/vimtips b/fortunes/vimtips index d22d43a..f7cd759 100644 --- a/fortunes/vimtips +++ b/fortunes/vimtips @@ -1,94 +1,5 @@ zz to center the cursor vertically on your screen. useful when you 250gzz, for instance. % -CTRL-w | and CTRL-W _ maximize your current split vertically and horizontally, respectively. CTRL-W = equalizes 'em. -% -%s/^\n// to delete all empty lines -% -:g/_pattern_/s/^/#/g will comment out lines containing _pattern_ (if '#' is your comment character/sequence) -% -vim -c [command] will launch vim and run a : command at launch, e.g. "vim -c NERDTree." -% -CTRL-w s CTRL-w T will open current buffer in new tab -% -CTRL-w K will switch vertical split into horizontal, CTRL-w H will switch a horizontal into a vertical. -% -:w !sudo tee % will use sudo to write the open file (have you ever forgotten to `sudo vim /path/to/file`?) -% -K runs a program to lookup the keyword under the cursor. If writing C, it runs man. In Ruby, it (should) run ri, Python (perhaps) pydoc. -% -Edit and encrypt a file: vim -x filename -% -:%s//joe/igc substitute your last search with joe. -% -/fred\_s*joe/ will search for fred AND joe with whitespace (including newline) in between. -% -From a command-line, vim scp://username@host//path/to/file to edit a remote file locally. -% -/fred|joe/ will search for either fred OR joe. -% -/.*fred&.*joe/ will search for fred AND joe, in any order. -% -// will search for fred, but not alfred or frederick. -% -/joe/e will search for joe and place the cursor at the end of the match. -% -/joe/e+1 will search for joe and place the cursor at the end of the match, plus on character. -% -/joe/s-2 will search for joe and place the cursor at the start of the match, minus two characters. -% -/joe/+3 will search for joe and place the cursor three lines below the match. -% -/joe.*fred.*bill/ will search for joe AND fred AND bill, in that order. -% -/begin\_.*end will search for begin AND end over multiple lines. -% -:s/\(.*\):\(.*\)/\2 : \1/ will reverse fields separated by ':' -% -%s/a/bar/g will place the contents of register 'a' in the search, and replace it with 'bar'. -% -Edit a command output in Vim as a file: $ command | vim - -% -ggVG= will auto-format the entire document -% -'0 opens the last modified file ('1 '2 '3 works too) -% -In insert mode do Ctrl+r=53+17. This way you can do some calcs with vim. -% -"_d will delete the selection without adding it to the yanked stack (sending it to the black hole register). -% -Basic commands 'f' and 't' (like first and 'til) are very powerful. See :help t or :help f. -% -:40,50m30 will move lines 40 through 50 to line 30. Most visual mode commands can be used with line numbers. -% -To search for a URL without backslashing, search backwards! Example: ?http://somestuff.com -% -:%s/~/sue/igc substitute your last replacement string with 'sue'. -% -g; will cycle through your recent changes (or g, to go in reverse). -% -:%s/^\n\{3}// will delete a block of 3 empty lines. -% -:%s/^\n\+/\r/ will compress multiple empty lines into one. -% -:%s#<[^>]\+>##g will remove HTML tags, leaving the text. (non-greedy) -% -:%s#.*\(hours\).*#\1# will delete everything on a line except for the string 'hours'. -% -"2p will put the second to last thing yanked, "3p will put the third to last, etc. -% -:wa will save all buffers. :xa will save all buffers and exit Vim. -% -After performing an undo, you can navigate through the changes using g- and g+. Also, try :undolist to list the changes. -% -You probably know that 'u' is undo. Do you know that Ctrl-R is redo? -% -ci{ change text inside {} block. Also see di{, yi{, ci( etc. -% -:set autochdir instead of :lcd %:p:h in your vimrc to change directories upon opening a file. -% -:read [filename] will insert the contents of [filename] at the current cursor position -% -to use gvim to edit your git messages set git's core editor as follows: git config --global core.editor "gvim --nofork" % ci" inside a " " will erase everything between "" and place you in insertion mode. @@ -366,5 +277,1304 @@ CTRL-U/CTRL-D - scroll up/down, moving the cursor the same number of lines if po ctrl-b / ctrl-f : page up / page down % ctrl-clic / ctrl-t : go to symbol definition (= ctrl-]) (using tags) and back. You can use "make tags" autotooled projects to create tags - - +% +0/joe/+3 -- find joe move cursor 3 lines down +% +/^joe.*fred.*bill/ -- find joe AND fred AND Bill (Joe at start of line) +% +/^[A-J]/ -- search for lines beginning with one or more A-J +% +/begin\_.*end -- search over possible multiple lines +% +/fred\_s*joe/ -- any whitespace including newline [C] +% +/fred\|joe -- Search for FRED OR JOE +% +/.*fred\&.*joe -- Search for FRED AND JOE in any ORDER! +% +/\/ -- search for fred but not alfred or frederick [C] +% +/\<\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 -- finding empty lines +% +/^str.*\nstr -- find 2 successive lines starting with str +% +/\(^str.*\n\)\{2} -- find 2 successive lines starting with str +% +/\(fred\).*\(joe\).*\2.*\1 -- using rexexp memory in a search find fred.*joe.*joe.*fred *C* +% +/^\([^,]*,\)\{8} -- Repeating the Regexp (rather than what the Regexp finds) +% +:vmap // y/" -- search for visually highlighted text -- visual searching +% +:vmap // y/=escape(@", '\\/.*$^~[]') -- with spec chars +% +/<\zs[^>]*\ze> -- search for tag contents, ignoring chevrons -- \zs and \ze regex delimiters :h /\zs +% +/<\@<=[^>]*>\@= -- search for tag contents, ignoring chevrons -- zero-width :h /\@= +% +/<\@<=\_[^>]*>\@= -- search for tags across possible multiple lines +% +/ -- search for multiple line comments -- searching over multiple lines \_ means including newline +% +/fred\_s*joe/ -- any whitespace including newline *C* +% +/bugs\(\_.\)*bunny -- bugs followed by bunny anywhere in file +% +:h \_ -- help +% +:nmap gx yiw/^\(sub\function\)\s\+" -- search for declaration of subroutine/function under cursor +% +:bufdo /searchstr/ -- use :rewind to recommence search -- multiple file search +% +:bufdo %s/searchstr/&/gic -- say n and then a to stop -- multiple file search better but cheating +% +?http://www.vim.org/ -- (first) search BACKWARDS!!! clever huh! -- How to search for a URL without backslashing +% +/\c\v([^aeiou]&\a){4} -- search for 4 consecutive consonants -- Specify what you are NOT searching for (vowels) +% +/\%>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 occurrence of home in any line [N] +% +^\(.*tongue.*\)\@!.*nose.*$ -- find str but not on lines containing tongue +% +\v^((tongue)@!.)*nose((tongue)@!.)*$ +% +.*nose.*\&^\%(\%(tongue\)\@!.\)*$ +% +:v/tongue/s/nose/&/gic +% +:%s/fred/joe/igc -- general substitute command -- *best-substitution* +% +:%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 +% +:%s/\r/\r/g -- Turn DOS returns ^M into real returns -- Is your Text File jumbled onto one line? use following +% +:%s= *$== -- delete end of line blanks +% +:%s= \+$== -- Same thing +% +:%s#\s*\r\?$## -- Clean both trailing spaces AND DOS returns +% +:%s#\s*\r*$## -- same thing +% +:%s/^\n\{3}// -- delete blocks of 3 empty lines -- deleting 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] +% +%s#><\([^/]\)#>\r<\1#g -- split jumbled up XML file into one tag per line [N] +% +:'a,'bg/fred/s/dick/joe/igc -- VERY USEFUL -- VIM Power Substitute +% +:%s= [^ ]\+$=&&= -- duplicate end column -- duplicating columns +% +:%s= \f\+$=&&= -- same thing +% +:%s= \S\+$=&& -- usually the same +% +:%s#example#& = &#gic -- duplicate entire matched string [N] -- memory +% +:%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] +% +:%s/^.\{-}pdf/new.pdf/ -- delete to 1st occurence of pdf only (non-greedy) -- non-greedy matching \{-} +% +:%s#\<[zy]\?tbl_[a-z_]\+\>#\L&#gc -- lowercase with optional leading characters -- use of optional atom \? +% +:%s/// -- delete possibly multi-line comments -- over possibly many lines +% +:help /\{-} -- help non-greedy +% +:s/fred/a/g -- sub "fred" with contents of register "a" -- substitute using a register +% +:s/fred/asome_texts/g +% +:s/fred/\=@a/g -- better alternative as register not displayed (not *) [C] +% +:%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/goat\|cow/sheep/gc -- ORing (must break pipe) -- ORing +% +:'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] +% +:s/__date__/\=strftime("%c")/ -- insert datestring -- Calling a VIM function +% +:inoremap \zd =strftime("%d%b%y") -- insert date eg 31Jan11 [N] +% +:%s:\(\(\w\+\s\+\)\{2}\)str1:\1str2: -- Working with Columns sub any str1 in col3 +% +:%s:\(\w\+\)\(.*\s\+\)\(\w\+\)$:\3\2\1: -- Swapping first & last column (4 columns) +% +:%s#\\|\\|\\|\<\inner join\>#\r&#g -- format a mysql query +% +:redir @*|sil exec 'g#<\(input\|select\|textarea\|/\=form\)\>#p'|redir END -- filter all form elements into paste register +% +:nmap ,z :redir @*sil exec 'g@<\(input\select\textarea\/\=form\)\>@p'redir END +% +:%s/^\(.\{30\}\)xx/\1yy/ -- substitute string in column 30 [N] +% +:%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 +% +:h /\zs +% +:%s/\(gg\)\@<=\d\+/\=submatch(0)+6/ -- increment only numbers gg\d\d by 6 (another way) +% +:h zero-width +% +:let i=10 | 'a,'bg/Abc/s/yy/\=i/ |let i=i+1 # convert yy to 10,11,12 etc -- rename a string with an incrementing number +% +:let i=10 | 'a,'bg/Abc/s/xx\zsyy\ze/\=i/ |let i=i+1 # convert xxyy to xx11,xx12,xx13 -- as above but more precise +% +:%s/"\([^.]\+\).*\zsxx/\1/ -- find replacement text, put in memory, then 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 +% +:'a,'bs/bucket\(s\)*/bowl\1/gic [N] -- substitute singular or plural +% +:%s,\(all/.*\)\@<=/,_,g -- replace all / with _ AFTER "all/" +% +:s#all/\zs.*#\=substitute(submatch(0), '/', '_', 'g')# -- Same thing +% +:s#all/#&^M#|s#/#_#g|-j! -- Substitute by splitting line, then re-joining +% +:%s/.*/\='cp '.submatch(0).' all/'.substitute(submatch(0),'/','_','g')/ -- Substitute inside substitute +% +:g/gladiolli/# -- display with line numbers (YOU WANT THIS!) -- *best-global* command +% +:g/fred.*joe.*dick/ -- display all lines fred,joe & dick +% +:g/\/ -- display all lines fred but not freddy +% +:g/^\s*$/d -- delete all blank lines +% +:g!/^dd/d -- delete lines not containing string +% +:v/^dd/d -- delete lines not containing string +% +:g/joe/,/fred/d -- not line based (very powerfull) +% +:g/fred/,/joe/j -- Join Lines [N] +% +: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/" -- increment numbers +% +:'a,'bg/\d\+/norm! ^A -- increment numbers +% +:g/fred/y A -- append all lines fred to register a +% +:g/fred/y A | :let @*=@a -- put into paste buffer +% +:let @a=''|g/Barratt/y A |:let @*=@a +% +:'a,'bg/^Error/ . w >> errors.txt -- filter lines to a file (file must already exist) +% +:g/./yank|put|-1s/'/"/g|s/.*/Print '&'/ -- duplicate every line in a file wrap a print '' around each duplicate +% +:g/^MARK$/r tmp.txt | -d -- replace string with contents of a file, -d deletes the "mark" +% +:g//z#.5 -- display with context -- display prettily +% +:g//z#.5|echo "==========" -- display beautifully +% +:g/|/norm 2f|r* -- replace 2nd | with a star -- Combining g// with normal mode commands +% +:nmap :redir @a:g//:redir END:new:put! a -- send output of previous global command to a new window +% +:'a,'bg/fred/s/joe/susan/gic -- can use memory to extend matching -- *Best-Global-combined-with-substitute* (*power-editing*) +% +:/fred/,/joe/s/fred/joe/gic -- non-line based (ultra) +% +:/biz/,/any/g/article/s/wheel/bucket/gic: non-line based [N] +% +:/fred/;/joe/-2,/sid/+3s/sally/alley/gIC -- Find fred before beginning search for joe +% +:g/^/exe ".w ".line(".").".txt" -- create a new file for each line of file eg 1.txt,2.txt,3,txt etc +% +:.g/^/ exe ".!sed 's/N/X/'" | s/I/Q/ [N] -- chain an external command +% +d/fred/ :delete until fred -- Operate until string found [N] +% +y/fred/ :yank until fred +% +c/fred/e :change until fred end +% +. last edit (magic dot) -- Summary of editing repeats [N] +% +:& 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 +% +; last f, t, F or T -- Summary of repeated searches +% +, last f, t, F or T in opposite direction +% +n last / or ? search +% +N last / or ? search in opposite direction +% +* # g* g# -- find word under cursor () (forwards/backwards) +% +% -- match brackets {}[]() +% +. -- repeat last modification +% +@: -- repeat last : command (then @@) +% +matchit.vim -- % now matches tags