VIM command to insert spaces in after few characters?

偶尔善良 提交于 2021-02-11 13:55:58

问题


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
  • space
  • /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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!