How to use the Vi text editor, here are the detailed steps

Mondo Digital Updated on 2024-01-31

VI is a powerful text editor that is included in most Linux systems, even embedded systems. Sometimes, you have to edit text files on a system that doesn't include a more user-friendly text editor, so it's crucial to understand VI.

Unlike Nano, which is an easy-to-use text editor for the terminal, the VI does not provide a list of keyboard shortcuts on the screen. It is a modal text editor that has both insert and command modes.

vi is a terminal application, so you have to launch it from the terminal window. Use the vi path file command to open an existing file with vi. If the file does not already exist, the vi path file command can also be used;vi will create a new file and write it to the specified location when saving.

If you want to edit system files, remember to use sudo. So, for example, if you want to edit your fstab file, you can type sudo vi etc fstab. If you are using a non-Ubuntu version of Linux that does not use sudo, use the su command.

This is what you will see when you open a file in VI. It looks like you can start typing, but you can't. vi is a modal text editor that opens in command mode. Attempting to type on this screen will result in unexpected results.

In command mode, you can use the arrow keys to move the cursor. Press the X key to delete the characters under the cursor. There are a variety of other delete commands, for example, typing dd (pressing 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 copy and press the V key. Move the cursor to select the text, and press y to copy the selected text or x to cut the text. Position the cursor where you want it and press the P key to paste the copied or cut text.

In addition to the command mode, another mode you need to know about is the insertion mode, which allows you to insert text in vi. Once you know that the insert mode exists, it's easy to get into the insert mode by pressing the i key once after positioning the cursor to the command mode. Start typing, and vi will insert the characters you typed into the file instead of trying to interpret them as commands.

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

You can save vi and exit from command mode. First, press the escape key to make sure you're in command mode (if you're already in command mode, pressing the escape key again won't do anything.) )

Type: wq and press Enter to write the file to disk and exit VI. You can also split this command, for example, typing :w and pressing enter to write the file to disk without exiting, or typing :q and exiting vi without saving the file.

If you've modified the file since your last save, vi won't let you quit, but you can type :q!and press Enter to ignore this warning.

Premium short** plan

Related Pages