February 9, 2019 · Don't Forget linux mac

(neo)vim shortcuts & cheatsheet

Vim is an IMproved version of Vi, useful for editing text files.

Startup and Exit:

:q[uit] # Quit Vim. [!] to force without writing

:wq {file} # Write to {file} and exit. [!] to exit always

:e[dit] {file} # Edit {file}. This is useful to re-edit the current file, when it has been changed outside of Vim. [!] to discard any changes to the current buffer. This is useful if you want to start all over again.

:!mv {src} {dest}, and then running :e {dest} # move {src} to {dest} and open the new buffer

Inserting and Deleting:

a # Append text after the cursor [count] times. [A] for end of line

i # Insert text before the cursor [count] times. [I] to do it before the first non-blank

o # Begin a new line below the cursor and insert text, repeat [count] times. [O] for above the cursor

:r[ead] [name] # Insert the file [name] below the cursor.

:r[ead] !{cmd} # Execute {cmd} and insert its standard output below the cursor

x # Delete [count] characters under and after the cursor. [X] to do it before the cursor

dd # Delete [count] lines

d+$ or D # Delete the characters under the cursor until the end of the line

{Visual}x or {Visual}d # Delete the highlighted text. [X] or [D] to delete the highlighted lines.

r{char} # replace the character under the cursor with {char}.

R # Enter Insert mode, replacing characters rather than inserting

~ # Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters

{Visual}~ # Switch case of highlighted text

:u[ndo] # Undo one change. Repeat for multiple

ctrl+r # Redo [count] changes which were undone

. # Repeat last change, with count replaced with [count]

:kJ # move current line, removing spaces before it, to the previous line

ctrl+V c <type> esc esc # selects column, types replacement text, apply it

ctrl+V <choose lines> :s/^/# /g # selects linex, substitute beginning of the line with #

:ciw # Quickly change word or line

Copying and Moving Text:

:reg[isters] or :di[splay] # Display the contents of all numbered and named registers (ie: where deletes and yanks are stored)

:reg[isters] {arg} # Display the contents of the numbered and named registers that are mentioned in {arg}

["x]yy # Yank [count] lines [into register x]

["x]p # Put the text [from register x] after the cursor [count] times

["x]P # Put the text [from register x] before the cursor [count] times

Moving Around:

0 # To the first character of the line (exclusive)

$ # To the end of the line and [count - 1] lines downward

:{num} # Jump to line {num}

e or w # Forward to the end of the word [count]

b # Backtrack to the previous word

h j k l # directional movement. left down up right, respectively

ctrl+D # page down

ctrl+BB # page up

} or { # Jump down or up.

Opening a terminal:

:terminal # To open a terminal on a new buffer (i to interact)

ctrl+\ ctrl+N # To end interaction

Buffers:

:e {file} # edit another {file}. An empty buffer can be created by entering :new or :vnew

:badd {file} # add a new buffer for a file without opening it

:split {file} # split window and load another {file}

ctrl+w {up arrow} # move cursor up a window

ctrl+w ctrl+w # move cursor to another window (cycle)

ctrl+w ctrl+r # swap the two parts of a split window

ctrl+w {_} # maximize current window

ctrl+w {=} # make all equal size

[c] ctrl+w {+/-} # modify window height by [c] lines

[c] ctrl+w {>/<} # modify window width by [c] lines

:vsplit file # vertical split

:hide # close current window

:only # keep only this window open

:ls # show current buffers

:b [c] # open buffer #[c] in this window

:b <minimum amount of unambiguous characters> tab # jumps to the document/buffer you want

:bd[elete] {num} # delete buffer {num}

:windo diffthis # with two split windows containing buffers, this is how to compare them

:windo diffoff # turn off diff between split buffers

:set list # whitespaces, tabs and eol characters are shown

For more references, check this or this or this out.