How To Use Vi Editor In Linux

How To Use Vi Editor In Linux

Reading time1 min
#Linux#OpenSource#Software#ViEditor#Terminal#Sysadmin

Mastering the Essential Vi Editor Commands for Efficient Linux Text Editing

If you've ever logged into a Linux server, perhaps via SSH, you've likely encountered the Vi editor. Despite the prevalence of modern graphical editors, Vi endures as a fundamental tool — favored for its blazing speed, minimal resource usage, and availability on virtually every Unix-like system. Yet, many users shy away from Vi assuming it’s archaic or challenging to learn.

Forget those myths! Knowing how to efficiently use Vi can elevate your productivity and confidence when editing configuration files, writing scripts, or making quick text changes on remote machines without a GUI.

In this post, we’ll break down the essential Vi commands you need to master today. By the end, you’ll see exactly why investing time in Vi is invaluable for any Linux sysadmin or developer.


Why Use Vi Editor?

  • Almost everywhere: Guaranteed to be installed on any Linux/Unix system.
  • Resource-light: Runs smoothly on servers and minimal systems where GUIs aren’t available.
  • Efficient navigation & editing: Teaches you powerful keyboard-driven editing that can be faster than mouse-centric GUIs once mastered.

Starting Up: Opening Files with Vi

To start editing a file with Vi (or Vim – Vi Improved), open your terminal and type:

vi filename.txt

If the file exists, it opens; if not, Vi opens an empty buffer ready to create a new file named filename.txt.


Understanding Vi Modes

Vi has two main modes:

  • Command Mode: The default mode when you open Vi. Keystrokes are interpreted as commands.
  • Insert Mode: For typing/editing text.

Switch between them using:

  • i — Enter insert mode before cursor.
  • Esc — Switch back to command mode from insert mode.

Always press Esc before typing any commands.


Essential Commands Cheat Sheet

1. Moving Around

CommandAction
hMove left
jMove down
kMove up
lMove right
0Beginning of the line
$End of the line
wJump forward one word
bJump backward one word

2. Inserting Text

  • i — Insert before cursor
  • I — Insert at beginning of line
  • a — Append after cursor
  • A — Append at end of line
  • o — Open new line below current
  • O — Open new line above current

3. Deleting Text

  • x — Delete character under cursor
  • dw — Delete word from cursor forward
  • dd — Delete entire current line

4. Undo / Redo

  • u — Undo last change
  • Ctrl + r — Redo undone change

5. Saving and Exiting

All commands below require switching to command-line mode by first pressing Esc, then typing:

  • :w — Save (write) changes
  • :q — Quit (fails if changes aren’t saved)
  • :wq or ZZ (shift + zz) — Save and quit
  • :q! — Quit without saving changes

Practical Editing Example

Let's say you want to correct a typo in /etc/hosts.

  1. Open the file:
sudo vi /etc/hosts
  1. Find the typo by moving with arrow keys or using /pattern. For example, search:
/localhost

and press Enter.

  1. Use arrow keys or navigation commands (h, j, etc.) to position cursor on the mistake.

  2. Press i (insert mode), fix the typo by typing normally.

  3. Hit <Esc> to return to command mode when done editing.

  4. Save and quit:

:wq
  1. You’re back at your shell prompt with your edits saved!

Advanced Tip: Using Search & Replace Inside Vi

To replace every occurrence of "foo" with "bar" in the file:

:%s/foo/bar/g

Here:

  • %s means search & replace throughout entire file
  • /foo/ is what you’re searching for
  • /bar/ is replacement text
  • /g means global on each line (replace all instances)

Wrapping Up

Mastering these essential commands opens doors to quick and powerful text editing right in your terminal - no mouse needed!

Don’t worry about memorizing everything at once—start by practicing these basics whenever you edit files via SSH or terminal on your local machine.

As you grow more comfortable, Vim even offers customizations and plugins that make it an unbeatable coding environment!

Remember: every Linux power user can benefit hugely from mastering Vi, so make it part of your toolkit starting today!


Ready to dive deeper? Next time we’ll explore visual mode selections, macros, and configuration tricks to supercharge your workflow!

Happy Vimming!