Open In App

Basic vi Commands

Last Updated : 02 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Text editors are essential tools for any programmer or computer enthusiast. One popular text editor that has been around for decades and is still widely used is vi (pronounced “vee-eye”). vi is a terminal-based text editor that offers powerful editing capabilities once you understand its basic commands.

In this article, we will explore the fundamental vi commands that will help you navigate, edit, and save your text files efficiently. Whether you’re a beginner or an experienced user looking to refresh your knowledge, this guide will provide you with a solid foundation in using vi.

VI COMMANDS

Below is a list of some of the most used VI commands. To start using vi, open the terminal and type the below line:

vi filename

Here “filename” is the name of the file that you want to edit. Suppose the file is GeeksForGeeks. The command will look something like this :

vi GeeksForGeeks.txt
(here I am using a txt file) 

2023-09-16-19_01_16-ubuntu-[Running]---Oracle-VM-VirtualBox

After opening the file it would look something like this :

2023-09-16-19_02_57-ubuntu-[Running]---Oracle-VM-VirtualBox

Modes in the Vi text editor

There are three modes in the vi text editor, they are :

  1. Normal
  2. Insert Mode
  3. Visual Mode

Normal Mode:

Normal Mode is the default mode when you open a file. In this mode, you can navigate and perform various operations on text.

Insert Mode:

To enter Insert Mode and start typing or editing text, press i (for insert before the cursor), I (for insert at the beginning of the line), a (for insert after the cursor), or A (for insert at the end of the line).

2023-09-16-19_09_11-ubuntu-[Running]---Oracle-VM-VirtualBox

Visual Mode

Visual Mode allows you to select text. You can enter Visual Mode by pressing v.

# In Normal Mode, press 'v' to enter Visual Mode.
This is some text you can select.

Navigation in VI:

1. Moving the Cursor:

In Normal Mode, you can move the cursor using the arrow keys or the h, j, k, and l keys, which correspond to left, down, up, and right, respectively.

2. Jumping to the Beginning or End of the Line:

Use 0 to go to the beginning of a line and $ to jump to the end.

# In Normal Mode, '0' takes you to the start, and '$' to the end.

This is the start of the line.                   $ This is the end of the line.

3. Jumping to Specific Lines:

To move to a particular line, type :n, where n is the line number.

# In Normal Mode, ':n' takes you to line number 'n'.

This is line 1.
This is line 2.
This is line 3.

To go to line 2, you’d type :2 and press Enter.

Editing Text

1. Inserting Text:

In Insert Mode, type your text as needed.

# In Insert Mode, type your text.

This is some text you can edit. [You are in Insert Mode]

2. Deleting Text:

In Normal Mode, use x to delete the character under the cursor, dd to delete the entire line, or D to delete from the cursor position to the end of the line.

# In Normal Mode, 'x' deletes the character under the cursor.
This is some t xt you can edit.  [Cursor on 'e']


# 'dd' deletes the entire line.
This line will be deleted.


# 'D' deletes from the cursor position to the end of the line.
This line is deleted. [Cursor on 'i']

3. Copying and Pasting:

To copy text, use yy. To paste, press p to paste below the cursor or P to paste above the cursor.

# In Normal Mode, 'yy' copies the current line.
This is a line to copy.


# 'p' pastes the copied line below the cursor.
This is a line to copy.

This is the pasted line.


# 'P' pastes the copied line above the cursor.
This is the pasted line.

This is a line to copy.

Saving and Quitting

Saving:

To save your changes, press : in Normal Mode, followed by w and then press Enter. Alternatively, you can use :w filename to save the file with a different name.

# In Normal Mode, ':w' saves the file.
:w


# ':w filename' saves the file with a new name.
:w newfilename.txt

Quitting:

To exit vi, type :q and press Enter. If you’ve made changes and want to save them while quitting, use :wq. If you want to quit without saving changes, use :q!.

# In Normal Mode, ':q' quits vi.
:q


# ':wq' saves changes and quits.
:wq


# ':q!' quits without saving changes.
:q!

Searching

Searching for Text:

In Normal Mode, press / followed by the text you want to search for. Press Enter to find the next occurrence, or n to repeat the search in the forward direction.

# In Normal Mode, '/' initiates a search.
/SearchText


# Press 'Enter' to find the next occurrence.
# Press 'n' to repeat the search.

Undo and Redo

Undoing:

To undo your last action, press u in Normal Mode.

# In Normal Mode, 'u' undoes the last action.

This is some text. [Undoing last action]

Redoing:

To redo an undone action, press Ctrl + r.

# In Normal Mode, 'Ctrl' + 'r' redoes an undone action.

This is some text. [Redoing last undone action].

FAQs

1. How do I copy and paste text between different vi instances or terminals?

Ans. To copy text between different vi instances or terminals, you can use the “+y command in Normal Mode to copy text to the system clipboard. Then, you can paste it into another vi instance or terminal using the system’s standard paste command (usually Ctrl+Shift+V or right-click and select Paste).

2. Can I customize vi to use different syntax highlighting for specific file types?

Ans. Yes, you can customize vi to apply syntax highlighting for specific file types by creating a .vimrc file in your home directory and adding configuration settings. For example, you can set up syntax highlighting for programming languages like Python, JavaScript, or HTML by specifying the appropriate settings in your .vimrc file.

3. How can I record and replay macros in vi to automate repetitive tasks?

Ans. Vi allows you to record and replay macros to automate repetitive tasks. To record a macro, press q followed by a letter (e.g., q a) to start recording into the specified register (e.g., register ‘a’). Perform your desired actions, then press q again to stop recording. To replay the macro, type @a (replace ‘a’ with the register you used) in Normal Mode.

4. What is the vi recovery mode, and how do I use it if vi crashes or is interrupted?

Ans. Vi has a recovery mode that helps you restore unsaved changes if vi crashes or is interrupted. When you restart vi and it detects an interrupted session, it will prompt you to recover the unsaved changes. You can follow the on-screen instructions to recover your work.

5. Is there a way to open and edit files over SSH using vi?

Ans. Yes, you can open and edit files over SSH using vi. Simply use the ssh command to connect to the remote server, and then use vi followed by the file path on the remote server to open and edit the file. For example, ssh user@remote_server and then vi /path/to/remote_file.

Conclusion

In conclusion, vi is a versatile and powerful text editor that has stood the test of time. Understanding its basic commands is essential for efficient text editing in a terminal environment.

As you become more familiar with vi, you’ll discover its efficiency and flexibility in handling text-based tasks. It’s a valuable tool for programmers, system administrators, and anyone who works extensively in a terminal. Practice these basic commands, and as you gain experience, you’ll unlock the full potential of vi for text editing and manipulation.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads