Practical case analysis of vim operation (1): adding content at the beginning or end of a line in batch

Time:2020-7-3

When we write code or documents, we often need to add the same content at the beginning or end of multiple lines. It is obviously inefficient to manually operate one line at a time. It is convenient to do such things.

Case 1

hold

aaa
bbb
ccc

ddd
eee

Change to

# aaa
# bbb
# ccc
# 
# ddd
# eee

Commenting out a piece of code is a very common operation. Many people know how to do it. You can replace it directly

:%s/^/# 

#There is a space after it.^Matches only the position of the beginning of the line, not any characters, so it can be replaced directly,$The same is true.

You can also use the column selection mode(<C-v>You can also use<C-q>Because<C-v>It is usually used for pasting. It can be used in VIM or terminal<C-v>Map to paste function, which is more convenient to be consistent with other software), select these lines, and thenI# <Esc>But it is not convenient to skip the empty line, so it will not be expanded.

Case 2

Many people don’t like to add comments at the beginning of an empty line (especially a space at the end of a line), so you need to

aaa
bbb
ccc

ddd
eee

Change to

# aaa
# bbb
# ccc

# ddd
# eee

It is certainly inefficient to replace all of them before deleting them

:%s/./# &

The point is right&Which represents what the front edge matches (that is, the first character of a non empty line).

Case 3

In actual scenarios, we seldom comment out a file as a whole. Generally, we annotate a certain paragraph, such as

aaa
bbb
ccc

ddd
eee

fff
ggg

hhh
iii

Change to

aaa
bbb
ccc

# ddd
# eee

# fff
# ggg

hhh
iii

This operation starts from line 5, and the next operation may start from line 1935GIt is certainly not universal and convenient to operate after positioning. It is usually much more convenient to search and locate

:/ddd/,/ggg/s/./# &

:/ddd/,/ggg/That is, the operation at the back only works between DDD and GGG.

Sometimes we just need to locate the starting line with a search, and then comment out the lines identified at the back (here, we annotate the current line and the four lines after it)

:/ddd/,+4s/./# &

+4It means to put the next four lines into action. If you use line number directly, change it to line number. For example5,+45,9。 If you want to select before you operate, navigate to the start line, and thenV, locate to the end line, and then enter:s/./# &It’s OK. It’s all routine operations. I don’t want to repeat them.

Case 4

Sometimes we need to add content at the end of the line, such as adding a semicolon, to add:

aaa
bbb
ccc

ddd
eee

Change to

aaa;
bbb;
ccc;

ddd;
eee;

You can use:

:%s/.$/&;

This time.Match the last character of a non empty line, just like the same. The positioning range is the same as the operation of several cases above.

Bind to key

If you need this kind of operation frequently, it is troublesome to input commands every time. You can bind them to keys, such as:

nnoremap <Leader>c :s/./# &<CR>
vnoremap <Leader>c :s/./# &<CR>

<Ldader>The default is\It’s not easy to press. Many people have mapped it to,First:

let mapleader = ','
nnoremap \ ,

It will be convenient. You can try it.

Although this is simple, it is not universal. For example, different code annotation methods are different, and each annotation method is mapped to keys, which is obviously too wasteful and inconvenient. You can use existing plug-ins.

Using the VIM commenting plug-in

Commenting out a piece of code is a very common operation. Someone has written a plug-in and can use it directly.

https://github.com/tpope/vim-…

After installation,gcComment (or uncomment) operations, such asgccIs to modify the current line,gcGIs modified from the current line to the end of the file. Plug ins recognize common programming languages or configuration files, and can also add custom formats. It should be noted that it will use line comments instead of block comments. For example, C code will be annotated as follows:

/* int main() { */

/*     return 0; */
/* } */

If you select GC again, it will be uncommented, so it does not affect the usage. If you want to use block annotations, you don’t need to use a plug-in because you only need to manipulate two lines.

It seems that this plug-in does not support adding content at the end of a line. In fact, it can be done through user-defined configuration, such as:

autocmd FileType xxx setlocal commentstring=%s;

If you want it to work with the annotation function, you have to find a way to dynamically modify the configuration, which can be mapped to keys or commands.

But generally speaking, adding content at the end of a line is not common. You can use the command directly.

Original text: http://www.mocihan.ml/archive…

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]