Skip to Content

The Ultimate Guide to Advanced VI Editing Commands

The VI editor is a powerful text editing tool that has been a staple in the UNIX and Linux environments for decades. While many users are familiar with its basic functionalities, such as opening files and entering text, mastering advanced commands can significantly enhance your productivity and editing efficiency. This guide aims to delve into the more sophisticated features of the VI editor that experienced users can leverage to streamline their workflow.

Understanding VI Modes

Before we explore advanced commands, it’s essential to understand the two primary modes of VI:

  • Command Mode: This is the default mode when you open a file. In this mode, you can navigate through the document, delete text, and perform various commands without entering text directly.
  • Insert Mode: You enter this mode by pressing i, allowing you to input text into your document. To return to Command Mode, simply press Esc.

Advanced Navigation Techniques

Navigating within a file efficiently is crucial for any editor. Here are some advanced commands that can help you move around more fluidly:

  • Jumping to Specific Lines: Use :n to jump directly to line number n. For instance, :50 takes you to line 50.
  • Search and Navigate: Press / followed by a search term to find specific text. Use n to move to the next occurrence and N to go back.

Efficient Text Manipulation

Once you’re comfortable navigating, mastering text manipulation commands will elevate your editing capabilities:

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
  • Yanking and Pasting Text: Use yy to yank (copy) an entire line and p to paste it below the current line. For multiple lines, use 3yy to yank three lines.
  • Deleting Text: The command dd deletes the entire line where the cursor is located. To delete from the cursor position to the end of the line, use d$.

Utilizing Buffers and Macros

For users who frequently repeat tasks, understanding buffers and macros can save significant time:

  • Using Buffers: You can store text in registers by using “ay (yank into register ‘a’) and later paste it with “ap.
  • Recording Macros: Start recording a macro by pressing q followed by a letter (e.g., qa). Perform your editing tasks and then press q again to stop recording. Replay with @a.

Vi command cheat sheet

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.

Edward Dan

Monday 21st of October 2024

Kudos to the you for compiling such a comprehensive guide. Keep up the excellent work!