Learning VI and VIM editor (10): Advanced editing Methods (1)

Source: Internet
Author: User
Tags line editor

The advanced features of some of the VI and ex editors are described in this chapter. This article mainly describes how to customize the editing environment, how to execute UNIX commands in VI, and how to filter text in VI with UNIX commands.

Custom VI:

There are many options that can be set in the VI editor, which will affect the operation of the VI editor.

you can use the EX command ": Set" to change options in VI. Also, as long as VI is open, the ". Exrc" file located in your home directory is read to get the relevant settings for VI. Using the ": Set" command in this file, you can change the way VI works. In addition, you can create a ". Exrc" file in the current directory to initialize VI with different options in different directories. but first, the ". Exrc" file in the home directory is executed first, then the ". Exrc" file for the current directory. In addition, any commands stored in the environment variable exinit will be executed when VI is opened. The settings in Exinit are performed first in the ". Exrc" file in the home directory.

: Set command: ": Set "command can change two types of options: One is the toggle option, you can only choose to turn on and off, and another acceptable numeric or string value. To turn a toggle option on, using ": Set option", to turn off a toggle option, use ": Set Nooption ". through ": Set All" displays a complete list of options, including the user's setting values and the default values chosen by VI. Pass ": Set option?" To see the current value of an option. Command ": Set" is used to display options that are specifically changed or set, which can be in the. EXRC "file or an option that has been set in the current edit session.

. exrc file: The ". Exrc" file that controls the VI environment is located in your home directory (home directory). If the ". Exrc" file is not in the home directory, you can create it using VI. The command to set the VI option can be written in the ". Exrc" file, and the file is actually read in by ex, so the command entered in the ". Exrc" file does not need a colon before. as shown, set the TAB key to 4 spaces:


VI In addition to reading the ". Exrc" file in the home directory, you can also read the ". Exrc" file in the current directory so that you can set different options under different directories. but before VI reads the ". EXRC" file in the current directory, you first need to set the EXRC option in the ". Exrc" file in your home directory, which is set EXRC.

You can also save the option settings to a file other than ". EXRC" and use the ": So" command to read in (so is the abbreviation for source).

Some useful options: There are many options that can be set in the VI editor, many of which are for internal use within VI, and usually do not need to be modified. Here are a few useful options:

(1) Option Wrapmargin is used to specify the distance from the end of the line to the right edge, so that when the input line exceeds the limit, the line can be wrapped (wrapping only in white space). However, the feeling Wrapmargin option is not very useful when actually testing. If you want to set the width of a row, you can also use the textwidth option;

(2) There are three additional options to control the VI search action, such as whether to ignore the case when searching, whether to go back to the beginning of the file to continue the search, pattern matching when the wildcard characters are identified, the default values of the three options are noignorecase,wrapscan,magic;

(3) Options that the programmer may be interested in: autoindent (auto indent), Showmatch (matching brackets), TabStop (tab key for several spaces), number (display line numbers), List (tab is printed as ^i, end of line is labeled $, etc.);

(4) autowrite option: When setting this option, release the ": N" command to switch to the next file, or use ":!" "To execute a shell command, VI automatically writes the changed buffer contents to disk.

Execute UNIX command:

When you use VI, you can display or read the results from any UNIX command. The exclamation point tells ex to create a shell and treats the subsequent text as a Unix command, which is ":!command". If you want to execute a series of commands and don't want to go back to VI halfway, you can use ":!sh" to create a shell and then end the shell with Ctrl+d and go back to VI.

The ": Read" command can be used in conjunction with UNIX commands to read the execution results of UNIX commands into a file. With the line address in front of the ": Read" command, you can insert the execution result of the command on the next line of the specified row, otherwise the default is to insert the next line in the current row. As shown, read the current path and insert it into the file:


To filter text by command:

text blocks can also be used as standard input for UNIX commands, and the output of this command replaces the original text in the buffer. the text can therefore be filtered by commands in Ex or VI. Ex is specifying a block of text through a row address, while VI specifies a block of text through a text object (a move command).

filter Text with ex: sort the first 5 lines of the file as shown:


filter Text with VI: The command form for filtering text with VI is "!textobject command". so if the current cursor is in the first line of the file, you can type "!4j sort" to sort the first 5 rows of the file. It is important to note that when you enter an exclamation point, the VI Editor does not display anything, and when you enter "4j", the VI editor displays the following:


Then enter the sort command. It can be found that filter text with VI in fact, or through the ex to filter the text (vi editor using the bottom editor is the ex Line editor). In addition to using this command, you can also use "!5!sort", where the second exclamation point represents the meaning of the current line, and the preceding number represents the meaning of repetition.


This chapter VI command summarizes:

": Set Option", ": Set Nooption", ": Set All", ": Set option?", ": Set", wrapmargin option, textwidth option, ignorecase option, Wrapscan option, Magic options, autoindent option, showmatch option, tabstop option, Number option, List option, Autowrite option, ":!command", ": R!command", ": LineNum, LineNum!command ","!textobject command. "



Learning VI and VIM editor (10): Advanced editing Methods (1)

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.