How To Edit Vi Editor In Linux

5 min read

How to Edit Files with the vi Editor in Linux: A Complete Beginner’s Guide

The vi editor (often referred to as vim when the enhanced version is used) is a powerful, modal text editor that has been a staple of Linux environments for decades. Whether you are managing configuration files, writing scripts, or editing source code, mastering the basics of vi can dramatically improve your productivity and confidence when working on the command line. This article walks you through the essential steps to open, manage, edit, save, and exit files using vi, and it also covers a few handy shortcuts and common pitfalls you might encounter It's one of those things that adds up..

Introduction

The vi editor is built into almost every Linux distribution, making it instantly available without the need for additional installations. Practically speaking, its two‑mode design—command mode and insert mode—can feel unfamiliar at first, but with a systematic approach you can quickly become fluent. Still, this guide is structured to help you understand the editor’s workflow, practice key commands, and troubleshoot issues that often arise for new users. By the end of this article you will be able to confidently open a file, make precise changes, and save those modifications without relying on graphical editors.

Basic Steps to Open and Edit a File

1. Opening a File

To launch vi and load a file, use the following syntax in your terminal:

vi filename.txt

If the file does not exist, vi creates it automatically. You can also specify a path, for example:

vi /etc/hosts

2. Understanding the Two Modes

  • Command Mode – This is the default mode when you first open a file. In this mode you can move around, search, and execute editing commands. The screen displays a “–” at the top-left corner.
  • Insert Mode – Activated by pressing the i key (or a for append). Here you can type and insert text. Press Esc (or Ctrl+ + Esc) to return to command mode.

3. Navigating the File

| Action | Key(s) | Description | |--------|--------|------------- नरेंद्र | | Move up | k | Move one line up | | Move down | j | Move one line down | | Jump to beginning of file | gg | Go to first line | | Jump to end of file | G | Go to last line | | Find a word | /word then Enter | Search forward | | Find previous occurrence | ?word then Enter | Search backward | | Repeat last search | n | Go to next match | | Go to line number | :50G | Jump to line 50 |

Easier said than done, but still worth knowing Still holds up..

4. Inserting and Editing Text

  • Insert at cursor position – Press i to switch to insert mode. You can now type, delete, or overwrite as needed.
  • Append after cursor – Press a to insert text after the cursor.
  • Open new line – Press Enter while in insert mode.
  • Delete a character – Press x.
  • Delete a word – Press dw.
  • Delete a line youngest – dd.
  • Change (replace) a character – c then x.
  • Change a word – cw.
  • Change a line – cc.
  • Undo last operation – u (or Ctrl+ + u).

5. Saving and Exiting

  • Save changes – Press Esc to return to command mode, then type :w and press Enter.
  • Exit vi – Type :q and press *Enter`. This works when no modifications are pending.
  • Save and exit – Type :wq and press *Enter`. This saves the file and quits.
  • Force quit without saving – Type :q! and press *Enter`. Use this only if you are certain you want to discard changes.

Advanced Editing Techniques

Working with Multiple Files

  • Open multiple files – vi file1.txt file2.txt …
  • Switch between open buffers – :bn (next) or :bp (previous).
  • Close a specific buffer – :bw (write and quit) or :bd (delete buffer).

Using Registers and Yanks

  • Copy (yank) text – Move to the start of the text, press v to select visually, then press y to yank into the default register.
  • Paste (put) text – Press p to paste after the cursor or P to paste before.

Marks and Jumping

  • Set a mark – While in command mode, press ma (where a is any letter) at the desired location.
  • Jump to mark – Press 'a to go to that mark.

Using the Ex Command Line

The colon (:) prefix opens the ex command line, which offers many powerful options:

  • Search and replace – :s/old/new/g replaces all occurrences on the current line. Use :1,$s/old/new/g for the whole file.
  • List line numbers – :set nu enables line numbers; :set nonu disables them.
  • Set tab stops – :set ts=4 (or :set ts=8).

Common Issues and Solutions

Problem Likely Cause Quick Fix
Unable to press i Still in command mode Press Esc to ensure you are in command mode, then press i
Changes not saved Forgot to press :w Press :wq or :q! if you want to discard
Screen appears garbled Terminal not in vi mode Press Ctrl+ + ], then run reset
Cannot delete lines Not in command mode Press Esc then try dd

If you frequently work with vi, consider creating a .vimrc file to personalize settings such as indentation, color schemes, and key mappings. A simple configuration might look like:

set number          " Show line numbers
set tabstop=4       " Tab width
set shiftwidth=4    " Indentation width
set expandtab       " Use spaces for tabs

Frequently Asked Questions (FAQ)

Q: Is vi the same as vim?
A: vi originally referred to the original Unix editor. vim (Vi IMproved) is a more feature‑rich version that is often installed by default on modern Linux systems. The commands remain largely compatible.

Q: How can I edit a file as root using vi?
A: Use sudo vi /path/to/file. You will be prompted for your password, and the editor will run with root privileges Still holds up..

**Q: What

Freshly Posted

Just Made It Online

Same World Different Angle

A Bit More for the Road

Thank you for reading about How To Edit Vi Editor In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home