Vim
Vim (Vi IMproved) is a highly configurable, efficient, and powerful text editor, widely used by developers and system administrators. It is an enhanced version of the Unix vi
editor, offering extensive features for text editing.
Modes
Vim operates in multiple modes, each serving a specific purpose:
- Normal Mode: Default mode for navigation and command execution.
- Insert Mode: For inserting text. Enter by pressing
i
,a
,o
, etc. - Visual Mode: For selecting text. Enter by pressing
v
(character-wise),V
(line-wise), orCtrl+v
(block-wise). - Command-Line Mode: For executing commands. Enter by pressing
:
in Normal mode. - Replace Mode: For replacing existing text. Enter by pressing
R
. - Ex Mode: For executing multiple commands. Enter by pressing
Q
.
Basic Commands
Opening and Saving Files
vim filename # Open or create a file
:w # Save changes
:w filename # Save as a new file
:q # Quit
:q! # Quit without saving
:wq or :x # Save and quit
Inserting Text
i # Insert before cursor
I # Insert at beginning of line
a # Append after cursor
A # Append at end of line
o # Open new line below
O # Open new line above
Navigating Within a File
h # Move left
j # Move down
k # Move up
l # Move right
0 # Move to beginning of line
^ # Move to first non-blank character of line
$ # Move to end of line
w # Move to beginning of next word
b # Move to beginning of previous word
e # Move to end of word
gg # Go to beginning of file
G # Go to end of file
:n # Go to line number n
The navigation commands can be prefixed with a number.
For example, typing 5j
would move us 5 lines down.
More navigations:
f[char] # Jump to the next occurence of a character
F[char] # Jump to the previous occurence of a character
t[char] # Jump to the position before the next occurence of a character
daw # Delete a word
( or ) # Move by sentences
{ or } # Move by paragraphs
Ctrl + D or Ctrl + U # Move by half-a-page
Ctrl + F or Ctrl + B # Move by a full page
Editing Text
x # Delete character under cursor
dd # Delete current line
dw # Delete word from cursor
d$ # Delete to end of line
u # Undo last change
Ctrl+r # Redo undone change
yy # Yank (copy) current line
p # Paste after cursor
P # Paste before cursor
r + [char] # Replace current character with new one
s # Delete current char and enter into insert mode
yi{ # Copy everything inside the '{}' (works with {}, (), [])
ya{ # Copy everything inside the '{}' including the brackets
di[{, (, [] # Delete everything inside brackets
Visual Mode Operations
v # Start visual mode (character-wise)
V # Start visual mode (line-wise)
Ctrl+v # Start visual block mode
y # Yank selected text
d # Delete selected text
> # Indent selected text
< # Unindent selected text
Searching and Replacing
/pattern # Search forward for 'pattern'
?pattern # Search backward for 'pattern'
n # Repeat last search in same direction
N # Repeat last search in opposite direction
:%s/old/new/g # Replace all occurrences of 'old' with 'new' in file
:%s/old/new/gc # Replace with confirmation
Working with Multiple Files
:split filename # Split window and open 'filename'
:vsplit filename # Vertical split with 'filename'
:tabnew filename # Open 'filename' in new tab
gt # Go to next tab
gT # Go to previous tab
Miscellaneous Commands
:set number # Show line numbers
:set nonumber # Hide line numbers
:set syntax=on # Enable syntax highlighting
:set syntax=off # Disable syntax highlighting
:help keyword # Open help for 'keyword'
Custom Key Mappings
Vim allows users to create custom key mappings to enhance productivity:
:map <F5> :w<CR> # Map F5 to save file
:imap jj <Esc> # Map 'jj' in insert mode to escape to normal mode
These mappings can be added to the .vimrc
file for persistence.
Vim in VS Code
We can use the Vim (vscodevim) extension in VS Code to use the Vim keybindings.
Thus the same modes, navigations and vim motions will work inside VS Code.
We can include custom key mappings like remapping jj
to Esc
like this:
Ctrl + Shift + P
→ Preferences: Open User Settings (JSON) (openingsettings.json
)- Enter the code block below:
"vim.insertModeKeyBindings": [
{
"before": ["j", "j"],
"after": ["<Esc>"]
}
]
Extras
- To set the numbering relative to the current number, run
:set relativenumber
. This allows us to move forward with[number]
and move backward with[number]k
. - If you are at an opening bracket, to move to the closing bracket, press
%
. -
To make they yank(
y
) and paste(p
) commands work with the system keyboard, add"vim.useSystemClipboard": true
tosettings.json
.