Vi and vim Editor (10): advanced editing method (1)

Source: Internet
Author: User

Vi and vim Editor (10): advanced editing method (1)

Custom vi:

There are many options that can be set in the vi Editor, which will affect the operations of the vi editor.

You can use the ex command ": set" to change the options in vi. In addition, as long as the vi is opened, the ". exrc" file located in your main directory will be read to get the relevant settings of vi. Use the ": set" command in this file to change the operating mode of vi. In addition, you can create a ". exrc" file in the current directory to use different options in different directories to initialize vi. But first, the ". exrc" file in the main directory is executed first, and then the ". exrc" file in the current directory. In addition, any commands stored in the environment variable EXINIT will be executed when vi is enabled. The settings in EXINIT are executed first than the ". exrc" file in the main directory.

: Set command:The ": set" command can change two types of options: one is the switch option, which can only be enabled or disabled, and the other can accept the value or string value. To enable a switch option, use ": set option". To disable a switch option, use ": set nooption ". You can use the equal sign (=) to set some options. ": Set all" will display a complete list of options, including the user's set value and default value selected by vi. Pass ": set option? "To view the current value of an option. The command ": set" is used to display specially changed or set options. These options can be set in the ". exrc" file or in the current editing session.

. Exrc file:The ". exrc" file that controls the vi environment is located in your home directory ). If the ". exrc" file does not exist in the main directory, you can create it using vi. You can ". in the exrc file, write the command to set the vi option, and the file is actually read by ex, so in ". you do not need to add a colon Before the command entered in the exrc file. As shown in, set the tab key to four spaces:

In addition to reading the ". exrc" file in the main directory, vi can also read the ". exrc" file in the current directory, so that different options can be set under different directories. However, before vi reads the ". exrc" file in the current directory, you must first set the exrc option in the ". exrc" file in your home directory, that is, set exrc.

You can also save the option settings to a file other than ". exrc" and read it with the ": so" command (so is the abbreviation of source ).

Some useful options:There are many options that can be set in the vi Editor, many of which are used internally by vi and do not need to be modified. Here are several useful options:

(1) The wrapmargin option is used to specify the distance from the end of the row to the right boundary, so that when the input line exceeds this limit, it can automatically wrap (only wrap in the blank space ). However, in actual testing, the wrapmargin option is not very useful. If you want to set the width of a row, you can also use the textwidth option;

(2) There are three other options to control the vi search action, such as whether to ignore the case when searching, whether to return to the beginning of the file to continue searching, and whether to identify wildcards during pattern matching, the default values of these three options are noignorecase, wrapscan, and magic;

(3) options that programmers may be interested in: autoindent (Auto indent), showmatch (matching of parentheses), tabstop (the tab key occupies several spaces), number (display row number ), list (print the tab key as ^ I and mark it as $ at the end of the line );

(4) autowrite option: When this option is set, if you issue the ": n" command to switch to the next file, or use ":! "To execute shell commands, vi will automatically write the changed buffer content to the disk.

Run the UNIX command:

When using vi, you can display or read the results produced by any UNIX Command. The exclamation point tells ex to create a shell and regards subsequent text as a UNIX Command, that is ":! Command ". If you want to execute a series of commands without returning to vi, you can use ":! Sh "to create a shell, and then use CTRL + D to end the shell and return to vi.

": Read" can be used in combination with UNIX commands to read the execution results of UNIX commands into files. Add the line address before the ": read" command to insert the execution result of the command in the next line of the specified line. Otherwise, it is inserted to the next line of the current line by default. As shown in, read the current path and insert it into the file:

Filter text by command:

Text blocks can also be used as standard input for UNIX commands. The output of this command replaces the original text in the buffer zone. Therefore, you can use commands in ex or vi to filter text. Ex Specifies text blocks through line addresses, while vi specifies text blocks through text objects (mobile commands.

Filter text with ex:Sort the first five rows of the file, as shown in:

Use vi to filter text:The command format for filtering text with vi is "! Textobject command ". Therefore, if the current cursor is in the first line of the file, you can enter "! 4j sort "to sort the first five rows of files. Note that when you enter an exclamation point, the vi editor does not display any content. After you enter "4j", the vi editor displays the following content:

Then enter the sort command. We can find that using vi to filter text still uses ex to filter text (the bottom-layer editor used by vi editor is the ex row editor ). In addition to this command, you can also use "! 5! Sort ", where the second exclamation point represents the meaning of the current row, and the previous number represents the meaning of repetition.

Summary of the vi commands in this chapter:

": Set option", ": set nooption", ": set all", ": set option? ",": Set ", wrapmargin option, textwidth option, ignorecase option, wrapscan option, magic option, autoindent option, showmatch option, tabstop option, number option, list option, autowrite option, ":! Command ",": r! Command ",": linenum, linenum! Command ","! Textobject command. "

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.