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?
surround.vim https://github.com/tpope/vim-surround
with this plugin, you can (cursor on or in
(
),cs([
to achieve your goal.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.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):
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.
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:
with the cursor somewhere between the
()
, you can type cs([ in normal mode to get:surround.vim
is easily installed with either Pathogen or Vundle, personally I prefer vundle. https://github.com/VundleVim/Vundle.vimadding the important commented point:
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:
but what I really wanted to do was this:
You will see that you cannot do that because the
c
puts the()
brackets into your0
register and therefore you actually need to do this:or (I was also trying out a 'yank' approach and came up with) this:
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:or this:
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
: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.