Vim

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient.

Navigation

h - move cursor left
j - move cursor down
k - move cursor up
l - move cursor right
10j - move cursor 10 lines down


w - jump forward to the next word
W - jump forward to the next word(word can conrtain punctuation)
3w - jump forward 3 words
b - jump backward to the start of the previous word
3b - jump backward 3 words
e - jump forward to the end of a word
E - jump forward to the end of a word(word can contain punctuation)


0 - jump to beggining of the line
^ - jump to first non-whitespace character of the current line
$ - jump to end of the line


:20 - jump to line 20
20G or 20gg - jump to line 20
gg - jump to first line of the file
G - jump to last line of the file


fh - jump to next ‘h’ character. f stands for “find**
Fh - jump to the previous ‘h’ character
% - jump to a matching parenthesis


Ctrl-U - scroll up half a screen of text
Ctrl-D - scroll down half a screen of text
Ctrl-F - scroll a whole screen forward
Ctrl-B - scroll a whole screen backward

Edit

i - insert before current character, then enter insert mode
I - insert atthe first non-whitespace character of the line, then enter insert mode
a - insert after current character(append, then enter insert mode
A - insert at the end of the line, then enter insert mode
o - insert a line below the cursor, then enter insert mode
O - insert a line above the cursor, then enter insert mode

x - delete the current character
ciw - change inner word
cw - change word
r - replace a single character
diw - delete inner word
. - repeat last command

Search and Replace

/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, no confirmation
:%s/old/new/gc - replace all old with new throughout file with confirmation. ‘y’ to replace, ‘n’ to skip, ‘q’ to quit

Copy and Paste

You can yarn(copy) and paste in Visual mode
y - yank. this is copying text
yy - yank(copy) the current line
3yy - yank(copy) 3 lines
p - paste after the cursor
P - paste before the cursor
x - delete(cut) the character after the cursor
X - delete(cut) the character before the cursor
dd - delete(cut) a line
2dd - delete(cut) two lines
dw - delete(cut) a world

Undo and Redu

u - - undo
Crtl-R - redo

Mode Switch

i - enter insert mode
v - enter character-wise visual mode. this is for selecting characters.
V - enter line-wise visual mode
Ctrl+v - enter block-wise visual mode. This is for selecting on “Columns” and “rows”
: - enter command mode
Esc - enter normal mode from insert or replace mode
Esc Esc - enter to normal mode from command or visual mode

Note that the mode will be display on the bottom left of your vim window.

Open and Close Files

:w - save the current file
:q - close the current file
:wq - save the current file then close
ZZ - same as :wq, save the current file then close
:x - same as :wq, save the current file then close
:q! -close the current file without saving

Tabs

tabnew - open a file in new tab
gt or :tabn - new tab
gT or :tabp - previous tab
3gt - go to tab number 3

Setting Options

To enable an option for an individual file, use :set to set the option. To permanently enable an option, write the options in ~/.vimrc file without the colon(:) before the set command.

The most useful commons are setting line numbers. You can use set number to enable line number. To disable, use set nonumber. There is also option to use relative number.
set number or set nu - show line number
set relativenumber or set rnu - use relative number
set nonu - hide line number
set norelativenumber or setnornu - disable relative number

Common options can be found in Top 50 Vim Configuration Options

My ~/.vimrc file looks like this:

1
2
3
4
set nu
syntax on
set cursorline
colorscheme slate

Vim Tips and Tricks

Use with VS Code

VS Code provides a Vim extension that allows you to use vim within VS Code.

You can add the following settings. This will allow vim to use system clipboard and use a better color for highlighting search.

1
2
3
"vim.usesystemclipboard": true,
"vim.searchhighlightcolor": "#ffc438",
"vim.searchhighlighttextcolor": "#000000"

View Command

The view command starts the vi full-screen editor in read-only mode. The read-only mode is only advisory to prevent accidental changes to the file.

Comment Multiple Lines

Fist move the cursor to the first character to comment out.

Then enter block visual mode using Ctrl + V.

Move cursor to the last line you want to comment out.

Type Shift + I to edit the first character.

Enter # or // depending on the programming language. Type x to uncomment.

Type Esc to exit visual mode.

Reference and resources