Skip to content

Instantly share code, notes, and snippets.

@wakproductions
Last active March 9, 2017 21:04
Show Gist options
  • Save wakproductions/9037ff18ae4469d497091b00045d0634 to your computer and use it in GitHub Desktop.
Save wakproductions/9037ff18ae4469d497091b00045d0634 to your computer and use it in GitHub Desktop.
VIM Cheat Sheet

Show line numbers: :set number

Reading a very large file

SO: How to edit multi gigabyte text files in vim

Vim will hang if you try to open up a multi-gigabyte file because it is trying to load the whole thing into a buffer. Ctrl-C will stop file load. If the file is small enough you may have been lucky to have loaded all the contents and just killed any post load steps. Verify that the whole file has been loaded when using this tip.

Vim can handle large files pretty well. I just edited a 3.4GB file, deleting lines, etc. Three things to keep in mind:

Press Ctrl-C: Vim tries to read in the whole file initially, to do things like syntax highlighting and number of lines in file, etc. Ctrl-C will cancel this enumeration (and the syntax highlighting), and it will only load what's needed to display on your screen. Readonly: Vim will likely start read-only when the file is too big for it to make a . file copy to perform the edits on. I had to w! to save the file, and that's when it took the most time. Go to line: Typing :115355 will take you directly to line 115355, which is much faster going in those large files. Vim seems to start scanning from the beginning every time it loads a buffer of lines, and holding down Ctrl-F to scan through the file seems to get really slow near the end of it. Note - If your Vim instance is in readonly because you hit Ctrl-C, it is possible that Vim did not load the entire file into the buffer. If that happens, saving it will only save what is in the buffer, not the entire file. You might quickly check with a G to skip to the end to make sure all the lines in your file are there.

Go to line number

If you're already in vi, you can use the goto command. To do this, press Esc , type the line number, and then press Shift-g . If you press Esc and then Shift-g without specifying a line number, it will take you to the last line in the file.

e 
Move to the end of a word.
w 
Move forward to the beginning of a word.
3w 
Move forward three words.
W 
Move forward a WORD (any non-whitespace characters).
b 
Move backward to the beginning of a word.
3b 
Move backward three words.
$ 
Move to the end of the line.
0 
Move to the beginning of the line.
^ 
Move to the first non-blank character of the line.
) 
Jump forward one sentence.
( 
Jump backward one sentence.
} 
Jump forward one paragraph.
{ 
Jump backward one paragraph.:
j

Jump forward one line.
k 
Jump backward one line.
H 
Jump to the top of the screen.
M 
Jump to the middle of the screen.
L 
Jump to the bottom of the screen.
10<PageUp> or 10<CTRL-B>
Move 10 pages up.
5<PageDown> or 5<CTRL-F>
Move 5 pages down.
G 
Jump to end of file.
1G 
Jump to beginning of file (same as gg).
50G 
Jump to line 50.
mx 
Set mark x at the current cursor position.
'x 
Jump to the beginning of the line of mark x.
`x 
Jump to the cursor position of mark x.
''
Return to the line where the cursor was before the latest jump.
(Two single quotes.)
``
Return to the cursor position before the latest jump (undo the jump).
(Two back ticks. This is above the Tab key on some keyboards.)
'. 
Jump to the last-changed line.
 % 
Jump to corresponding item, e.g. from an open brace to its matching closing brace. See Moving to matching braces for more.

Search

n normal mode you can search forwards by pressing / (or ) then typing your search pattern. Press Esc to cancel or press Enter to perform the search. Then press n to search forwards for the next occurrence, or N to search backwards. Type ggn to jump to the first match, or GN to jump to the last.

Note: ggn skips first match if it is at row 1 column 1. Type Gn to jump to the real first match. For this, 'wrapscan' must be on (default).

Search backwards by pressing ? then typing your search pattern. Pressing n searches in the same direction (backwards), while N searches in the opposite direction (forwards).

Search and Replace

:s/foo/bar/g	Change each 'foo' to 'bar' in the current line.
:%s/foo/bar/g	Change each 'foo' to 'bar' in all the 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 (see Note below).
:'<,'>s/foo/bar/g	When compiled with +visual, change each 'foo' to 'bar' for all lines within a visual selection. Vim automatically appends the visual selection range ('<,'>) for any ex command when you select an area and enter :. Also, see Note below.
:.,$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).
:g/^baz/s/foo/bar/g	Change each 'foo' to 'bar' in each line starting with 'baz'.

Delete all lines in a file

Type gg to move the cursor to the first line of the file, if it is not already there. Type dG to delete all the lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment