Table of Content
Vim Modes
Vim operates in distinct modes, each designed for specific tasks:
- Normal Mode: Default state for efficient movement, deletion, searching, and more.
- Insert Mode: Allows you to type text directly into your file.
- Visual Mode: Enables text selection for manipulation.
How to Navigate using Vim Key Bindings
Basic Movement
Here are some basic movement keys:
h --> move left
j --> move down
k --> move up
l --> move right
10j --> move down 10 lines
H --> top line on screen
M --> middle line on screen
L --> bottom line on screen
The left, right, up and down arrow keys can also be used to navigate.
Moving to Top and Bottom
You can use these keys to move to the top or bottom of the page:
gg --> Move to the first line of the file
G --> Move to the last line of the file
Moving to a Specific Line
You can move to a specific using : followed by the line number:
:20 --> Go to line 20 of the file
Moving Within a Line
You can use these keys move to the start or end of a line:
0 --> Go to the start of the line
^ --> Go to the first non-whitespace character of the line
$ --> Go to the end of the line
Moving Through Words
Here are some keys for moving through lines of text:
b --> Go to the previous word
e --> Go to the end of the current word
w --> Go to the next word
How to Edit Text in Normal Mode
Copy and paste
You can copy and paste characters or lines with these keys:
y --> Copy (yank) the character under the cursor
yy --> Copy (yank) the entire line
p --> Paste the previously deleted or copied text after the cursor
Shift + p --> Paste the previously deleted or copied text left from the cursor
"*y or "+y --> Copy to systems clipboard
"*p or "+p --> Paste from the systems clipboard after the cursor
Delete and change
You can delete (d) or change (c) characters or lines with these keys:
x --> Delete the character under the cursor
Shift + x --> Delete the character left from the cursor
dd --> Delete the entire line
dw or cw --> Delete the word on the right
dip or cip --> Delete only the paragraph
dap or cap --> Delete the paragraph and the following blank lines
With d, Vim remains in normal mode and with c, Vim switches to input mode after deletion and you can start typing directly.
Undo and Redo
u --> Undo the last action
Ctrl + r --> Redo the undone action
Searching and Replacing
/ --> Start searching forward
? --> Start searching backward
:s/old/new/g --> Replace all occurrences of "old" with "new" in the entire file
Insert Mode
In Insert mode, you can type text directly into your file. Here are some key bindings to help you navigate in this mode:
Esc --> Return to Normal Mode
i --> Start inserting text before the cursor
a --> Start inserting text after the cursor
o --> Open a new line below the current line and start inserting text
Visual Mode
Visual mode is useful for selecting and manipulating text visually. Here are some key bindings:
v --> Start character-wise visual mode
V --> Start line-wise visual mode
Ctrl + v --> Start block-wise visual mode
d --> Delete the selected text
y --> Copy (yank) the selected text
p --> Paste the copied text after the cursor
More Navigation Commands
Scrolling
Ctrl + u --> Move half a screen up
Ctrl + d --> Move half a screen down
Ctrl + b --> Move one full screen up
Ctrl + f --> Move one full screen down
Jumping between Words and Paragraphs
( --> Jump to the beginning of the previous sentence
) --> Jump to the beginning of the next sentence
{ --> Jump to the beginning of the previous paragraph
} --> Jump to the beginning of the next paragraph
These commands provide a comprehensive overview of the essential Vim key bindings for efficient navigation and text manipulation.
Programming-Specific Vim Key Bindings
Moving Between Functions
In a code file, navigating between functions is a common task. Vim makes it efficient with these keys:
]] --> Move to the beginning of the next function
[[ --> Move to the beginning of the previous function
These commands are invaluable for quickly jumping between different parts of your code.
Indentation
Maintaining consistent code indentation is crucial. Vim enhances this with these keys:
>> --> Indent the current line to the right
<< --> Indent the current line to the left
Effortlessly adjust the indentation to adhere to coding standards and improve code readability.
Folding
Code folding aids in managing large files. Vim provides powerful folding commands:
zf{motion} --> Create a fold (replace {motion} with a movement command)
zo --> Open a fold
zc --> Close a fold
zr --> Reduce folding level throughout the file
zm --> Increase folding level throughout the file
Use folding to collapse and expand sections, this makes it easier to focus on specific parts of your code.
Code Commenting
Efficiently comment and uncomment code with:
gcc --> Comment/uncomment the current line
gc{motion} --> Comment/uncomment the lines covered by {motion}
Speed up the commenting process and maintain code documentation effortlessly.
Matching Parentheses
Effortlessly navigate and understand code structure with:
% --> Move to the matching parenthesis, bracket, or brace
This makes it easier to navigate complex code by quickly jumping between corresponding code blocks.