Andrew Fletcher published: 13 December 2019 (updated) 25 October 2023 3 minutes read
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 Fletcher
•
16 Jan 2025
get IP address from terminal OSX
When troubleshooting network issues or configuring devices, knowing your IP address can be essential. Whether you're connected via Wi-Fi, Ethernet, or tethering through a mobile provider, macOS offers powerful built-in tools to quickly identify your IP address. Here's a practical guide tailored to...
Andrew Fletcher
•
07 Jan 2025
Managing DDEV environment troubleshooting and setting up multiple Drupal projects
DDEV has become a popular tool for local web development, offering a streamlined approach to managing Docker-based environments. However, setting up and managing DDEV projects, particularly with the latest versions of Docker Desktop, can present challenges. This article guides you through resolving...
Andrew Fletcher
•
28 Dec 2024
Optimising file transfers by improving efficiency from cp to rsync
Transferring files between development and production environments is a critical task in the deployment process. However, I continue to come across multiple approaches that scale from awesome automation using pipelines to the basic of direct command line entry. Where the basic approaches rely on...