Skip to Content

The Ultimate Guide to Advanced VI Editing Commands

Advanced vi Tips

Mastering vi command

Advanced VI Editor Commands
For experienced users looking to enhance their editing efficiency:
Example: Use :s/old/new/g to replace all occurrences of “old” with “new” in the current line.
Command Effect
:set nu Display line numbers in the editor.
:set nonu Hide line numbers.
:wq Save changes and exit the editor.
😡 Save changes and exit (similar to :wq).
:e filename Open a different file for editing.
gg Go to the first line of the file.
G Go to the last line of the file.
/pattern Search for ‘pattern’ in the file.
n Repeat the last search forward.
N Repeat the last search backward.
Using Visual Mode for Selection
To select text for editing:
Example: Press v, then use arrow keys to select text, followed by commands like d to delete or y to yank (copy).
Command Effect
v Select text character by character.
V Select entire lines.
Ctrl + v Select a block of text (visual block mode).
d Delete selected text.
y Yank (copy) selected text.
Using Macros for Repetitive Tasks
To record and execute macros:
Example: Press q a, perform actions, then press q again to stop recording. Execute with @a.
Command Effect
q{register} Start recording a macro into {register} (e.g., qa).
@{register} Execute the macro stored in {register}.
@@ Repeat the last executed macro.
Sophisticated Search and Replace
To perform complex replacements:
Example: Use :%s/old/new/gc, where ‘c’ prompts for confirmation before each replacement.
Command Effect
%s/pattern/replacement/gc Replace ‘pattern’ with ‘replacement’ globally with confirmation.
:g/pattern/d Delete all lines matching ‘pattern’.
:v/pattern/d Delete all lines not matching ‘pattern’.
Navigating Between Files with Buffers
To manage multiple files efficiently:
Example: Use commands like :bn (next buffer) or :bp (previous buffer).
Command Effect
:ls List all open buffers.
:b{number} Switch to buffer {number}.
:bd Delete a buffer from the list.
:bn Go to the next buffer.
:bp Go to the previous buffer.
Differentiating Between Modes with Highlighting
To visually distinguish between modes:
Example: Set syntax highlighting with: :syntax on in command mode.
Command Effect
:syntax on Enable syntax highlighting.
:syntax off Disable syntax highlighting.
:set hlsearch Highlight search results.
:set nohlsearch Turn off search highlighting.
:set wrapscan Wrap search at end of file.
Scripting and Command-Line Editing in VI
To automate tasks using scripts:
Example: Use :source script.vim to execute commands from a script file.
Command Effect
:source filename.vim Execute commands from filename.vim.
:r filename Read content from filename into current buffer.
:w !command Write output of current buffer to an external command.
:!command Execute an external command without leaving vi.
:sh Open a shell prompt while in vi.
Advanced Navigation Commands in VI
For experienced users looking to enhance their navigation skills in the VI editor:
Example: Use :10 to move the cursor directly to line 10 of the file.
Command Effect
0 Move to the beginning of the current line.
$ Move to the end of the current line.
w Move forward to the beginning of the next word.
b Move backward to the beginning of the previous word.
e Move to the end of the current word.
H Move to the top of the screen.
M Move to the middle of the screen.
L Move to the bottom of the screen.
nH Move down to the nth line from the top of the screen.
nL Move up to the nth line from the bottom of the screen.
Scrolling Commands for Efficient Navigation
To scroll through your document without moving the cursor:
Example: Use Ctrl + d to scroll down half a page, or Ctrl + u to scroll up half a page.
Command Effect
Ctrl + f Scroll forward one full screen.
Ctrl + b Scroll backward one full screen.
Ctrl + d Scroll down half a screen.
Ctrl + u Scroll up half a screen.
Navigating Between Lines and Characters Quickly
To move through lines and characters efficiently:
Example: Type 5j to move down five lines at once.
Command Effect
{number}j Move down {number} lines.
{number}k Move up {number} lines.
{number}h Move left {number} characters.
{number}l Move right {number} characters.
f{char} Move forward to next occurrence of character {char}.
F{char} Move backward to previous occurrence of character {char}.
Navigating with Marks and Jumps
To set marks for quick navigation:
Example: Use ma to set mark ‘a’, then jump back with ‘a.
Command Effect
ma Set mark ‘a’ at current cursor position.
‘a Jump back to mark ‘a’.
`a Jump back to exact position of mark ‘a’.
:marks List all set marks.
Navigating with Search Commands
To quickly find text in your document:
Example: Use /search_term to search for “search_term”.
Command Effect
/pattern Search forward for ‘pattern’.
?pattern Search backward for ‘pattern’.
n Repeat last search in same direction.
N Repeat last search in opposite direction.