Vi is a powerful text editor that is widely used in the Unix and Linux environments. It is known for its efficiency, versatility, and extensibility. Vi was originally developed in the 1970s as part of the Unix operating system, and has since become a standard tool for editing text files in a variety of contexts.
Vi can be intimidating for beginners, but with practice, it can become a powerful tool for editing text. To help you get started, we’ve put together a cheat sheet that covers the most commonly used commands in vi. The cheat sheet is organized into different themes, such as navigation, editing, searching, and advanced features. Each command is listed along with a brief description of what it does.
Here are a few tips to get you started with vi:
Vi has two modes: command mode and insert mode. Command mode is used for navigating and editing text, while insert mode is used for typing text. To switch between modes, press the ‘esc’ key to enter command mode, and press ‘i’ to enter insert mode.
Vi uses keyboard shortcuts to perform commands. For example, to move the cursor to the right, press the ‘l’ key. To delete the character under the cursor, press the ‘x’ key.
Vi has a range of advanced features, such as the ability to search for and replace text, and the ability to customize the editor using configuration files. These features can be accessed using commands that begin with a colon (‘:’).
Cheat Sheet
Getting Started
Command | Description |
vi | Open a new file in vi |
vi <filename> | Open an existing file in vi |
i | Enter Insert mode |
esc | Return to Command mode |
:q | Quit vi |
:w | Write changes to the file |
:wq | Write changes to the file and quit |
Navigation
Command | Description |
h | Move left one character |
j | Move down one line |
k | Move up one line |
l | Move right one character |
w | Move forward one word |
b | Move back one 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 |
Editing
Command | Description |
x | Delete the character under the cursor |
dd | Delete the current line |
yy | Yank (copy) the current line |
p | Paste the contents of the clipboard |
u | Undo the last change |
. | Repeat the last change |
r | Replace the current character |
cw | Change the current word |
C | Change the rest of the line |
J | Join the current line with the next line |
:s/old/new/g | Replace all occurrences of ‘old’ with ‘new’ in the current line |
:s/old/new/gc | Replace all occurrences of ‘old’ with ‘new’ in the current line with confirmation |
Searching
Command | Description |
/pattern | Search forward for ‘pattern’ |
?pattern | Search backward for ‘pattern’ |
n | Move to the next match |
N | Move to the previous match |
Advanced
Command | Description |
:set nu | Display line numbers |
:set nonu | Hide line numbers |
:set tabstop=4 | Set the tab size to 4 spaces |
:set shiftwidth=4 | Set the indent size to 4 spaces |
:set expandtab | Use spaces instead of tabs for indentation |
:set noexpandtab | Use tabs instead of spaces for indentation |
:set hlsearch | Highlight search results |
:set nohlsearch | Disable highlighting of search results |
Exiting
Command | Description |
:w | Save changes |
:wq or 😡 | Save changes and exit |
:q! | Discard changes and exit |
Visual Mode
Command | Description |
v | Enter visual mode |
V | Enter visual line mode |
Ctrl+v | Enter visual block mode |
y | Yank the selected text |
d | Delete the selected text |
~ | Change the case of the selected text |
> | Indent the selected text |
< | Unindent the selected text |
Reference: