Quick learn Vi Editor

Source: Internet
Author: User
Tags expression engine

Configuration files and shell script files are both text files, which must be edited using a text editor. In Linux, there are many types of text editors, including the character interface vi and emacs, and the graphic interface gedit under the "attachment" menu item) ", you can choose to use according to your preferences, but for users who learn to use the Linux system, the school vi editor is generally used.

Editor vi allows you to edit various unformatted text files, especially for source programs. It provides many basic text operations such as input, search, replacement, deletion, and block operations, and can be customized to suit your own needs. In vim, there are also some enhanced functions, such as Undo multi-line input, highlighted parameter display, command line editing, multi-window support for graphical vi, and multi-buffer.

Because vi is an interactive text editor, it has no menus, only commands, and many commands. It is not used by many beginners, however, after learning how to use it, you will soon love it. There are three working modes in vi: text editing mode, command mode, and line editing mode. The text editing mode must work in the Insert state, while the line editing mode is actually a command mode, except that it needs to input ": "and then enter the command. You can use some commands

And most commands are the first letter of the English word in the operation.

In addition, vi is just a text editor. It is not a typographical software, so it cannot edit the font, format, paragraph, and other attributes of a document like word, this is determined by its purpose. Modifications made to the text in vi will be placed in the memory buffer. As long as the user does not enter the storage command, they can be discarded at any time. Generally, vi does not automatically save the backup file when saving the file, but when opening the file, the Linux system will automatically create the file extension in the directory related to the disk. swp file, also known as swap

File. If the file is used as a backup file for some reason, such as editor crash, system power failure, or illegal shutdown, when you start the system next time, you will receive an email in your home directory. The user determines whether to restore the file by running a command.

Recommended reading:

  • Powerful Vim Editor
  • Build a Vim Development Environment on CentOS 6.2
  • Vim 7.4a released, a new and faster Regular Expression Engine
  • Install the highlighted Vim editing tool in CentOS 5.4
  • Vim tips: C language settings
  • Set the Vim row number in Ubuntu

Basic commands

In the command mode of the vi editor, the command format is nnc. Specifically, the character c is the command, and the nn is the integer, which indicates that the command will be repeatedly executed nn times. If the nn value of the number of repetitions is not given, the command will be executed only once. For example, if you press the j key in command mode, the cursor moves down one line, and if you enter 10 j in command mode, the cursor moves down 10 lines. Obviously, this is more efficient than simply moving the keyboard cursor key.

1. Position the cursor

Because the insert, delete, and replace operations on the file content are for the characters or lines where the cursor is located, you should be familiar with the commands and methods for moving the cursor, make full use of the number of times that commands are repeatedly executed.

W move the cursor to the next 1st characters.

W move the cursor to the 2nd characters of the last 1st words

E or E move the cursor to the last 1 character of the next word

B or B move the cursor to the first 1st characters in the word

/String move the cursor backward and place it at the first character of string

? Move the cursor forward to the string and place it at the first character of the string.

$ Move the cursor over the last 1 character of the line

Ctrl + B move the cursor to the row's 1st characters

The difference between the w and W commands is that the non-space characters are separated. The w command regards non-space characters as words, while the W command ignores them. When/string or? In the string command, in addition to moving the cursor to the specified position, it also highlights all the matching strings in the file.

2. insert or add

Both insert and add Add add new content to the edited file, but because insert can be before or before the cursor, add can be after the cursor or after the current row, therefore, vi provides multiple insert and add commands:

I insert characters before the cursor

I insert characters in the row header of the current row

A. Add characters after the cursor

A adds characters at the end of the row of the current row

O Add a new row after the current row

O Add a new row before the current row

Since these six commands will switch the vi editor from command mode to the Insert state in text editing mode, after the insertion or addition is complete, you must press ESC to return to command mode.

3. Delete

You can delete the content in a text file by characters, words, and unit of action. Therefore, there are multiple delete commands. After the command is deleted, the vi will remain in command mode.

X deletes the character at which the cursor is located and moves the cursor to the next character in the row.

X deletes the first character of the cursor position, and the cursor stays on the original character

D. Delete All characters from the cursor position to the end of the row, and the cursor stays at the end of the row.

Press twice d to delete the row where the cursor is located and move the cursor to the next row

D first and w later Delete All characters between the cursor position and the first character of the next word, and

Move the cursor to the first character of the next word.

4. Search

When searching strings in a file, you can either search forward or backward. Therefore, there are two commands in the format:

/Expression

? Expression

Here, the slash (/) indicates to search backward from the current position, question mark (?) Forward lookup from the current position. expression is a regular expression. If the regular expression is a string and contains:. ^ $ /~ Special characters (metacharacters), when you look for strings containing these special characters, you must add a backslash (\) character before each of them to indicate escape.

Next, let's take a look at the highlights of page 2nd:

  • 1
  • 2
  • Next Page

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.