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

Anyone managing Linux infrastructure long enough has experienced a critical production incident where the only editor available is vi. The difference between fumbling and executing precise, atomic changes in seconds depends on comfort with vi’s navigation and editing arsenal.


Working on a headless server, limited to a serial console, you open /etc/fstab to fix a mount issue. There’s no GUI, no nano, just vi. Here, speed and accuracy matter—a single extra character can cause boot failure.

Modes At a Glance

A fast vi session never confuses modes:

  • Normal mode (default): navigation and commands.
  • Insert mode: direct text entry.
  • Visual mode: robust selection (character, line, block).
  • Command-line mode (:): for operations like save, quit, search, or substitution.

Return to Normal mode anytime using Esc. Misreading mode often leads to frustrating mistakes. On legacy systems running BusyBox vi or Vim 8.x, mode support may vary—watch for visual cues.


Navigation: Beyond Arrow Keys

Word-wise and Semantic Motions

  • w, b, e: jump by word (useful with camelCase or snake_case variables).
  • 3w: move forward three words.
  • {, }: jump by blank-line-separated paragraphs; valuable when editing logs or YAML.

For line-speed:

  • 0: line start; ^: first non-blank; $: line end.
  • :N (e.g. :23): direct to line 23.

Full-file jumps:

  • gg: top of file.
  • G: end of file.

Known issue: On some remote Vim configurations, gg can lag with very large (10k+ line) files over SSH.

Search and Repeat

  • /pattern, then n (forward), N (backward): regex-compatible search.
  • To highlight matches: :set hlsearch.
  • Clear highlight: :nohlsearch.

Note: Poorly timed global substitutions (:g/pattern/d) can be destructive. Always double-check before executing in system config files.


Editing Efficiently

Combined Motions: Action × Direction × Count

Master combining operator, motion, and count:

ActionSyntaxExampleEffect
Deleted{motion}d3wDelete next 3 words
Changec{motion}c$Change to end of line (and enter insert)
Yanky{motion}y}Yank until next paragraph
Repeat.Repeat last change command

Deleting lines:

  • dd: delete line.
  • 3dd: delete next 3 lines (useful during log pruning).

Copy and paste:

  • yy: yank (copy) line.
  • p: paste after cursor; P: before cursor.

Side note: Vim's default clipboard is not system-integrated unless compiled with +clipboard. To yank to system clipboard, use "*y.

Visual Mode Block Selection

Sometimes, config files need columnar edits (e.g., adjusting all indentation). Press Ctrl-v for blockwise, use arrow keys, then apply d/y/c as needed.

Not perfect: blockwise paste (P) can behave unpredictably with tabs vs. spaces—verify alignment, especially in YAML.


Patterns and Substitution

Batch editing is indispensable:

  • Substitute globally: :%s/old/new/g
  • Line-range substitution: :12,24s/foo/bar/g
  • Confirm each: append c (e.g., :%s/eth0/enp0s3/gc)

Regexes work as expected. When replacing tabs with spaces:

:%s/\t/    /g

Practical miss: This won’t always fix indentation in Makefiles where literal tabs are required; use with caution.


vi Customization: Practical .vimrc Snippets

Enhance productivity:

set number          " Show line numbers
set relativenumber  " Relative line numbers (faster line jumps)
set tabstop=4       " Set tab width for code readability
set expandtab       " Convert tabs to spaces
syntax on           " Enable syntax highlighting
set incsearch       " Progressive search
set clipboard=unnamedplus  " Use system clipboard (if compiled)

These features require Vim 7.3+; some (clipboard) require custom builds on minimal distros (e.g., Alpine, Docker images).


Real-World Usage: Rapid Network Config Refactor

Editing /etc/network/interfaces during a migration:

  1. Open file as root:

    sudo vi /etc/network/interfaces
    
  2. Search for all eth0:
    /eth0, then traverse with n.

  3. Substitute all with confirmation:

    :%s/eth0/enp0s3/gc
    
    • g replaces all on a line.
    • c prompts confirm—safer for production.
  4. Save and exit:

    :wq
    

Gotcha: On minimal images with only POSIX vi (/bin/vi), some functions (visual selection, multilevel undo) are unavailable.


Non-Obvious Efficiency Tips

  • Use registers to store/retrieve multiple yanks:
    • Yank to named register: "ayy (register a), paste with "ap.
  • Jump back and forth between edit locations:
    • Ctrl-o, Ctrl-i cycle jump list.
  • Insert previously yanked text in insert mode: Ctrl-R then register name.
  • Repeat the last change: the . operator—especially powerful with substitutions.

vi is not about nostalgia; it is about precision under pressure, resource minimalism, and universal availability. Whether remediating a failed boot loader in a rescue shell or bulk-editing production configs, command fluency in vi means the job gets done—editor stays out of your way.

Provoke thought: What if your only access is a misconfigured single-user mode with / mounted read-only? Knowing how to navigate and yank text without accidental write attempts can mean the difference between a fast fix and downtime.