DEV Community

Cover image for Linux 101: Part 5 - Mastering VI Editor: A Beginner's Guide to Text Editing, Navigation, and Essential Commands
Himanshu Bhatt
Himanshu Bhatt

Posted on

Linux 101: Part 5 - Mastering VI Editor: A Beginner's Guide to Text Editing, Navigation, and Essential Commands

Introduction to VI Editor

The VI Editor is one of the most powerful and widely used text editors in Linux and UNIX-based systems. It is available by default on nearly all Linux distributions and is highly valued for its simplicity, efficiency, and versatility. Whether you are editing a simple text file or writing complex scripts, VI offers a fast and reliable editing experience.


Features of VI Editor

  • Modes: VI operates in different modes, namely:

    • Command Mode: The default mode where you can navigate and edit text. In this mode, you can delete, copy, and paste text, among other tasks.
    • Insert Mode: Allows you to insert or modify text. When you're in this mode, you can type as you would in any other text editor.
    • Visual Mode: Enables selecting text for manipulation or copying. You can highlight a portion of the text, then cut or copy it.

How to Switch Between Modes:

  • Command Mode: When you open VI, it starts in Command Mode. If you're in any other mode and want to return to Command Mode, press Esc.

  • Insert Mode: To enter Insert Mode, press i (insert before the cursor), I (insert at the beginning of the line), a (insert after the cursor), or A (insert at the end of the line). To exit Insert Mode and return to Command Mode, press Esc.

  • Visual Mode: To enter Visual Mode, press v to select characters or V to select entire lines. Once you're in Visual Mode, you can navigate with the arrow keys to select the text you want. Press y to yank (copy) the selected text or d to delete it. To exit Visual Mode, press Esc.


Advantages of Using VI Editor

  • Lightweight & Fast: VI is a terminal-based editor, meaning it consumes very little system resources. It’s fast and works well even on older machines or remote servers.

  • Ubiquitous: VI comes pre-installed on nearly all Linux systems, so you don’t need to worry about installing a text editor when you work on different systems.

  • Robustness: It’s extremely stable and reliable, making it a go-to tool for many system administrators and developers, especially when working in headless or remote environments.

  • Powerful Customization: With Vim (a version of VI), you can tweak your editor to work exactly how you want it to, from the layout to advanced syntax highlighting and error checking.

  • Enhanced Productivity: Once you get accustomed to its commands and keybindings, you’ll notice a significant boost in text editing speed and efficiency.


How to Install VI Editor

Most Linux distributions come with VI or Vim (an enhanced version of VI) installed by default. However, if for some reason it is not already installed, you can install it using the following commands based on your distribution:

  • Debian/Ubuntu:

    sudo apt install vim
    
  • CentOS/RHEL:

    sudo yum install vim
    
  • Fedora:

    sudo dnf install vim
    
  • openSUSE:

    sudo zypper install vim
    
  • Arch Linux:

    sudo pacman -S vim
    

Once installed, you can open VI or Vim by typing vi or vim followed by the filename in the terminal:

vi filename.txt
Enter fullscreen mode Exit fullscreen mode

Navigating in VI Editor

One of the core strengths of the VI Editor is its efficient navigation system. Instead of using a mouse to move around, you can navigate through your text entirely using the keyboard. This makes editing faster and more efficient, especially once you get used to it.

Basic Navigation Commands

  • h: Move the cursor left by one character.
  • j: Move the cursor down by one line.
  • k: Move the cursor up by one line.
  • l: Move the cursor right by one character.

These are the basic movement keys, often referred to as the home row keys. They allow you to navigate around the text without taking your hands off the keyboard.


Moving by Words

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

These commands are helpful for quickly jumping across words.


Moving to Specific Positions

  • $: Move the cursor to the end of the current line.
  • 0: Move the cursor to the beginning of the current line.
  • gg: Move the cursor to the beginning of the document (top).
  • G: Move the cursor to the end of the document (bottom).

These commands help you quickly navigate to the start or end of the document, which is especially useful when working with large files.


Moving by Line Numbers

  • 50G: Jump to the 50th line of the document.
  • :n: Jump to a specific line, where "n" is the line number. For example, :20 takes you to line 20.

Moving by Paragraphs

  • {: Move the cursor up to the beginning of the previous paragraph.
  • }: Move the cursor down to the beginning of the next paragraph.

This is helpful when working with large sections of text or code, as it lets you quickly skip between blocks of content.


Navigating to the Start or End of a File

  • Ctrl + f: Move forward one screen (page).
  • Ctrl + b: Move backward one screen (page).

This is useful when you need to scroll through larger chunks of text quickly without jumping line by line.


A Trick to Remember VI Navigation Shortcuts

