======Vim shortcuts====== =====Search & Replace===== == Basic search & replace == :%s/foo/bar/g Find each occurrence of 'foo', and replace it with 'bar'. :%s/foo/bar/gc Change each 'foo' to 'bar', but ask for confirmation first. :%s/\/bar/gc Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. :%s/foo/bar/gci Change each 'foo' (case insensitive) to 'bar'; ask for confirmation. This may be wanted after using :set noignorecase to make searches case sensitive (the default). :%s/foo/bar/gcI Change each 'foo' (case sensitive) to 'bar'; ask for confirmation. This may be wanted after using :set ignorecase to make searches case insensitive. == Search range == :s/foo/bar/g Change each 'foo' to 'bar' in the current line. :%s/foo/bar/g Change each 'foo' to 'bar' in all lines. :5,12s/foo/bar/g Change each 'foo' to 'bar' for all lines from line 5 to line 12 inclusive. :'a,'bs/foo/bar/g Change each 'foo' to 'bar' for all lines from mark a to mark b inclusive. :.,$s/foo/bar/g Change each 'foo' to 'bar' for all lines from the current line (.) to the last line ($) inclusive. :.,+2s/foo/bar/g Change each 'foo' to 'bar' for the current line (.) and the two next lines (+2). :%s/foo/bar/g Equivalent to :1,$s/foo/bar/g (change all lines). :g/^baz/s/foo/bar/g Change each 'foo' to 'bar' in each line starting with 'baz'. == others == When searching: ., *, \, [, ], ^, and $ are metacharacters. +, ?, |, {, }, (, and ) must be escaped to use their special function. \/ is / (use backslash + forward slash to search for forward slash) \t is tab, \s is whitespace \n is newline, \r is CR (carriage return = Ctrl-M = ^M) \{#\} is used for repetition. /foo.\{2\} will match foo and the two following characters. The \ is not required on the closing } so /foo.\{2} will do the same thing. \(foo\) makes a backreference to foo. Parenthesis without escapes are literally matched. Here the \ is required for the closing \). When replacing: \r is newline, \n is a null byte (0x00). \& is ampersand (& is the text that matches the search pattern). \1 inserts the text of the first backreference. \2 inserts the second backreference, and so on. =====Windows===== ==open windows== :split filename :vsplit filename ==Navigation== ctrl W = equal size :res -N ctrl W - reduce height by N :res +N ctrl W + increase height by N ctrl W _ ctrl W < decrease window width by N (default 1) ctrl W > increase window width by N (default 1) ===== Tabs ===== ==Open tab== :tabedit filename ==Navigation== :tabn(ext) gt #goto next tab. :tabp(rev) gT #goto previous tab. ===== General Navigation===== ==Using marks== To jump to a mark enter an apostrophe (') or backtick (`) followed by a letter. Using an apostrophe jumps to the beginning of the line holding the mark, while a backtick jumps to the line and column of the mark. Using a lowercase letter (for example `a) will only work if that mark exists in the current buffer. Using an uppercase letter (for example `A) will jump to the file and the position holding the mark (you do not need to open the file prior to jumping to the mark). *Each file can have mark '''a''' – use a lowercase mark to jump within a file. *There is only one file mark '''A''' – use an uppercase mark to jump between files. {| class="wikitable" !Command !! Description |- | ma || set mark '''a''' at current cursor location |- | 'a || jump to line of mark '''a''' (first non-blank character in line) |- | `a || jump to position (line and column) of mark '''a''' |- | d'a || delete from current line to line of mark '''a''' |- | d`a || delete from current cursor position to position of mark '''a''' |- | c'a || change text from current line to line of mark '''a''' |- | y`a || yank text to unnamed buffer from cursor to position of mark '''a''' |- | :marks || list all the current marks |- | :marks aB || list marks '''a''', '''B''' |} Commands like d'a operate "linewise" and include the start and end lines.
Commands like d`a operate "characterwise" and include the start but not the end character. It is possible to navigate between lowercase marks: {| class="wikitable" !Command !! Description |- | ]' || jump to next line with a lowercase mark |- | [' || jump to previous line with a lowercase mark |- | ]` || jump to next lowercase mark |- | [` || jump to previous lowercase mark |} The above commands take a count. For example, 5]` jumps to the fifth mark after the cursor. ==Special marks== Vim has some special marks which it sets automatically. Here are some of the most useful: {| class="wikitable" !Command !! Description |- | `. || jump to position where last change occurred in current buffer |- | `" || jump to position where last exited current buffer |- | `0 || jump to position in last file edited (when exited Vim) |- | `1 || like `0 but the previous file (also `2 etc) |- | '' || jump back (to line in current buffer where jumped from) |- | `` || jump back (to position in current buffer where jumped from) |- | `[ or `] || jump to beginning/end of previously changed or yanked text |- |- | `< or `> || jump to beginning/end of last visual selection |- |} See the full list at {{help|'[}} and following. =====Paths===== ==Buffer Path== %:p full path to buffer. %:p:h parent directory of buffer. In Action: show content of parent dir of current buffer. :!ls %:p:h ===== Tab(edit) ===== * convert tabs to spaces (useful for python programming) :set tabstop=2 shiftwidth=2 expandtab or :set expandtab :retab ... works for current buffer...