Vim Cheetsheet

By   10-05-2017 · 3 minute read · 520 words

Issue was trying to get back to a previous location in a file after jumping to it. Still jump movements but they should help you move a bit more efficiently.

ctrl-o




## Clear line and drop into insert mode

Wow, this one is really cool, I usually just want to nuke a line and go right into insert mode. Instead of dding and then going back up and line and going into insert mode you can just use S

S


## Don’t auto close brackets.

If you are using a plugin such as auto pairs, sometimes you don’t want it to auto-close after you open a brace.

ctrl + v


Navigate to the 3rd word ahead:

3e


Navigate to the beginning of text on the current line:

^


Navigate to the first line:

gg


Jump back to a previous location:

ctrl + o


Jump back to next location:

c


Go to last line in file:

G


Go to matching parenthesis or brackets

%


## Searching

Navigate to a previous search result:

N


Searching backwards for the word “carmen”

?carmen


## Alterations

Re-do changes:

ctrl + r


Change to the end of the line. Not sure if it’s more efficient to use c$over C, regardless it’s two keystrokes. C or c$


Replacing a string good with bad on the current line:

:s/good/bad/


Delete a word with a leading space. See diw for just deleting a “inner” word.

## Text objects

daw


Moving back a word

b


Delete everything inside []

di"


Navigating between splits - I was frustrated for a long time when a help screen would open and I don’t know how to close it.

ctrl-w + j


## Writing to a file when you don’t have write permissions

This one was from peep code. Say you open a file and edit it only realizing that you need to be root to write to it. Instead of saving the file to something like /tmp then copying it back over the original file with higher elevated privileges, you can do all this with one single command.

:w !sudo tee %


The % here expands to the current file name

## Regex search and replace

:%s/2\.2\.8/2.2.9/gc


% - search the current buffer
g - search for all occurrences.
c - ask for confirmation 

## Visual mode

V


Capital V is line wise selection/copy.

v


This is letter by letter visual selection.

## Pasting

P


Paste before current line.

## Change mode

Instead of y to yank or d to delete you can use c to “change” text. This is useful because it deletes the text and puts you into insert mode immediately.

## Moving code words

B

W


## Move to the middle of a line

gm


## Repeating character jumps

I often confused character jumping with . which is repetition but instead using ; after a jump to repeat it. The , repeates the jump in the opposite direction.

## Jumping to middle of screen

M


### Marks

Add marks with ma then jump to it using a

### Editing text

R


### Moving between sentences

Use either ( or ) to move between sentences. ( ) `

vim