How To Use Vi In Linux

How To Use Vi In Linux

Reading time1 min
#Linux#Programming#OpenSource#vi#TextEditor#CommandLine

Mastering Advanced Navigation and Editing Techniques in vi for Efficient Linux Text Management

Forget the myth that vi is outdated and hard to learn; unlocking its advanced navigation and editing features transforms it into the ultimate tool for Linux efficiency that modern developers swear by.


If you've ever dipped your toes into the world of Linux, chances are you’ve encountered vi — one of the oldest and most powerful text editors in the Unix/Linux universe. While many shy away from vi, dismissing it as archaic or difficult to master, the reality couldn’t be further from the truth.

Understanding advanced navigation and editing techniques in vi opens a gateway to incredibly fast, efficient text manipulation — whether you’re writing code, editing config files, or performing system maintenance. This post dives deep into how you can leverage vi’s power to speed up your workflow dramatically, moving you from casual user to a true power user.


Why Mastering vi Matters

When managing Linux systems, editing text files isn’t just about typing text. It’s about doing so quickly and accurately without leaving your terminal or switching contexts. Modern IDEs are great, but knowing vi means:

  • Instant availability: You can edit any file on virtually any Linux box quickly.
  • Minimal resource consumption: Vi runs smoothly even on slow or embedded systems.
  • Speed and precision: Advanced commands let you navigate and edit without repetitive key presses.

Ultimately, mastering vi reduces downtime during critical edits and boosts your overall productivity.


Getting Started: Modes in vi Explained

Before diving into advanced commands, a quick refresher:

  • Normal mode (default): For navigation and commands.
  • Insert mode: For inserting/editing text.
  • Visual mode: For selecting text (characters, lines, blocks).
  • Command-line mode (:): For saving files, searching, running commands.

Press Esc anytime to return to Normal mode.


Advanced Navigation Techniques

1. Move by Words and Paragraphs

Efficient navigation means less wasted time moving around your text.

  • w: Jump forward to the start of next word.
  • b: Jump backward to the start of previous word.
  • e: Jump to end of current/next word.

Example: To move forward 3 words, type 3w.

  • { and }: Move backward/forward by paragraph.

2. Line Navigation Shortcuts

Quick line jumps are crucial:

  • 0 (zero): Start of line.
  • ^: First non-whitespace character of line.
  • $: End of line.

Example: Quickly move to end of line by pressing $.

To move directly to a specific line number:

:[line_number]

E.g., :42 moves cursor to line 42.

3. Search and Repeat Search

Search with /pattern then press n to go forward or N backward through matches.

Example:

/foo

Finds "foo". Press n for next occurrence.


Efficient Editing Techniques

1. Deleting Text Smartly

Commands combine motion with actions like delete (d), change (c), yank (y).

Examples:

  • dw: Delete from cursor to start of next word.
  • d$: Delete from cursor to end of line.

You can combine numbers too:

  • d3w: Delete next three words.

To delete an entire line:

dd

Or delete multiple lines:

3dd

2. Changing Text Quickly

Use change command (c) similar to delete but automatically enters insert mode afterward.

Examples:

  • cw: Change current word (delete + insert mode).
  • c$: Change till end of line.

3. Copying (Yanking) and Pasting

Copying lines or texts is simple using yank (y) command:

  • Yank whole line:
yy
  • Yank multiple lines:
3yy

Paste with:

p   # after cursor
P   # before cursor

4. Visual Mode for Selection

Press v (character-wise), V (line-wise), or <Ctrl-v> (block-wise) to select text.

Once selected:

  • Press d to delete,
  • Press y to copy,
  • Press any movement keys to expand/reduce selection.

Combining Commands with Counts & Motions for Speed

vi shines when you combine commands plus motions plus counts — this is where expert efficiency kicks in.

Example workflow:

Say you want to delete three words starting from current cursor position:

d3w

Or substitute "foo" with "bar" throughout the file via command mode:

:%s/foo/bar/g

(Here % means entire file; removing % applies substitution only on the current line.)


Customizing Your vi Experience

To get even more out of vi for daily work:

  1. Enable syntax highlighting by adding in your .vimrc (if you use vim):
syntax on
set number          " show line numbers
set ai              " auto indent lines
set hlsearch        " highlight search results
  1. Navigate visually using relative numbers:
set relativenumber 

This makes jumping around easier because counts now refer easily visible distances.


Practical Example: Fixing a System Config Fast

Imagine you need quickly edit the network config file /etc/network/interfaces.

sudo vi /etc/network/interfaces

You want to find all mentions of “eth0” and replace with “enp0s3” (common new naming).

At command prompt inside vi:

:%s/eth0/enp0s3/gc

The trailing c confirms each replacement interactively — safer for critical configs.

Use /eth0 then n, then at each instance type . to repeat last change if desired, or skip with 'n'.

When done editing hit:

:wq    # save & quit 

Final Tips for Mastery

  • Practice daily: A few minutes daily navigating & editing with vi vastly improves retention.
  • Learn motions first: Good movement is half the battle; start with quick motions before complex edits.
  • Use help inside vim: Type :help <command> — e.g., :help d
  • Experiment with registers: Store multiple yanks/deletes in named buffers like "a, "b.

With these techniques at your fingertips, that old-timer called vi becomes a turbocharged ally in Linux management — a tool that modern developers rely on not out of nostalgia but because it just works brilliantly when mastered.


Ready to level up? Fire up your terminal now — jump into a file with vi — try some commands above — watch your productivity soar!