Vi text editing guide

Source: Internet
Author: User
Vi is a powerful text editor that is included in most Linux systems, and even has vi in embedded Linux. Sometimes you have to edit the text on a system without a more friendly text editor, so it is necessary to know more about vi. And Nano

Vi is a powerful text editor that is included in most Linux systems, and even has vi in embedded Linux. Sometimes you have to edit the text on a system without a more friendly text editor, so it is necessary to know more about vi.

Unlike Nano (an easy-to-use Terminal text editor), vi does not stick to it and provides you with a series of shortcut keys on the screen. It is a mode-based text editor that supports both the insert mode and command mode.

Getting started

Vi is a command line (terminal) application, so you need to start it from the terminal window. If you want to open an existing file, you can use commands such as vi/path/to/file. If the file does not exist, the command vi/path/to/file will also work normally. in this case, vi will create a new file and write it to the specified location when you save the file.

If you want to edit a system file, do not forget to use sudo. For example, if you want to edit the fstab file, Type sudo vi/etc/fstab. If the Linux release you are using is not of the Ubuntu series, you may need to use the su command.

Command mode

When you open a file with vi, you will see a page similar to the following. It seems that you can start typing directly, but this is not the case. Vi is a mode text editor. you can open it in command mode. Typing on the current screen may lead to unexpected results.

When vi is in command mode, you can move the cursor with the arrow key. Press the x key to delete a character at the current cursor. There are many other delete commands in vi. for example, if you type dd (press d twice), the entire line of text will be deleted.

In command mode, you can cut or paste text. Move the cursor to the left or right of the text you want to cut or paste, press the v key, and move the cursor to select the text, press y or x to select whether to copy or cut the text. Move the cursor to the desired position and press the p key to paste the text you want to copy and cut.

Insert mode

In addition to the command mode, you need to understand the insert mode, which allows you to insert new text in vi. Once you understand it, it is very easy to insert data-just press the I key after you move the cursor to the desired position. Now, if you start typing, vi will send the characters you typed to the file, instead of trying to interpret them as commands.

Once you complete the work in insert mode, you only need to press ESC to return to the command mode.

Save and exit

You can save or exit vi in command mode. First, press ESC to make sure that you are in command mode. (if you are in command mode, press ESC to avoid any other tasks ).

Type: wq and press enter to write the file to the disk and exit vi. You can also use this command to break it down. for example, enter w and press enter to write the file to the disk but not exit. Then, enter q and press enter to exit vi without saving the file.

If you have modified the file, vi will not allow you to exit without saving it. However, you can enter q! Press enter to ignore this warning.

If you are looking for an easy-to-use command line text editor, consider Nano. Most Linux distributions are preinstalled with Nano, but embedded systems or other customized systems generally only contain vi.

Original article: Chris Hoffman translation: Geeks fan-gossip lovers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.