Table of Contents
Open Table of Contents
Introduction
Vim is a text editor that is included in most Linux distributions. It is a very powerful tool that can be used to edit text files, edit code, and even manage files on your server. Vim is a modal editor, which means that it has different modes for writing, navigating, and selecting text. Vim is a very powerful tool that can be used to edit text files, edit code, and even manage files on your server.
Modes
Vim has different modes for writing, navigating, and selecting text. The most common modes are:
- Normal mode: This is the default mode. You can use it to navigate through the file, delete text, copy text, and paste text.
- Insert mode: This mode is used to insert text into the file. You can enter insert mode by pressing
i
or a
.
- Visual mode: This mode is used to select text. You can enter visual mode by pressing
v
.
- Command mode: This mode is used to run commands. You can enter command mode by pressing
:
.
- Replace mode: This mode is used to replace text. You can enter replace mode by pressing
R
.
Commands
Opening and Saving Files
Command | Description |
---|
vim file | Open a file in Vim |
:w | Save a file |
:wq | Save a file and exit Vim |
:q | Exit Vim |
:q! | Exit Vim without saving changes |
Navigating Files
Command | Description |
---|
h | Move cursor left |
j | Move cursor down |
k | Move cursor up |
l | Move cursor right |
w | Move cursor to the beginning of the next word |
b | Move cursor to the beginning of the previous word |
e | Move cursor to the end of the next word |
0 | Move cursor to the beginning of the line |
$ | Move cursor to the end of the line |
gg | Move cursor to the beginning of the file |
G | Move cursor to the end of the file |
:n | Move cursor to line number n |
Ctrl + f | Move cursor forward one page |
Ctrl + b | Move cursor backward one page |
Ctrl + d | Move cursor forward half a page |
Ctrl + u | Move cursor backward half a page |
Editing Files
Command | Description |
---|
i | Enter insert mode |
a | Enter insert mode after the cursor |
o | Enter insert mode on a new line below the cursor |
O | Enter insert mode on a new line above the cursor |
r | Replace character under cursor |
R | Enter replace mode |
x | Delete character under cursor |
dd | Delete line |
yy | Copy line |
p | Paste line below cursor |
P | Paste line above cursor |
u | Undo last change |
Ctrl + r | Redo last change |
. | Repeat last change |
J | Join line below to the current one |
Searching Files
Command | Description |
---|
/pattern | Search forward for pattern |
?pattern | Search backward for pattern |
n | Repeat search in same direction |
N | Repeat search in opposite direction |
Selecting Text
Command | Description |
---|
v | Enter visual mode |
V | Enter visual line mode |
Ctrl + v | Enter visual block mode |
aw | Select a word |
ab | Select a block with parentheses |
aB | Select a block with braces |
ib | Select inner block with parentheses |
iB | Select inner block with braces |
Esc | Exit visual mode |
Editing Text
Command | Description |
---|
~ | Switch case |
> | Indent line |
< | Unindent line |
= | Auto indent |
gq | Format text |
J | Join line below to the current one |
Ctrl + a | Increment number |
Ctrl + x | Decrement number |
Copying and Pasting Text
Command | Description |
---|
yy | Copy line |
p | Paste line below cursor |
P | Paste line above cursor |
yw | Copy word |
yiw | Copy inner word |
y$ | Copy to end of line |
y0 | Copy to beginning of line |
y^ | Copy to first non-blank character of line |
ygg | Copy to first line of file |
yG | Copy to last line of file |
y/pattern | Copy to next occurrence of pattern |
y?pattern | Copy to previous occurrence of pattern |
y/pattern/ | Copy to next occurrence of pattern on the same line |
y?pattern? | Copy to previous occurrence of pattern on the same line |
Deleting Text
Command | Description |
---|
x | Delete character under cursor |
dw | Delete word |
dd | Delete line |
d$ | Delete to end of line |
d0 | Delete to beginning of line |
d^ | Delete to first non-blank character of line |
dgg | Delete to first line of file |
dG | Delete to last line of file |
d/pattern | Delete to next occurrence of pattern |
d?pattern | Delete to previous occurrence of pattern |
d/pattern/ | Delete to next occurrence of pattern on the same line |
d?pattern? | Delete to previous occurrence of pattern on the same line |
Exiting Vim (no joking)
Command | Description |
---|
:w | Save a file |
:wq | Save a file and exit Vim |
:q | Exit Vim |
:q! | Exit Vim without saving changes |
References
Happy learning!