Thursday, June 21, 2012

Going back to empty brackets and quotes

Hello, I have this terrific mapping which takes me back to the
previous empty brackets or quotes:
inoremap jk <c-o>?\({}\\|\[]\\|<>\\|><\\|()\\|""\\|''\\|><lt>\)?s+1<Return>

This works great if there is no whitespace between the empty brackets
/ quotes, so I added the whitespace check:
inoremap jk <c-o>?\({\s*}\\|\[\s*]\\|<\s*>\\|>\s*<\\|(\s*)\\|"\s*"\\|'\s*'\\|>
\s*<lt>\)?s+1<Return>

However, this new regex leaves the cursor at the first character
inside the brackets. I would like it to be at the half-way point, as
sometimes there is no whitespace, sometimes a single space, and
sometimes a double space depending on nestling:

if (something) {}
if ( someFunc(something) ) {}
if ( someFunc(something) || otherFunc(something) )

To write that code, I will do:
if () {}|
^ Here I press jk
if ( ) {}|
^ Here I press jk
if ( ) {}|
^ Here I press jk

Is there any way to put the cursor right in the middle of the
brackets? I tried to write a function which would calculate the amount
of whitespace characters between the brackets and would then press
<Right> half that many times, but I completely failed. I then tried to
select until the next bracket, replace all double whitespace
characters with a single whitespace, set a mark, paste the
now-half-size selection again, and then return to the mark. That
didn't work out so well either! What approach should I be taking?

Thank you!

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment