How to edit blocks in vim without visual block mode

痴心易碎 提交于 2019-12-20 07:13:11

问题


Is there a way to edit a vertical block in a code without using the visual block mode selection?


回答1:


What's so bad about blockwise visual mode?! There's no practical alternative to it.

  • You could use :substitute with atoms like \%>v, \%<v, \%>l and \%<l to limit the pattern match to a rectangular block, but that's very tedious.
  • There are some multi-edit plugins (inspired by other editors) that allow you to select some areas, and then simultaneously edit them all.
  • For special purposes, you could write a scriptlet / mapping with getline() / setline() and String manipulation in Vimscript.



回答2:


In addition to Ingo's answer, I'll add this: Ex commands are line-wise by design. The nature of the visual mode doesn't matter: Ex commands will always use the first line and the last line of your selection as range by default anyway.

Because it's not line-wise, visual-block mode and block "thinking" doesn't really align with Ex commands.




回答3:


You can do something similar to this recent answer: https://stackoverflow.com/a/22238813/3130080

For example,

:%s/\%6c/x/

will insert "x" before the 6'th character in each line, and

:1,2s/\%>1c\%<4c.//g

will delete characters 2 and 3 in lines 1 and 2.

:help /\%c


来源:https://stackoverflow.com/questions/22251379/how-to-edit-blocks-in-vim-without-visual-block-mode

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