Learning VI and VIM editor (12): Advanced editing Methods (3)

Source: Internet
Author: User
Tags tag name

This chapter focuses on the use of the @ feature in the VI editor, the ex script, and the Ctags tool.

function

A named buffer provides another way to create a "macro" that allows you to save a complex sequence of commands into a named buffer and then execute the contents of the buffer through the @ command. as an example, first enter the following line in insert mode:


the row contents are then deleted and saved to the "a" named buffer, which can then be move the cursor to the header of a word, execute "@a", and you can execute the contents of the buffer as a command. In this example, the word is modified to function, while still in command mode. because the @ command is interpreted as a VI command , you can use the dot (.) to repeat the command regardless of what command is stored in the buffer. "@@" repeats the last @ operation.

Using the ex script:

The ex command can be stored in a ". EXRC" file so that these ex commands are automatically executed when the VI editor is opened. Alternatively, you can store the ex command in a different file and then execute the script by reading the script file with ": So ScriptName" When you open the VI editor. As an example, the "Substitute.ex" script has the following content:


After opening a file with the VI editor, you can execute the ex script by executing the following command:


You can also work directly on the file on the Unix command line, where you need to use the redirection function of Unix: " Ex-filename < ScriptName", This allows the ScriptName script to function on the filename file. For example:


Of course we can also write a shell script that contains the call to the ex script, which allows us to make more flexible changes to the file. For example, call the "Substitute.ex" script in a shell script:


Here Document:

in shell script, the operator << represents the input of the following line to a string as a command, also known as here document. as shown in this syntax, the ex command can be written directly in the shell script:


The "End_of_script" string here can be any string, as long as the string does not appear elsewhere in the input text, allowing you to discern where the here document ends. It is necessary to note that it is more flexible to write the edit command to a file other than the shell script compared to here document.

comments in the ex script: In an ex script, the text after any double quotation marks is ignored when executed, so the double quotation marks can be used as the beginning of the comment.

In addition to the ex Editor, Unix offers an editor that is more powerful than ex: the SED flow editor and the awk data manipulation language, which provide more powerful editing capabilities. Of course, this is the future direction of learning, is still solid to learn a good VI editor.

To edit the program source code:

The VI Editor provides a lot of powerful features for programmers, including indentation control, the start and end of the search process, and the use of ctags.

Indentation Control:

If you need to use automatic indentation in VI, you need to set the following options: " : Set Autoindent ". This way, when a space or tab is used to indent, subsequent rows are automatically indented at the same distance. and when you enter code in the case of auto-indentation, pressing "ctrl+t" at the beginning of a line causes the current row to go to the next level of indentation, while pressing "ctrl+d" returns to the previous level of indentation. It is important to note that "ctrl+t" and "ctrl+d" are used in insert mode.

the << and >> commands can also be used to indent code. >> defaults to moving the code to the right by 8 spaces, while << defaults to moving the code 8 spaces to the left. The number of spaces that are moved by default can be changed by the shiftwidth option, such as setting the number of spaces moved to 4 with ": Set shitwidth=4". And You can move multiple lines at the same time by adding a number to the "<<" or ">>" front.

When we indent the code, the space bar is blank on the screen because of the TAB key, and it's not easy to see if there are any spaces at the end of the trip, so you can change the state of the display by using the list option, as the previous article has already said. The same effect can be achieved with the ": L" command.

A special Search command:

"(,[,{,<" can be called open brackets, when the cursor is on any kind of opening brackets, press the "%" key, you can move the cursor to a pair of closed parentheses.) Similarly, when the cursor is over a closed parenthesis, pressing the "%" key will return to the corresponding opening parenthesis. when the cursor is not on the bracket character, when the "%" key is pressed, VI will look for the first opening or closing parenthesis in the current line, and then move the cursor to the parentheses corresponding to the parentheses.

Using the tag (tag):

The ctags command on UNIX can produce an information file that the VI Editor can use to determine which functions are defined separately in each source file. By default, the file name of this information file is tags. using the " ctags" command in Unix with the ": Tag" command in VI, you can quickly move the cursor to a label. first create the appropriate tags file for the program source file using the "ctags" command on the UNIX command line, and then use the ": Tag name" command in the VI editor to move the cursor to the line where the name function is located.

in command mode, you can move the cursor to the identifier you want to find, and then enter "ctrl+[", or the cursor to the line where the identifier is located. It is important to note that VI looks for a word that starts at the current cursor position, not the complete word that contains the cursor. and If the found label is in a new file, but the current file is not saved, VI will not let you read the new file. You must first save the current file with the ": w" command, or you can pass ": tag! Name "to force VI to discard the editing results.

about the ctags command follow-up articles will also continue to learn, here is just a simple use of learning ctags.


This chapter VI command summarizes:

"@", "@@", "Ctrl + T", "Ctrl + D", "<<", ">>", ": L", "%", ": Tag name", "Ctrl + [", ": tag! Name ".



Learning VI and VIM editor (12): Advanced editing Methods (3)

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.