如何插入递增数字在非空行中

Open your file in Vim.

Set a variable to hold the incrementing number. You can start with 1 or any number you prefer. In command mode, type this and press Enter:

bash

:let i=0

Now, use the following command to insert numbers at the beginning of each non-empty line. This command uses the global (g) command to run a substitution (s) on every line that is not just whitespace. The \ze in the substitution pattern ensures that the number is inserted at the start of the line:

:g/\S/let i+=1 | s/^/=i . ‘. ‘/

Here’s what each part does:

:g/\S/: This tells Vim to execute a command on all lines that contain a non-whitespace character (\S).
let i+=1: This increments the variable i by 1.
|: This separates Vim commands on the same line.
s/^/\=i . ' '/: This substitutes the start of the line (^) with the value of i followed by a space. \= allows evaluating the expression i . ' ' to concatenate the number and a space.

𝔓46 𝔓46

:

将段落合并成一行

'<,'>g/^./ .,/^$/-1 join