The key to remembering VI's navigation commands is to associate each key with its direction or behavior:

  • h, j, k, l: Think of these as your home row arrow keys.

    • h = left, j = down, k = up, l = right.
  • w, b, e: These are all word navigation commands:

    • w = "w" for word, moves forward to the next word.
    • b = "b" for backward, moves backward to the previous word.
    • e = "e" for end, moves to the end of the current word.
  • 0, $: Think of these as line navigation:

    • 0 = start of the line.
    • $ = end of the line.
  • gg, G: These are file position commands:

    • gg = beginning of the file.
    • G = end of the file.
  • Ctrl + f, Ctrl + b: These are scroll commands:

    • Ctrl + f = forward one page.
    • Ctrl + b = backward one page.

Editing Text in VI Editor

Once you're familiar with navigating in VI, the next step is to learn how to edit your text. VI's powerful editing features allow you to make changes quickly and efficiently. Let’s go through some of the most common text-editing operations in VI.


Inserting Text

To insert or add text in VI, you need to switch from Command Mode to Insert Mode.

  • i: Insert text before the cursor.
  • I: Insert text at the beginning of the current line.
  • a: Insert text after the cursor.
  • A: Insert text at the end of the current line.
  • o: Open a new line below the current line and enter Insert Mode.
  • O: Open a new line above the current line and enter Insert Mode.

Once you’ve entered Insert Mode, you can type normally. To return to Command Mode, simply press the Esc key.

Example:

  1. Press i to insert before the cursor and start typing.
  2. Press Esc to exit Insert Mode and return to Command Mode.

Deleting Text

In Command Mode, you can delete text using the following commands:

  • x: Delete the character under the cursor.
  • dw: Delete the current word.
  • dd: Delete the entire line where the cursor is.
  • d$: Delete from the cursor’s current position to the end of the line.
  • d0: Delete from the cursor’s current position to the beginning of the line.

You can also combine commands to delete multiple lines:

  • 5dd: Delete 5 lines starting from the current line.

Example:

  1. Press x to delete the character under the cursor.
  2. Press dw to delete the word under the cursor.

Copying Text (Yanking)

In VI, copying text is referred to as yanking.

  • yy: Yank (copy) the entire current line.
  • yw: Yank (copy) the current word.
  • y$: Yank (copy) from the cursor’s position to the end of the line.

Once you’ve yanked the text, you can paste it using the p command (paste after the cursor) or P (paste before the cursor).

Example:

  1. Press yy to yank (copy) the entire line.
  2. Press p to paste the copied line after the current line.

Undoing and Redoing Changes

Sometimes, you’ll make a mistake while editing. Fortunately, VI allows you to undo or redo changes:

  • u: Undo the last change.
  • Ctrl + r: Redo the last undone change.

Example:

  1. Press u to undo the most recent change.
  2. Press Ctrl + r to redo the change you just undid.

Saving and Exiting

After editing your file, you’ll likely want to save your changes and exit VI. Here’s how:

  • :w: Save (write) the file without exiting.
  • :wq: Save the file and exit VI.
  • :x: This is equivalent to :wq and saves and exits the file.
  • :q: Exit the file without saving (if no changes were made).
  • :q!: Force exit without saving (if changes were made).

Example:

  1. Press :w to save the file but keep editing.
  2. Press :wq to save the file and exit.

Searching and Replacing Text

You can search for specific text within your file and even replace it using VI. Here’s how to do it:

Searching:

  • /text: Search forward for "text" in the file.
  • ?text: Search backward for "text".
  • n: Move to the next occurrence of the search term.
  • N: Move to the previous occurrence of the search term.

Replacing:

To replace text in VI, use the following command format:

  • :%s/old/new/g: Replace all occurrences of "old" with "new" in the entire file.
  • :s/old/new/g: Replace all occurrences of "old" with "new" in the current line.
  • :%s/old/new/gc: Replace all occurrences with confirmation (you'll be asked for confirmation before each replacement).

Example:

  1. Press /hello to search for the word "hello" in the file.
  2. Press n to go to the next occurrence.
  3. Press :%s/hello/world/g to replace all occurrences of "hello" with "world."

Conclusion

Editing in VI Editor becomes incredibly efficient once you master the basic commands for inserting, deleting, copying, undoing, saving, and searching text. These operations help you quickly modify and organize your files, whether you’re editing code or writing documents. VI’s keyboard-centric design may seem challenging at first, but once you practice, it will become a seamless and powerful part of your Linux workflow.

With these basic editing commands, you're well on your way to becoming comfortable using VI for text editing tasks in no time!


Got questions or need clarification? Drop a comment below! 🛠️

Stay updated with my latest blogs and tech insights! 🚀

Follow me on _himanshubhatt1 for more updates and future posts.

Top comments (0)