Photo by Aaron Burden on Unsplash
Vi / Vim simplified - CheatSheet
In this article I'm going to share my most used commands in Vim that help me in day to day terminal workloads
Vi - Unix Editor, Other editors are like Emacs and Nano.
Syntax to open a file using Vim.
vim File_name
The above command opens an already-created file, else if the file is missing it creates a new file.
Vim - has two operating modes.
Insertion mode
Execution mode
It starts with the Execution mode, to make changes to the File - you have to be in the Insertion mode. At any time, you can see which mode you are in on the status bar which is located at the top of the editor.
To get into Insertion mode - press i
Mode | Enables Users To |
Insert | Insert text by typing. |
Execute | Execute commands within the editor. |
Esc | Returns to command mode. |
Execute Modes Commands
Saving and Exiting Vim
To go back to the Execute command, when you enter the colon (:) operator, a small command prompt section appears at the bottom-left of the editor. That is where you watch for the commands to exit the work.
Command | Function | |
:w {file name} | Saves file with the specified name. | |
:q | Quits when no changes have been made after the last save. | |
:q! | Quits, ignoring changes made. | |
:qa | Quits multiple files. | |
:wq | Saves the current file and quits. | |
:e! | Reverts to the last saved format without closing the file. | |
:help | Opens Vim’s built-in help documentation. |
Navigate through a file in Vim
Navigation Key | Used to |
h | Move left one character |
j | Move down one line |
k | Move up one line |
l | Move right one character |
Shift+L | Move the cursor to the bottom of the screen. |
Shift+H | Move the cursor to the first line of the screen. |
Editing a file in Vim
Editing operators in command mode are powerful tools that can be used to manipulate text with simple keystrokes.
NB: They can also be used in combination with motions to edit multiple characters.
Command | Action |
a | Insert characters to the right of the cursor |
A | Append characters to the current line |
i | Insert characters to the left of the cursor |
I | Insert characters at the beginning of the current line |
o | Add a new line after the current line |
O | Insert a new line above the current line |
Deleting characters/ words in the file
Command | Action |
x | Delete the character selected by the cursor. |
d | Delete text. |
dd | Delete the current line. |
Copying and Pasting on the file
Command | Action |
p | Paste text on the line below the cursor. |
P | Paste text on the line above the cursor. |
y | Copy text. |
yy | Copy the line above the cursor. |
Undo option
Command | Action |
u | Undo the latest change. |
U | Undo all changes on the current line. |
Searching through the file
Command | Action |
/{text string} | Search through the document for specified text. |
?{text string} | Search backwards through document for specified text. |
Best Resources to Practise
Blogs
atmos cheatsheet - easy straight to the point
The geekdiary - Well-outlined cheatsheet
Games or Interactive Sessions
Type
vimtutor
on a Unix-based terminal