Table of Contents
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/\<foo\>/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 (<tt>'</tt>) or backtick <tt>()</tt> 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 <tt>a</tt>) will only work if that mark exists in the current buffer. Using an uppercase letter (for example <tt>A</tt>) 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.
<code>
{| class="wikitable"
!Command !! Description
|-
| <tt>ma</tt> || set mark '''a''' at current cursor location
|-
| <tt>'a</tt> || jump to line of mark '''a''' (first non-blank character in line)
|-
| <tt>a</tt> || jump to position (line and column) of mark 'a'
| <tt>d'a</tt> | |
<tt>da</tt> || delete from current cursor position to position of mark '''a'''
|-
| <tt>c'a</tt> || change text from current line to line of mark '''a'''
|-
| <tt>ya</tt> |
|
| <tt>:marks</tt> | |
| <tt>:marks aB</tt> | |
</code>
Commands like <tt>d'a</tt> operate “linewise” and include the start and end lines.<br>
Commands like <tt>da</tt> operate "characterwise" and include the start but not the end character.
It is possible to navigate between lowercase marks:
{| class="wikitable"
!Command !! Description
|-
| <tt>]'</tt> || jump to next line with a lowercase mark
|-
| <tt>['</tt> || jump to previous line with a lowercase mark
|-
| <tt>]</tt> || jump to next lowercase mark
Special marks
Vim has some special marks which it sets automatically. Here are some of the most useful:
{| class=“wikitable” !Command !! Description
<tt>.</tt> || jump to position where last change occurred in current buffer
|-
| <tt>“</tt> |
|
<tt>0</tt> || jump to position in last file edited (when exited Vim)
|-
| <tt>1</tt> |
|
| <tt>''</tt> | |
| <tt>``</tt> | |
<tt>[</tt> or <tt>]</tt> |
|
<tt><</tt> or <tt>></tt> |
|
See the full list at '[ 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...