1

I'm trying to write a vimscript function that will allow me to map a key to select a regex match on the line my cursor is on. This will help with selecting strings for use with ctags.

For example, I have the following text:

0 def hello
1   My::Fancy::Klass.call(args)
2 end

Assuming my cursor is on line 1, I would like to bind a key to select only the My::Fancy:Klass text.

I was able to achieve this like so:

nnoremap <leader>] V /\%V\(\(\u\l\+\)\+:\{2}\(\u\l\+\)\+\)\+<cr><esc>

But I would like to do the same with vimscript and am not having any luck.

6
  • 1
    You really should try "very magic" mode (see :h /\v). Start the pattern with \v and you no longer need to apply so many backslashes to indicate special meaning. This is the equivalent pattern: \v%V((\u\l+)+:{2}(\u\l+)+)+.
    – B Layer
    Commented Sep 26, 2018 at 1:24
  • I'm not quite clear on this. You want to call a function instead of hit the key(s) of a mapping? Also, why are you entering Visual mode only to exit it? If you're just trying highlight something on the current line all the vimscript you need is this: let @/ = '\v%V((\u\l+)+:{2}(\u\l+)+)+'
    – B Layer
    Commented Sep 26, 2018 at 1:42
  • I'm not sure how to use that registry variable. I can see the pattern when I echo it, but it's not visually selecting the string. I'm trying to use this so I can navigate with ctags and the only way to do that is if the entire string is visually selected.
    – Nathan
    Commented Sep 26, 2018 at 1:55
  • 1
    Hi Nathan, welcome to vi.stackexchange! This question seems similar but different from the one you posted just an hour before. Are they truly duplicate questions or do they seem different enough to you to justify two separate questions?
    – D. Ben Knoble
    Commented Sep 26, 2018 at 13:31
  • 1
    Hi @D.BenKnoble, they lead to the same place in my situation but maybe not for others. I would say they are different.
    – Nathan
    Commented Sep 27, 2018 at 7:35

1 Answer 1

1

As I mention in my comment it's not clear what you're trying to do. But I can give you an Ex command sequence (vimsript) that does the same thing as your mapping:

exec 'norm! V' | let @/ = '\v%V((\u\l+)+:{2}(\u\l+)+)+' | exec "norm! \<C-C>"

Here's how these three commands match up to your mapping:

exec 'norm! V'  ==>  V
let @/ = '\v%V((\u\l+)+:{2}(\u\l+)+)+' ==> /\%V\(\(\u\l\+\)\+:\{2}\(\u\l\+\)\+\)\+<cr>
exec "norm! \<C-C>"  ==> <esc>  (Ctrl-C and Escape do the same thing)

Even though your mapping leaves Visual mode your comment implies you want to remain in it. If that's true remove the third part:

exec 'norm! V' | let @/ = '\v%V((\u\l+)+:{2}(\u\l+)+)+'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.