Skip to main content

In Vim, following is an outline of the vi(m) functions you can utilise whilst in command mode - yes they are case sensitive

action outcome
a change to insert mode (after cursor)
A change to insert mode (at end of line)
dd delete one line
G go to end of the file
1G go to top of the file
i change to insert mode (before cursor)
J merge next line with this one
p paste deleted or yanked text after cursor
P paste deleted or yanked text before cursor
r replace one character
R overwrite text
x delete one character
yy yank line (copy)
$ The $ key in normal mode will move the cursor to the end of the current line
0 0 (zero) in normal mode will move the cursor to the start of the current line

 

/ search, follow / with text to find
{esc}:wq! write file and quit
{esc}:q! quit without saving
%s/old/new/g substitute; replace "old" with "new" on all lines
:g/pattern/d delete all lines that match the pattern

 

Note: when editing a file and the permission is 444 or r--r--r--, to save the file you need to add an exclamation mark... for example :wq!

 

Vim is a highly customisable text editor with a rich set of commands and features. Here are some of the most commonly used Vim commands to help you get started:

Basic Movement

key outcome
h Move the cursor left
j Move the cursor down
k Move the cursor up
i Move the cursor right

 

Word Movement

key outcome
w Move the cursor to the beginning of the next word
b Move the cursor to the end of the current word
e Move the cursor to the end of the current word
ge Move the cursor to the end of the previous word

 

Line Movement

key outcome
0 (zero) Move to the beginning of the line
$ Move to the end of the line
e Move the cursor to the end of the current word
^ Move to the first non-whitespace character of the line

 

Page Movement

key outcome
ctrl u Scroll up (half page)
ctrl d Scroll down (half page)
ctrl b Page up (full page)
ctrl f Page down (full page)

 

Search and Find

key outcome
/search_term Search forward for search_term
?search_term Search backward for search_term
n Move to the next search result
N Move to the previous search result

 

Insert Mode

key outcome
i Insert text before the cursor
I Insert text at the beginning of the line
a Insert text after the cursor
N Insert 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

 

Visual Mode

key outcome
v Enter visual mode to select text character by character
V Enter visual mode to select whole lines
ctrl v Enter visual block mode to select rectangular blocks of text

 

Copy, Cut, Paste

key outcome
yy Yank (copy) the current line
dd Delete (cut) the current line
p Paste the yanked or deleted text after the cursor
P Paste the text before the cursor

 

Undo and Redo

key outcome
u Undo the last change
ctrl r Redo the last undone change

 

Save and Quit

key outcome
:w Save the current file
:q Quit Vim
:wq Save and quit
:q! Quit without saving

 

Other Commands

key outcome
:e file_path Edit a different file
:sp file_path Split the screen and open a different file
:vsp file_path Vertical split and open a different file
:set number Display line numbers
:set nonumber Hide line numbers

 

These are just a few of the many Vim commands available. Vim has a steep learning curve but offers powerful text-editing capabilities once you become familiar with its commands and concepts. You can access Vim's built-in help system by typing :help to learn more.

Related articles

Andrew Fletcher18 Mar 2024
Resolving CVE-2022-48624 less issue
To resolve the CVE-2022-48624 vulnerability on Ubuntu using Nginx, it's crucial to understand that the issue lies within the "less" package, not Nginx itself. The vulnerability affects "less" before version 606, where close_altfile in filename.c in less omits shell_quote calls for LESSCLOSE,...
Andrew Fletcher12 Mar 2024
How to determine the size of a directory in Terminal
To determine the size of a directory using the terminal, you can use the du (disk usage) command. The syntax for this command can vary slightly depending on the operating system you are using, but a common way to use it is as follows: For Linux and macOSdu -sh /path/to/directoryduDisk...