Use of Vim advanced commands

Source: Internet
Author: User
[Keywords] Vims command g command file format encoding method [abstract] This article summarizes Vim's s command and g command, as well as file line feed format and edit format conversion command. 1. s command if a text editor does not support regular expressions, what you can do may be like notepad, copy, paste, and complete in Windows, slightly more complex operations [keywords] Vim s command g command file format encoding method

[Abstract] This article summarizes the s and g commands of Vim, as well as the conversion commands for line feed format and edit format.

1. s command

If a text editor does not support regular expressions, you may be able to copy
Bay, paste, and complete, a little more complex operations will be powerless. VI (M) as a necessary text for various Unix release versions
The editing tool provides powerful support for regular expressions.
Find the operation, and the Vim s command, which can replace a regular expression with another regular expression. for example:
If you are very familiar with regular expressions, execute replace in Vim, and the search operation is like a duck.
The expression also learned from Vim and recorded some common commands for future forgetting.
Wait:

The basic format of the s command is:

: S/str1/str2/replace str1 that appears for the first time in the line with string str2
: S/str1/str2/g replace all the strings in the row with str2

From the above we can see that g is placed at the end of the command to replace each appearance of the search string; without g, it indicates that only the search word is
String is replaced for the first time. g is placed at the beginning of the command to replace all rows containing search strings in the body.

": % S/[Ctrl-v] [Enter] // g" remove the line ending Mark M in windows. for command analysis, Ctrl-v is used,
The input enter key is considered a special character, rather than the end of the vim command.

": % S/\ n \@! // "Or": g/./,/^ $/-1j ": press enter to typeset the text that is segmented by blank lines into segments by press Enter.
Text, if you do not want to retain the blank line between paragraphs ": g/./,/^ \ s * $/j"

": % S/\ s \ + $ //" remove spaces at the end of all rows:

": % S! \ S *//.*!! "Remove all" // "comments, command analysis: first you can note that the separator here is switched
"! ", The reason is that the"/"character is used in the mode or string. if other separators are not used
"/" Is written as "\/", and the preceding command must be written as ": % s/\ s * \/. */", which is less readable.
It is quite simple. people who have used regular expressions know that "." matches any character except line breaks.
.

": % S! \ S */\ * \ _. \ {-} \ */\ s *! ! G "remove all"/**/"comments, command analysis: (this is a bit complicated
The Vim regular expression features are used. "\ _." matches all characters including line breaks;
"\ {-}" Indicates that the first character can appear zero or multiple times. However, if the entire regular expression can match successfully,
The smaller the number of matched characters, the better. The Mark "g" indicates that a row can be matched and replaced multiple times. The replacement result is a space.
The purpose is to ensure that expressions such as "int/* space not necessary around comments */main ()" are
It is still valid after replacement)

": % S/$/\ r/g" converts the text segmented by carriage return into text formatted by carriage return and segmented by blank lines.

2.g command

The global command is one of Vim's most powerful commands (if used well, it won't be inferior to the s command at all ).
The biggest problem that affects the popularity of g commands is that g commands are mostly hard to understand, but not deeply understood.
Ten commands are quite difficult. This section summarizes several g commands that I often use and provides explanations to avoid forgetting them in the future.

Format of the 2.1g command

You can use ": help g" to obtain the detailed help of the g command. the format of the g command is:

[Range] global/{pattern}/{command}

The global command searches for {pattern} within the text range specified by [range] (the entire file by default), and then matches
To execute the command {command}. if you want to execute the command for a line that does not match, use global! Or
Vglobal command.

2.2g command example

Example 1. inverted file line (that is, the tac command under unix)

: G/^/m 0

This command marks/^/at the beginning of the line to match all lines of the file (this is a common search technique, if/./is used
Configure non-empty lines, less than the requirements of this example), and then use the move command to move each line to the first line (the next line of the 0th line) in sequence)
To implement the reverse function.

Example 2. delete all empty rows in the file

: G/^ \ s * $/d
^ \ S * $ is a regular expression for empty rows. after finding it, execute d to delete the row. this is my most common example.
Because \ r \ n is used repeatedly in the program, log files generated are usually one line of log, one line
Space. use this command to make the file look more compact.

Example 3: delete an even number of rows

: G/^/+ 1 d

Command Analysis: this command matches all rows and then deletes the rows (where + 1 is used to locate the next row of the current row ).
Why is it a line break? Because the second line is deleted when the + 1 d command is executed on the first line, while the second line is also marked
Note, but it does not exist. Therefore, the command for deleting the third line will not be executed.

Example 4: delete an odd number of rows,

: G/^/d | m.

Analysis: The light is: g/^/d obviously does not work, this will delete all rows, we need to use the move command to mark the even rows
Remove. of course, this example can be easily converted to 4), which is only used to emphasize the concept of tag.

Example 5: convert text segmented by carriage return into text segmented by blank lines by carriage return.

: GgVGgq

For more complex examples, refer to [1]. There are many commands that cannot be understood yet, and of course they cannot be remembered.

3. file format conversion

For historical reasons, Windows uses "\ r \ n" to indicate the end of a row, Unix uses "\ r" to indicate the end, and MacOS uses "\ n ",
Vim can automatically recognize these formats.
Some programs may not recognize Unix line breaks. if someone else sees your file, there may be only one line leading to the article.
Why don't I keep the line breaks when I complain?

It is very easy to modify the line feed format of a file in Vim. run the following command on the command line and save it.

: Setfileformat = unix

This file is stored in Unix format ("\ r"). other "tricks ":

Vi dos_file.txt
: % S/^ M/g

^ M must be Ctrl + V + M at the same time, which indicates the carriage return. Not directly input ^ M, that's useless ,:-).

4. file encoding conversion

Vim can support Chinese characters, but most people who have used Vim have a chaotic experience. when they are not careful about the file, they have to solve the problem.
The garbled problem is relatively simple. you only need to set the values of enc and fenc in. vimrc. (refer to study note 11 and share it with me.
. Vimrc). setting the values of enc and fenc ensures that no garbled characters appear in the vim body, but there are some plug-ins (such
TxtBrowser) the document's enc and fenc must be consistent in order to correctly display the Chinese document structure.
In fact, it is very simple to modify the fenc value of a file. for example, if you want to set the fenc value of the file to cp936
Run the following command on the command line to save and exit.

: Set fenc = cp936

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.