VI Tips and Tricks ~ Turn IBM

Source: Internet
Author: User

Compound Search
     1  #!/bin/ksh     2  #     3  echo "Starting"     4  File=${1}     5     6  Echo ${file}     7     8  if [[${file} = 1]] and then     9          ((file=${file}+1))          echo "Adding one gives"     11
   ${file}    -  fi  echo "ending"  exit

If you want to search for a pattern or string that appears after another pattern or string, regardless of whether the two patterns or strings are on the same line, you can specify ; two search commands separated by semicolons () to perform a composite search. For example, to search for {file}+1 a string that appears after a string echo , you should use the /{file}+1/;/echo/ . In the file shown in Listing 6, this command finds the first word in line 10th.

Compound search is especially useful for finding a command that appears behind another command in your code-for example, where a function is called after a variable is set.

Replay Search Mode

When searching for a pattern to replace in a file, you can have VI save any pattern to match in the buffer, and then, when you perform the substitution, you can replay them with a buffer reference number. The method is to place the \( pattern \) between and, which instructs the VI to place the pattern in the numbered buffer (1 to 9). You can use buffer reference numbers \1 To reference these buffers when you perform the substitution \9 .

For example, suppose you want to search for a line that starts with the word Martin in the file shown in Listing 7 and prefix each match with the Mr and suffix wicks, enter command mode, enter the VI command :%s/^\(Martin\)/Mr \1 Wicks/g , and then press ENTER.

Listing 7. Replay Search mode (previous)
Martin is an IT consultant. Martin likessnowboarding and mountain biking. Martin hasworked on UNIX systems for over years. Martin alsoworked for many years before this on mainframes. Martin lives in London.~~~~:%s/^\ (martin\)/mr \1 wicks/g

Let's break down this command to explain:

    • :%s-Instructs VI to perform the replacement.
    • /-The pattern separator character.
    • ^\(Martin\)-Look for the Martin line beginning with the string and save the string in buffer 1.
    • /-The pattern separator character.
    • Mr \1 Wicks-Replace the found string with a string Mr , plus the contents of buffer 1, plus the string Wicks .
    • /-The pattern separator character.
    • g-Global modification (that is, all matching places are modified).

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

The result of the modification is shown in Listing 8.

Listing 8. Replay search mode (after)
Mr Martin Wicks is an IT consultant. Martin likessnowboarding and mountain biking. Martin hasworked on UNIX systems for over years. Martin alsoworked for many years before this on mainframes. Mr Martin Wicks lives in London.~~~~:%s/^\ (martin\)/mr \1 wicks/g

  

Screening

As you may know, by pressing Escape in VI, enter :!command (where command is the UNIX command to execute), and then press ENTER, you can execute the command in the shell. For example, :!pwd displays the current working directory of the edit session.

However, you can also send a portion of a file as a standard input to a UNIX command and replace the same part of the edit buffer with the resulting output. For example, if you want to sort the entire file shown in Listing 9 within a VI session, you can press Escape, enter :1,$!sort and press ENTER, which allows VI to pass everything from the first line to $ the end of the file () to the sort command, replacing the specified part with the output.

Listing 9. Perform file sorting within a VI session (before sorting)
543276548963134~~:1,$!sort

  

Listing 10 shows sort the results of the operation.

Listing 10. Perform file sorting within a VI session (after sorting)
123334445566789~~:1,$!sort

Alternatively, you can precede the shell command with the number of rows you want to manipulate from the current cursor position. The method is to press Escape, enter a number for the specified number of lines, enter two exclamation points ( !! ), and then enter the UNIX command.

For example, place the cursor at the beginning of line 4th in Listing 9, press Escape, and then enter:

4!! awk ' {print ' New text ', $ A} '

If you press ENTER again, the prefix text will be added to line 4th through 7th (inclusive) New text , as shown in Listing 11.

Listing 11. Add new text in front of multiple lines of code
543New text 2New text 7New text 6New text 548963134~~!awk ' {print ' New text ', $} '

You can use the pipe delimiter ( | ) to connect UNIX commands together to perform complex and powerful filtering in a VI session. For example, suppose you want to replace the contents of the file in the edit buffer of the current VI session with the first space-delimited field of each line, sorted in ascending order and converted to uppercase, then enter after pressing Escape:

: 1,$!awk ' {print $} ' | Sort | TR [: Lower:] [: Upper:]

Save some content

You can save a portion of the currently edited file by pressing Escape and entering :start,endfile , where start is the first line to be saved in the current file, andend is the last row w to save. Indicates that you want to write to another file (or overwrite the existing file ), and file is the one that you want to save to the specified section. For the last line, you can use $ the end of the presentation file. You can w use the two greater than sign ( >> ) later to indicate that you want to append the content to the file instead of overwriting the file. The example in listing 12 attaches lines 6th through 9th (inclusive) to the file /tmp/newfile .

Listing 12. Save part of a file to another file (append instead of overwrite)
     1  #     2  # Internet host table     3  #     4  :: 1     localhost     5  127.0.0.1       localhost       loghost     6  192.168.0.6     centos5     7  192.168.0.10    appserv     8  192.168.0.11    webserv     9  192.168.0.12    test  192.168.0.5     solaris10       # Added by DHCP ~~~:6,9w >>/tmp/newfile

VI Tips and Tricks ~ Turn IBM

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.