Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you’ll have to edit a text file on a system that doesn’t include a friendlier text editor, so knowing Vi is essential.
Unlike Nano, an easy-to-use terminal text editor, Vi doesn’t hold your hand and provide a list of keyboard shortcuts on the screen. It’s a modal text editor, and it has both an insert and command mode.
Getting Started
Vi is a terminal application, so you’ll have to start it from a terminal window. Use the vi /path/to/file command to open an existing file with Vi. The vi /path/to/file command also works if the file doesn’t exist yet; Vi will create a new file and write it to the specified location when you save.
Remember to use sudo if you want to edit a system file. So, for example, you’d type sudo vi /etc/fstab if you wanted to edit your fstab file. Use the su command instead if you’re using a non-Ubuntu version of Linux that doesn’t use sudo.
Command Mode
This is what you’ll see when you open a file in vi. It looks like you can just start typing, but you can’t. Vi is a modal text editor, and it opens in command mode. Trying to type at this screen will result in unexpected behavior.
While in command mode, you can move the cursor around with the arrow keys. Press the x key to delete the character under the cursor. There are a variety of other delete commands — 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. Position the cursor at the left or right side of the text you want to copy and press the v key. Move your cursor to select text, and then press y to copy the selected text or x to cut it. Position your cursor at the desired location and press the p key to paste the text you copied or cut.
Insert 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 i key once after you’ve positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands.
Once you’re done in insert mode, press the escape key to return to command mode.
Saving and Quitting
You can save and quit vi from command mode. First, ensure you’re in command mode by pressing the escape key (pressing the escape key again does nothing if you’re already in command mode.)
Type :wq and press enter to write the file to disk and quit vi. You can also split this command up — for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.
Vi won’t let you quit if you’ve modified the file since you last saved, but you can type :q! and press enter to ignore this warning.
Check out Nano if you’re looking for an easier-to-use terminal text editor. Most Linux distributions come with Nano installed, but embedded systems and other stripped-down environments often only include Vi.