Quickest way to change a pair of parenthesis to brackets in vim

♀尐吖头ヾ 提交于 2019-11-29 17:00:31

问题


I am new to vim and in the process of discovering tons of interesting things that one can using this powerful editor.

One particular thing that I need to do very frequently is to change a pair of parenthesis in the text to square-brackets (for example to change ( (a+b+c) ) to [ (a+b+c) ]) or vice-verso. I now do this by manually changing the two characters ( and ) to [ and ].

However, when there is a lot of text in between the parenthesis, it could be difficult to locate the pair of opening and closing parenthesis especially because after changing the first ( to [ then the % command will no longer be able to find the corresponding ).

I was wondering if there is a better and quicker way to make such changes?


回答1:


I would simply do it like this: %r]^or[.

Here's an explanation:

  • f( -- put cursor on first parenthesis you want to change (if it's not already there).
  • % -- jump to the matching parenthesis.
  • r] -- replace the parenthesis with a bracket.
  • CTRL-O -- jump back to to first parenthesis.
  • r[ -- replace the parenthesis with a bracket.



回答2:


I personally use https://github.com/tpope/vim-surround as it provides everything I could ever need, reading through the source you can see the solution is non-trivial.

A typical example:

Hello("World")

with the cursor somewhere between the (), you can type cs([ in normal mode to get:

Hello["World"]

surround.vim is easily installed with either Pathogen or Vundle, personally I prefer vundle. https://github.com/VundleVim/Vundle.vim

adding the important commented point:

cs([ adds spaces in the block, this should be cs)]



回答3:


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

with this plugin, you can (cursor on or in (), cs([ to achieve your goal.




回答4:


With lh-brackets, I would use <m-b>( to change any pair of bracket-like characters (cursor on the first/last character of the pair) to a pair of parenthesis. <m-b>{ -> curly-brackets, and so on.

For the curious ones, this is how it works -- see s:ChangeTo(). Internally, I do a %r]``r[, and I have a dedicated treatment for quote characters.




回答5:


Without any plugin it can be done by deleting the content inside the parenthesis and yanking in the new bracket (from anywhere within the bracket):

di(a[]<esc>P%2X

Obviously more key that using surround but but not that many ;-)

Note

There is no need to remember the sequence of key but only to start by deleting the inside of the brackets. Then it's just normal vim fu.




回答6:


Based on a few of the SO's around this matter (see my comment in the @mb14 answer here), I was thinking of muscle-memorizing something like this:

di(a<bkspace><bkspace>[]<Esc>P

but what I really wanted to do was this:

di(c%[]<Esc>P

You will see that you cannot do that because the c puts the () brackets into your 0 register and therefore you actually need to do this:

di("_c%[]<Esc>P

or (I was also trying out a 'yank' approach and came up with) this:

yi(ca([]<Esc>"0P

Okay, neither is too bad, but it occurred to me that this is going to all go much better if I map <leader>c to "_c so that I have a real delete and can do this:

di(\c%[]<Esc>P

or this:

yi(\ca([]<Esc>P

Both are pretty close to what I wanted to do, and the thought process has given me one of the most valuable lines in my $MYVIMRC:

noremap <leader>c "_c


来源:https://stackoverflow.com/questions/25405072/quickest-way-to-change-a-pair-of-parenthesis-to-brackets-in-vim

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