问题
gvim command to insert a space or character after every few characters in a file
example: How to add space after every 12 characters in the below lines?
Before adding spaces:
abcdefghijklmnopqrst
dkdkefghijklmnopqrst
After adding spaces
abcdefghijkl mnopqrstvgah
dkdkefghijkl mnopqrstbgdh
回答1:
You can jump to the 12th column by typing 12| so:
:%norm! 12|a<space>
Where space is typed literally
回答2:
A generalized version of this answer fits here perfectly:
:'<,'>s/\(.\{12\}\)/\1 /g
^
|
substitute this with the number of characters
after you'd like to insert spaces in
To break it down:
'<,'>
is the range; see:h range
s
is short for substitute; see:h :s
\(
and\)
is a regular expression (regex) capturing group (:help \( is not very helpful...).
will match any single character\{
and\}
is a regex match quantifier meaning how many times a preceding pattern should be matched (.\{3\}
means...
, and so on)\1
is pasting the text captured by the\(
-\)
block/g
is to replace all occurences of a pattern in a line (see:h s_flags
)
来源:https://stackoverflow.com/questions/59389188/vim-command-to-insert-spaces-in-after-few-characters