Tips for using the vi Editor: Ten awesome commands (1)

Source: Internet
Author: User

When using the vi Editor-whether it's a first-time user or experienced user-most people tend to only master the Core Command set and these commands can execute the most common functions: navigate or save the file; insert, update, delete, or search for data; exit but do not save the modification.

However, the vi editor is extremely powerful with rich features and functions. Even after using vi for years, you may still find new unknown commands. The commands discussed in this article are not very well-known commands, but they can simplify your current operation methods and make your work more efficient, or, you can perform operations that you did not know you could do with vi.

Open and Close row numbers

Many options in the vi editor can control the appearance and feeling of editing sessions. Use the: set command to modify the session settings in vi. After you press the Escape key to enter the command mode, you can use the: set all command to display the list of options and settings.

One of the options that can be set is number. Its function is to open and close the row number, as shown in Listing 1 ).

Listing 1. Before turning on the row number # Internet host table #: 1 localhost127.0.0.1 localhost loghost192.168.0.6 centos5192.168.0.10 appserv192.168.0.11 webserv192.168.0.12 test192.168.0.5 solaris10 # Added by DHCP ~~~ : Set number

This command allows vi to display the row number on each record of the file being edited. After entering the vi command mode, you can enter: set number and press enter to open the line number, as shown in Listing 2 ).

Listing 2. 1 #2 # Internet host table3 #4: 1 localhost5 127.0.0.1 localhost loghost6 192.168.0.6 centos57 192.168.0.10 appserv8 192.168.0.11 webserv9 192.168.0.12 test10 192.168.0.5 solaris10 # Added by DHCP ~~~ : Set number

You can use the: set nonumber command to close the row number. You can also use this command and the abbreviation of: set number, that is, set nu and: set nonu.

To quickly calculate the number of rows to be processed using the vi function, it is very helpful to display the row number. The row number is particularly useful when there are many rows that may span multiple screens. In addition, sometimes you know the row range to be processed, but you need to find out the initial and end line numbers to be used in the vi command.

If you want to display the row number each time you enter the vi Session, add the set number row to the. exrc file in the main directory.

Auto indent

Indentation is an important part of a style when writing code in some programming languages, ensuring better code readability. If necessary, you can set Automatic indent in the vi editor based on the style of the programming language. Use autoindent to enable or disable auto indent, as shown in listing 3 ).

Listing 3. Enable auto indent #! /Bin/ksh # for file in/etc/* doif [[-f $ {file}]; thenecho "$ {file} is a file "~~~~~ : Set autoindent

After that, if you enter spaces or tabs at the beginning of a line, the new line will be indented to the same position. In command mode, enter: set autoindent and press enter to enable auto indent. Determine the indent level by setting shiftwidth. For example, set shiftwidth = 4. set each level of indentation to four spaces. See figure 4 ).

Listing 4. Set the indent level #! /Bin/ksh # for file in/etc/* doif [[-f $ {file}]; thenecho "$ {file} is a file" elif [[-d $ {file}]; thenecho "$ {file} is a directory" fidone ~~ : Set shiftwidth = 4

In command mode, you can use the> command to add a level-1 indent to an existing line. (The command is used to reduce the level-1 indent. Add an integer before these commands to increase or decrease the level-1 indent for multiple lines. For example, if you place the cursor at the beginning of Line 4 in Listing 4 and enter the command mode, enter 5), the following five rows will be added with a level of indentation. Listing 5 shows the result.

Listing 5. indent code block #! /Bin/ksh # for file in/etc/* doif [[-f $ {file}]; thenecho "$ {file} is a file" elif [[-d $ {file}]; thenecho "$ {file} is a directory" fidone ~~

You can use the: set noautoindent command to disable automatic indent. You can also use the short description of this command and the autoindent command, namely: set ai and: set noai. You can also use: set ai sw = 4 to open the indent and set the indent level in a command.

If you want to enable automatic indent every time you start a vi Session and set the indent level to four spaces, add set ai sw = 4 lines to the. exrc file in the main directory.

Case Insensitive during search

As you know®In the search, the mode match is case sensitive. However, if you want vi to be case insensitive, you can use the: set ignorecase command. Use: set noignorecase case sensitive. You can also use the abbreviation: set ic and: set noic ).

If you want to enable case-insensitive search every time you enter a vi Session, add the set ignorecase row to the. exrc file in the main directory.

Compound search

In vi, you can use the/command to search for strings. You must specify the pattern to be matched in the form of a literal string or regular expression. For example, to search for the word echo in a file, you only need to enter command mode, enter/echo, and press Enter. This command will find the first word in line 3rd of the file shown in Listing 6.

Listing 6. Compound search 1 #! /Bin/ksh2 #3 echo "Starting" 4 file =$ {1} 56 echo $ {file} 78 if [[$ {file} = 1]; then9 (file =$ {file} + 1) 10 echo "Adding one gives" \ 11 $ {file} 12 fi13 echo "Ending" 14 exit ~~

You can use a simple regular expression to specify the rows that contain a word and have another word. For example, to find the first line that contains string echo, followed by zero or more characters, and followed by string file, use/echo. * file. In the file shown in Listing 6, this command will find the first word in line 6th.

However, this command is considered to be a match only when the two strings appear on the same line. You can specify a semicolon (;) separate the two search commands to perform a composite search. For example, to search for the string echo that appears after the string {file} + 1, use/{file} + 1/;/echo /. In the file shown in Listing 6, this command will find the first word in line 10th.

Composite search is particularly useful for finding a command that appears after another command in the code-for example, where a function is called after a variable is set.

Replay Search Mode

When you search for a replacement pattern in a file, you can let vi save any pattern to be matched in the buffer; then, when performing the replacement, you can use the buffer reference number to replay them. The method is to place the mode between \ (and \), which instructs vi to place the mode in the numbered buffer 1 to 9. During replacement, you can use the buffer reference numbers \ 1 to \ 9 to reference These buffers.

For example, if you want to search for lines starting with the word Martin in the file shown in listing 7 and add the prefix Mr and the suffix Wicks to each match, enter the command mode and enter the vi command: % s/^ \ (Martin \)/Mr \ 1 Wicks/g, and press Enter.

Listing 7. before replaying the search mode) Martin is an IT consultant. martin likessnowboarding and mountain biking. martin hasworked on UNIX systems for over 15 years. martin alsoworked for policyears before that on mainframes. martin lives in London. ~~~~ : % S/^ \ (Martin \)/Mr \ 1 Wicks/g

The following is an explanation of this command:

: % S-Indicates vi to replace.

/-Mode separator.

^ \ (Martin \)-searches for the row starting with the string Martin and saves the string in buffer 1.

/-Mode separator.

Mr \ 1 Wicks-replace the string found with the string Mr, add the content in buffer 1, and add the string Wicks.

/-Mode separator.

G-Global modification is used to modify all matching locations ).

Buffer references can be used in both search and replacement strings.

For the Modification result, see listing 8.

Listing 8. after replaying the search mode) Mr Martin Wicks is an IT consultant. martin likessnowboarding and mountain biking. martin hasworked on UNIX systems for over 15 years. martin alsoworked for policyears before that on mainframes. mr Martin Wicks lives in London. ~~~~ : % S/^ \ (Martin \)/Mr \ 1 Wicks/g


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.