Vim Essential Commands & Techniques
In this article, we’ll explore crucial Vim commands and techniques. I’ll be highlighting several commands that I regularly use for my daily code editing tasks.
Basic Navigations Techniques
We can do the navigation throughout the file in normal mode. Here is the elementary navigation
h — Move Left, j — Move Down, k — Move Up, l — Move right
Basic Editings
- Insert Text: Press
i
in Normal Mode to start inserting text at the cursor position.
Deleting Text
x
deletes the character under the cursor.dd
deletes the entire line.
Copying and Pasting
yy
copies the current line.p
pastes the copied or deleted content after the cursor.P
pastes before the cursor.
Advanced Navigation Techniques
Jumping between documents
Note: To jump between documents, we must be in normal mode in Vim. Moving to normal mode is very easy in Vim, by clicking the key ESC we can achieve this.
w — The key ‘w
’ (lowercase w) moves the cursor to the beginning of the next word delimited by whitespace or punctuation.
W — The key ‘W
’ (Uppercase W) moves the cursor to the beginning of the next word delimited only by whitespace and tabs.
e — The key ‘e
’ (lowercase e) moves the cursor to the end of the next word delimited by whitespace or punctuation.
E — The key ‘E
’ (Uppercase E) moves the cursor to the end of the next word delimited only by whitespace and tabs.
b — The key ‘b
’ (lowercase b) moves the cursor backward to the beginning of the previous word delimited by whitespace or punctuation.
B — The key ‘B
’ (Uppercase B) moves the cursor Backword to the beginning of the previous word delimited only by whitespace and tabs.
0 — Moves the cursor to the beginning of the line
$ — Moves the cursor to the end of the line
^ — Moves the cursor to the first non-blank character of the line.
g_ — This command is used to move the cursor to the last non-blank character of the line
Here in this, the last two commands are most useful if you are using vim to code.
f{char} — Moves the cursor to the next occurrence of the specified character, if the {char} is present in the current line
t{char} — Moves the cursor to just before the next occurrence of the specified character, if the {char} is present in the current line.
F{char} — Moves the cursor backward to the previous occurrence of the specified character, if the {char} is present in the current line
T{char} — Moves the cursor backward to just before the previous occurrence of the specified character, if the {char} is present in the current line.
Note: Here in the above last two commands, just need to replace {char}
with the desired character.
Advanced Editings
- Move to the beginning of the line and enter Insert Mode:
Shift + i
- Move to the end of the line and enter Insert Mode:
Shift + A
- Pressing
c
twice deletes the current line and enters Insert Mode.
Deletion and Modification Commands
Uppercase D (D)
: Deletes from the current cursor position to the end of the line, staying in Normal Mode.Uppercase C (C)
: Deletes from the current cursor position to the end of the line, entering Insert Mode.
Word Operations
- To delete a word with the cursor in the middle of the word:
diw
(delete in a word) - To change a word with the cursor in the middle of the word:
ciw
(change in a word) - To copy a word with the cursor in the middle of the word:
yiw
(yank in a word)
Handy Commands for Codebases
In Vim, the ci
and ca
commands, along with their variations, are part of Vim's text object selection and editing capabilities.
ci
stands for "change inside," andca
stands for "change around."
These commands work with text objects or motions and are followed by an object or a motion to define the area of text you want to affect.
ci{}
: Change inside{}
. This command operates within a specified pair of characters, such as parentheses()
, curly braces{}
, square brackets[]
, single quotes''
, or double quotes""
.- For example,
ci(
changes the content inside the parentheses. ca{}
: Change around{}
. This command changes the specified pair of characters along with their contents.- For example,
ca"
changes the text inside double quotes and the double quotes themselves.
ci{b|B}
and ca{b|B} (Normal mode command)
ciB
stands for "change inside Block," andcaB
stands for "change around Block."- These commands operate within or around a block of text enclosed by curly braces
{}
. - For example,
ciB
will change the content inside the curly braces, whilecaB
will change the content along with the curly braces themselves. cib
andcab
-> These commands operate within or around a block of text enclosed by curly braces()
.
Basic Search
/pattern
- Search forward for the specified pattern.?pattern
- Search backward for the specified pattern.- Press
n
to find the next occurrence in the same direction, orN
to find the previous occurrence in the opposite direction. *
- searching for the next whole word under the cursor#
- Searching for the previous whole word under the cursor
Highlight Search Matches
- Enable search highlighting by typing
:set hlsearch
. To turn it off, use:set nohlsearch
.
Search and Replace
- To replace text interactively, use the
:substitute
command::s/search_pattern/replace_text/g
. - Add a
c
flag for confirmation before replacing each occurrence::s/search_pattern/replace_text/gc
.
Undoing and Redoing Actions
- u (Undo): The ‘u’ command in Vim is used to undo the last change made to the text. For example, if you delete a word, change a line, or perform any editing action, pressing ‘u’ will revert that specific action.
- Ctrl + r (Redo): In Vim, pressing Ctrl + r after undoing an action will redo the previously undone action. This command allows you to revert back to a change that was undone using the ‘u’ command.
Retrieving Files and Reading External Command Output
- Retrieve your test file using the command
:r test
where test is
the name of the file you used.
the file you retrieve is placed below the cursor line. - you can also read the output of an external command. for example,
:r !ls
reads the output of the ls command and puts it below the
cursor.
Handling Multiple Files and Navigating Splits in Vim
:vsp file2.txt
opens 'file2.txt' in a vertical split.:sp anotherfile.py
opens 'anotherfile.py' in a horizontal split.
Switching Between Splits
Ctrl + w + w
: switch between different splits.Ctrl + w + (h/j/k/l)
: Moves to the left/below/above/right split.
Tab Handling
:tabe <file>
or:tabedit <file>
: Opens a file in a new tab.gt
andgT
: Navigate between tabs.:tabm[ove] #
: Moves to a specific tab (e.g.,:tabmove 3
).:tabclose
or:tabc
: Closes the current tab.
Operating Cases
g~
(Swap Case): This command swaps the case of the characters. For instance, if you have a selection or are positioned on a character in Vim, usingg~
will invert the case. Uppercase letters become lowercase, and vice versa, within the selected text or at the cursor position.gU
(Uppercase): When used in Vim,gU
converts the selected text or the character under the cursor to uppercase.gu
(Lowercase): Thegu
command in Vim converts the selected text or the character under the cursor to lowercase.
These are a few commands I frequently use for file editing in my daily tasks. I trust you found this article enjoyable. Until the next insightful piece, take care and stay tuned for more enriching content!