Vi Editor: The Linux Trick Few Know About! Discover It! 🔥

11 Vim Tips That Will Save You Hours of Editing Time

Vi Editor: Master it in 3 easy, stress-free steps! 🚀

Vi is a powerful text editor that comes included with most Linux systems, even embedded ones. Sometimes, you'll need to edit a text file on a system that doesn't have a more user-friendly text editor, so knowing Vi is key. ✍️

Unlike Nano, a simpler terminal text editor, Vi doesn't hand you a list of shortcuts. on-screen keyboard. It is a modal text editor, meaning it has both an insert mode and a command mode.

Getting Started with Vi

Vi is a terminal application, so you'll need to launch it from a terminal window. Use the command I saw /path/to/file to open an existing file with Vi. The command I saw /path/to/file also works if the file doesn't exist yet; Vi will create a new file and save it to the specified location when you're done. 💾

Ubuntu terminal with the vi command being used to open a new file.

Remember use sudo if you want to edit a file of the system. So, for example, you would have to write sudo vi /etc/fstab if you want to edit your fstab file.

Command Mode

This is what you'll see when you open a file in vi. It looks like you can only start typing, but that's not the case. Vi is a modal text editor, and it opens in command mode. Trying to type in this screen will give you unexpected behavior. ⚠️

A file opened in the vi text editor in an Ubuntu terminal window.

While in command mode, you can move the cursor with the arrow keys. Press the key x to delete the character under the cursor. There are several other commands for deleting; for example, typing dd (press the d key twice) deletes an entire line of text. 🗑️

You can select, copy, cut, and paste text in command mode. Place the cursor to the left or right of the text you want to select. you want to copy and press the key v. Move the cursor to select the text, and then press and to copy the selected text or x to cut it. Place the cursor in the desired location and press the key p to paste the text you copied or cut.

Selected text at the end of a file opened in the vi text editor.

Insertion Mode

Aside from command mode, the other mode you need to know about is insert mode, which allows you to insert text in Vi. Entering insert mode is easy once you know it exists: just press the key Yo once after you've positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file instead of trying to interpret them as commands. 📝

Vi text editor with text indicating that text was added by pressing the "i" key first.

Once you're done in insert mode, press the escape key to return to command mode.

Save and Exit

You can save and exit vi from command mode. First, make sure you're in command mode by pressing the Escape key. (Pressing the Escape key again doesn't do anything if you're already in command mode.)

Writes :wq and press Enter to save the file to disk and exit vi. You can also split this command; for example, type :w and press enter to save the file to disk without exiting, or type :q to exit vi without saving the file.

Vi text editor with the "wq" command written at the bottom of the window.


Vi text editor with error stating "Cannot write since last change" at the bottom of the window.


If you're looking for a more user-friendly terminal text editor, check out Nano. Most Linux distributions come with Elder brother preinstalled, but embedded systems and other small environments often only include Vi. 🖥️

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most voted
Online Comments
See all comments