Guide to vi Editor on Linux


Introduction

The vi editor is one of most popular text editors on Linux. It is a command-line-based editor that comes pre-installed on most Linux distributions. Although it may seem daunting to new users, it is a powerful and efficient tool for editing text files. In this guide, we will cover basics of using vi editor on Linux.

Opening vi Editor

To open vi editor, open a terminal window and type "vi" followed by name of file you want to edit. For example, to edit a file called "example.txt", type −

vi example.txt

If file does not exist, vi will create a new file with that name. Once you have opened a file in vi, you will see contents of file on screen.

Modes in vi

One of most important concepts to understand when using vi is concept of modes. vi has two modes: command mode and insert mode.

Command Mode

When you first open a file in vi, you are in command mode. In command mode, you can use various keyboard commands to navigate file and perform various editing functions. Here are some of most commonly used commands in command mode −

  • h − Move cursor left

  • j − Move cursor down

  • k − Move cursor up

  • l − Move cursor right

  • i − Switch to insert mode

  • x − Delete character under cursor

  • dd − Delete current line

  • :w − Save file

  • :q − Quit vi

  • :q! − Quit vi without saving changes

Insert Mode

Insert mode is used to enter text into file. To switch to insert mode from command mode, press "i" key. In insert mode, you can enter text just like you would in any other text editor. To return to command mode, press "Esc" key.

Editing Text

To edit text in vi, you must first be in command mode. Then, use various commands available in command mode to make changes to text. For example, to delete a line of text, position cursor on line you want to delete and type "dd". To delete a single character, position cursor on character and type "x".

Moving Cursor

    One of most important functions of vi is navigating text using keyboard. Here are some of most commonly used commands for moving cursor −

  • h − Move cursor left

  • j − Move cursor down

  • k − Move cursor up

  • l − Move cursor right

  • 0 − Move cursor to beginning of line

  • $ − Move cursor to end of line

  • G − Move cursor to end of file

  • gg − Move cursor to beginning of file

Searching for Text

To search for text in vi, you must be in command mode. Then, type "/searchterm" and press Enter. vi will search for first instance of "searchterm" in file and position cursor on it. To search for next instance of search term, type "n". To search for previous instance, type "N".

Replacing Text

To replace text in vi, you must be in command mode. Position cursor on character you want to replace and type "r" followed by replacement character. To replace a word, position cursor on first character of word and type "cw" followed by replacement text.

Saving and Quitting

To save changes to a file in vi, you must be in command mode. Type ":w" and press Enter. To quit vi, type ":q". If you have made

If you have made changes to file and want to save them before quitting, type ":wq" and press Enter. If you want to quit without saving changes, type ":q!" and press Enter.

Using vi Editor with Multiple Files

It is possible to edit multiple files in vi by opening them in separate tabs or windows. To open a new tab, type ":tabnew" followed by name of file you want to open. To switch between tabs, type ":tabnext" to go to next tab or ":tabprev" to go to previous tab.

Customizing vi Editor

Vi editor provides several customization options that can be used to make your editing experience more comfortable. For example, you can change color scheme of editor, set up automatic indentation, and configure syntax highlighting.

To customize editor, you can edit .vimrc file located in your home directory. This file contains configuration options for vi editor. To edit file, use following command −

vi ~/.vimrc

In .vimrc file, you can add configuration options by typing them in. For example, to change color scheme of editor, you can add following line −

colorscheme desert

This will change color scheme to desert. You can also add other configuration options, such as auto-indentation or syntax highlighting.

Using vi with Other Programs

Vi editor can be used in conjunction with other programs to make editing more efficient. For example, you can use command line tool grep to search for text in multiple files, and then use vi to edit files.

To use grep with vi, use following command −

grep "searchterm" *.txt | vi -

This will search for "searchterm" in all text files in current directory, and then open them in vi for editing.

You can also use vi to edit files remotely over SSH. To do this, use following command −

ssh user@hostname vi filename

This will open file "filename" on remote server and allow you to edit it using vi.

Conclusion

The vi editor is a powerful tool for editing text files on Linux. Although it may seem intimidating at first, with practice it can become a fast and efficient way to edit text files. By mastering basic commands and concepts outlined in this guide, you can become proficient in using vi editor and take your Linux editing skills to next level.

Updated on: 24-Mar-2023

390 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements