Navigation Commands for vim
| Command |
Description |
h |
Move cursor left |
j |
Move cursor down |
k |
Move cursor up |
l |
Move cursor right |
w |
Move to the beginning of the next word |
b |
Move to the beginning of the previous word |
e |
Move to the end of the current word |
0 |
Move to the beginning of the line |
$ |
Move to the end of the line |
gg |
Move to the beginning of the file |
G |
Move to the end of the file |
:n |
Move to line n |
Editing Commands
| Command |
Description |
i |
Insert text before the cursor |
I |
Insert text at the beginning of the line |
a |
Append text after the cursor |
A |
Append text at the end of the line |
o |
Open a new line below the current line |
O |
Open a new line above the current line |
x |
Delete the character under the cursor |
X |
Delete the character before the cursor |
dd |
Delete the current line |
D |
Delete from the cursor to the end of the line |
r |
Replace the character under the cursor |
R |
Enter replace mode |
u |
Undo the last change |
Ctrl + r |
Redo the last undo |
. |
Repeat the last command |
Saving and Exiting
| Command |
Description |
:w |
Save the file |
:wq |
Save and exit |
:q |
Exit Vim |
:q! |
Exit without saving |
:x |
Save and exit (same as :wq) |
:w filename |
Save as filename |
:saveas filename |
Save as filename |
:w !sudo tee % |
Save a file that requires sudo permissions |
Search and Replace
| Command |
Description |
/pattern |
Search for pattern |
?pattern |
Search backward for pattern |
n |
Go to the next occurrence of the search pattern |
N |
Go to the previous occurrence of the search pattern |
:%s/old/new/g |
Replace all occurrences of old with new in the file |
:%s/old/new/gc |
Replace all occurrences of old with new in the file with confirmation |
:n1,n2s/old/new/g |
Replace all occurrences of old with new between lines n1 and n2 |
Visual Mode
| Command |
Description |
v |
Enter visual mode |
V |
Enter visual line mode |
Ctrl + v |
Enter visual block mode |
y |
Yank (copy) the selected text |
d |
Delete the selected text |
c |
Change the selected text |
> |
Indent the selected text to the right |
< |
Indent the selected text to the left |
= |
Auto-indent the selected text |
Working with Multiple Files
| Command |
Description |
:e filename |
Open filename for editing |
:n |
Go to the next file |
:N |
Go to the previous file |
:ls |
List all open buffers |
:b n |
Switch to buffer n |
:bn |
Go to the next buffer |
:bp |
Go to the previous buffer |
:bd |
Delete the current buffer |
:sp filename |
Split the window horizontally and open filename |
:vsp filename |
Split the window vertically and open filename |
Ctrl + w + h |
Move to the window on the left |
Ctrl + w + j |
Move to the window below |
Ctrl + w + k |
Move to the window above |
Ctrl + w + l |
Move to the window on the right |
Ctrl + w + = |
Make all windows equal size |
Ctrl + w + + |
Increase window height |
Ctrl + w + - |
Decrease window height |
Ctrl + w + > |
Increase window width |
Ctrl + w + < |
Decrease window width |
Advanced Commands
| Command |
Description |
:r filename |
Insert the contents of filename |
:r !command |
Insert the output of command |
:!command |
Execute command in the shell |
:set number |
Show line numbers |
:set nonumber |
Hide line numbers |
:set hlsearch |
Highlight search results |
:set nohlsearch |
Disable highlighting of search results |
:set incsearch |
Show partial matches for a search phrase |
:set noincsearch |
Disable showing partial matches for a search phrase |
:set ignorecase |
Ignore case in search patterns |
:set noignorecase |
Consider case in search patterns |
:set wrap |
Enable line wrapping |
:set nowrap |
Disable line wrapping |
:set paste |
Enable paste mode |
:set nopaste |
Disable paste mode |
That’s it for Chapter 12, Text Editing in Linux ! You’ve now learned the basics of nano and vim. Nano is easier to use for beginners, while vim one of the most powerful text editors in the Linux world. In the next chapter, we’ll dive into customizing the shell further, including advanced scripting and configuration. Until then, practice using nano or vim to become more comfortable with its unique workflow.
Pages: 1 2 3