Master the Vi Text Editor - Linux Tutorial

As a developer, mastering the VI editor is a valuable skill that can significantly boost your productivity when working on the command line or editing configuration files. VI is a powerful and efficient text editor with a steep learning curve, but the benefits it offers are well worth the effort. In this guide, we’ll explore the essentials of VI with practical examples to help you become proficient in using this versatile tool.

VI Modes

VI operates in different modes, each serving a distinct purpose. Understanding these modes is fundamental to effective VI usage:

  1. Normal Mode: The default mode for navigating and manipulating text.
  2. Insert Mode: Allows you to insert or modify text.
  3. Command-Line Mode: Used for executing commands.

To switch between modes:

  • From Normal to Insert: Press i.
  • From Insert to Normal: Press Esc.
  • From Normal to Command-Line: Press :.

Essential VI Commands

  • h, j, k, l: Move the cursor left, down, up, or right respectively.
  • w, b: Move forward or backward by a word.
  • 0, $: Move to the beginning or end of a line.
  • G: Move to the end of the file.
  • gg: Move to the beginning of the file.

Editing

  • i: Enter Insert mode before the cursor.
  • I: Enter Insert mode at the beginning of the line.
  • a: Enter Insert mode after the cursor.
  • A: Enter Insert mode at the end of the line.
  • o, O: Open a new line below or above the current line.

Deleting and Copying

  • x: Delete the character under the cursor.
  • dd: Delete the current line.
  • yy: Copy the current line.
  • p: Paste the copied or deleted text after the cursor.
  • P: Paste before the cursor.

Searching and Replacing

  • /pattern: Search forward for a pattern.
  • ?pattern: Search backward for a pattern.
  • n: Move to the next occurrence.
  • N: Move to the previous occurrence.
  • :s/old/new/g: Replace all occurrences of ‘old’ with ‘new’ on the current line.
  • :%s/old/new/g: Replace all occurrences of ‘old’ with ‘new’ in the entire file.

Saving and Quitting

  • :w: Save changes.
  • :q: Quit (exit) if no changes were made.
  • :wq or ZZ: Save and quit.
  • :q!: Quit without saving.
Next Post Previous Post
No Comment
Add Comment
comment url