Finding a delimiter programmatically

2 views
Skip to first unread message

Tim Johnson

unread,
Nov 28, 2008, 10:54:31 PM11/28/08
to vim_use
Using vim 7.1 (huge) on kubuntu 7.10

Executing the following
?[\|(\|{
Gives me the expected results. I.E.
The cursor is placed at the closest previous '[' or '(' or '{'

Executing the following
/]\|)\|}
Gives me the expected results. I.E.
The cursor is placed on the closest next ']' or ')' or '}'

Yet these two functions:
function! PreviousDelimiter()
execute '?[\|(\|{'
execute ':nohls'
endfunction
" and
function! PreviousDelimiter()
execute '?[\|(\|{'
execute ':nohls'
endfunction

yield unexpected results. In each case, when the function
is called, the cursor is placed at the beginning of the line
of the first successful match, rather than on the target char.

Your help is appreciated and even moreso, pointers to
discussions on the relevant subjects.

Thanks
Tim

Matt Wozniski

unread,
Nov 28, 2008, 11:32:01 PM11/28/08
to vim...@googlegroups.com
On Fri, Nov 28, 2008 at 10:54 PM, Tim Johnson wrote:
>
> Using vim 7.1 (huge) on kubuntu 7.10
>
> Executing the following
> ?[\|(\|{
> Gives me the expected results. I.E.
> The cursor is placed at the closest previous '[' or '(' or '{'
>
> Executing the following
> /]\|)\|}
> Gives me the expected results. I.E.
> The cursor is placed on the closest next ']' or ')' or '}'

Here, you're using the thing mentioned at :help / or :help ? - they
are characterwise searches.

> Yet these two functions:
> function! PreviousDelimiter()
> execute '?[\|(\|{'
> execute ':nohls'
> endfunction
> " and
> function! PreviousDelimiter()
> execute '?[\|(\|{'
> execute ':nohls'
> endfunction
>
> yield unexpected results. In each case, when the function
> is called, the cursor is placed at the beginning of the line
> of the first successful match, rather than on the target char.
>
> Your help is appreciated and even moreso, pointers to
> discussions on the relevant subjects.

And here, you're using the thing mentioned at :help :/ and :help :?
- they are linewise searches that specify a line address. To get the
behavior that you want, either use the search() function, or use
"norm!" to make vim behave as though you typed the ? normal mode
command instead of the ? ex-mode command.

exe 'norm! ?[\|(\|{' . "\<CR>"
or
exe "norm! ?[\\|(\\|{\<CR>"

~Matt

Tony Mechelynck

unread,
Nov 29, 2008, 5:29:33 AM11/29/08
to vim...@googlegroups.com

":execute" runs its argument as an ex-command (the colon is unnecessary,
but, as you can see by starting a command-line with two or more colons,
it does no harm). In this case your search is run as a naked range,
which puts the cursor at the start of the line so found. See ":help
:range". Similarly, ":225" or ":exe 225" would put the cursor at the
start of the 225th line.

You might try
normal ?[\|(\|{
nohls
instead.


Best regards,
Tony.
--
What good is having someone who can walk on water if you don't follow
in his footsteps?

Matt Wozniski

unread,
Nov 29, 2008, 5:39:02 AM11/29/08
to vim...@googlegroups.com
On Sat, Nov 29, 2008 at 5:29 AM, Tony Mechelynck wrote:
>
> You might try
> normal ?[\|(\|{

This won't work. And I gave a correct answer 6 hours ago.

~Matt

Tom Link

unread,
Nov 29, 2008, 5:50:39 AM11/29/08
to vim_use
> exe 'norm! ?[\|(\|{' . "\<CR>"
> or
> exe "norm! ?[\\|(\\|{\<CR>"

Instead of (cryptic) nested norm & exec constructs, you could also use
the search() and/or searchpair() functions.

call search('[[({]', 'b')

Just as an alternative solution.

Tony Mechelynck

unread,
Nov 29, 2008, 6:08:15 AM11/29/08
to vim...@googlegroups.com

Well, I forgot to escape; and I happen to read this newsgroup
top-to-bottom when I get to it. Sometimes the answer is so evident that
I forget someone else might already have given it.

Regards,
Tony.
--
"I have to convince you, or at least snow you ..."
-- Prof. Romas Aleliunas, CS 435

Tim Johnson

unread,
Nov 29, 2008, 11:52:50 AM11/29/08
to vim...@googlegroups.com
On Friday 28 November 2008, Tim Johnson wrote:
> Using vim 7.1 (huge) on kubuntu 7.10
Thanks to all for the edification on this topic. I
especially appreciated Matt's distinction between characterwise
and linewise searches for / and ?

cheers
tim

Reply all
Reply to author
Forward
0 new messages