Vim Cheatsheet

Vim (short for Vi Improved) is a highly customizable, free and open-source text editor that was originally developed in the 1980s as a successor to the Unix text editor called vi. Vim is designed to be fast, powerful, and efficient, and is often used by programmers and system administrators for editing code and configuration files.

Vim is known for its powerful editing capabilities, which are accessible through a variety of key combinations and commands. These include features like text manipulation, search and replace, macros, and syntax highlighting. Vim also offers support for various programming languages, as well as plugins and extensions that can be used to extend its functionality.

One of the unique features of Vim is its modal editing system, which allows the user to switch between different modes for different editing tasks. For example, in normal mode, the user can navigate through the text using various key combinations, while in insert mode, the user can insert and edit text as they would in a standard text editor.

Although Vim can have a steep learning curve, its flexibility and power make it a popular choice for many programmers and power users.

Essentials

Cursor movement

h j k l         # Arrow keys

w               # Next word
W               # Next word (space separated)
e               # Next end of word

b               # Previous word
B               # Previous word (space separated)
ge              # Previous end of word

0               # Start of line
$               # End of line
^               # First non-blank character

Ctrl+d          # Move down half a page
Ctrl+u          # Move up half a page

}               # Go forward by paragraph (the next blank line)
{               # Go backward by paragraph (the next blank line)

gg              # Go to the top of the page
G               # Go the bottom of the page

:[num]          # Go to that line in the document

Ctrl+e          # Scroll down one line
Ctrl+y          # Scroll up one line

Editing text

Inserting text:

i               # Start insert mode at cursor
a               # Start insert mode after cursor
I               # Start insert mode at the beginning of the line
A               # Start insert mode at the end of the line

o               # Add blank line below current line
O               # Add blank line above current line

u               # Undo
Ctrl+r          # Redo

>               # Indent one level
<               # Unindent one level

J               # Join line below to the current one
r [char]        # Replace a single character with the specified char (does not use Insert mode)

Esc             # Exit insert mode

Utilizing the clipboard:

y               # Yank (copy) from the cursor to the movement location
yy              # Yank (copy) a line

d               # Deletes from the cursor to the movement location
dd              # Delete line
c               # Deletes from the cursor to the movement location, then starts insert mode
cc              # Delete line, then starts insert mode

p               # Paste after cursor
P               # Paste before cursor

x               # Delete (cut) current character
X               # Delete (cut) previous character

Note: Both d and c copy the deleted text to the clipboard.

Exiting

:w              # Write (save) the file, but don’t quit
:wq             # Write (save) and quit
:q              # Quit (fails if anything has changed)
:q!             # Quit and throw away changes

Search and replace

Search and replace patterns:

/pattern        # Search for pattern
?pattern        # Search backward for pattern

n               # Repeat search in same direction
N               # Repeat search in opposite direction

:%s/old/new/g   # Replace all old with new throughout file (gn is better though)
:%s/old/new/gc  # Replace all old with new throughout file with confirmations

Search characters:

f [char]                # Move forward to the given char
F [char]                # Move backward to the given char

t [char]                # Move forward to before the given char
T [char]                # Move backward to before the given char

;                       # Repeat search forwards
,                       # Repeat search backwards

Multiple editors

:e <filename>           # Edit a file

Working with multiple tabs:

:tabe                   # Make a new tab
gt                      # Go to the next tab
gT                      # Go to the previous tab

Working with split windows:

Ctrl+wv                 # Veritcally split windows
:vsp <path-to-file>     # Veritcally split windows, new window opens the specified file

Ctrl+ws                 # Horizontally split windows
:sp <path-to-file>      # Horizontally split windows, new window opens the specified file

Ctrl+ww                 # Switch between windows
crtl+wq                 # Quit a window

Vim config

Set the number of tab spaces:

:set tabstop=<number>

Note: Put this setting in .vimrc in your home directory to make the persistent.