Using Ack.vim on visual selection
Asked Answered
J

3

5

Currently I have this mapping in my ~/.vimrc

noremap <Leader>a :Ack <cword><cr>

which enables me to search for a word under the cursor.

I would like to search for a current visual selection instead, because sometimes words are not enough.

Is there a way I can send visual selection to ack.vim?

Jihad answered 18/1, 2015 at 15:5 Comment(0)
L
12

You can write a visual-mode map that yanks the highlighted text and then pastes it verbatim (properly escaped) onto the vim command-line:

vnoremap <Leader>a y:Ack <C-r>=fnameescape(@")<CR><CR>

This solution uses the <C-r>= trick that allows you to enter a kind of second-level command-line, which allows you to enter any vimscript expression, which is then evaluated, and the result is stringified and pasted onto the (original, first-level) command-line where the cursor is.

A slight disadvantage of this approach is that it commandeers the unnamed register, which you may not want.

Limousin answered 18/1, 2015 at 15:16 Comment(6)
A small tweak: instead of vnoremap, it's better to use xnoremap. The former also maps the keystrokes in "select" mode, which is usually used by snippet plugins. Read :help mapmode-x for details.Merrick
@AndrewRadev, good idea, although the map as written would not work for select mode, because the leading y would overwrite the selection and start insert mode. I suppose it could be made to work by writing a separate map for select mode (smap) and prepending the map arg with <Esc>gv to get out of select mode without doing any damage and then getting into (regular) visual mode, where the remainder of the map can take effect.Limousin
I meant that vnoremap would also map the keystrokes in select mode, and you probably don't want that. xnoremap would only map them in visual mode. Somewhat counterintuitively :).Merrick
Aha, I just read the vim help, and right you are! When I read your comment I thought you'd meant "latter" when you said "former", but that was my mistake. It's funny, you basically made the exact point I tried to make in my response.Limousin
Honestly, "latter" and "former" are some of the most confusing words in the English language. To this day, I still don't get who made them up. So yeah.Merrick
I used this to do a vmap for :Rg (package fzf.vim) and it worked great. Finally, I can visually select text and search for it easily. Thank you!Tangier
M
0

While bgoldst's answer should work just fine, you could also consider my fork of ack.vim: https://github.com/AndrewRadev/ack.vim

It comes with a working :Ack command in visual mode, and a few other extras that I've summarized at the top of the README.

Merrick answered 19/1, 2015 at 10:23 Comment(1)
A fork from 2015 and the original Ack.vim still doesn't have visual mode.Hauger
P
0

At the time of this writing this is the default behaviour of Ack.

Just do the following:

  1. move your cursor on any word in normal mode (for instance, hit Esc button to enter in normal mode, you know...)
  2. type :Ack with no argument
  3. it will search for the word under the cursor

Usually I select text during a search in a file (for instance put cursor inside word and type * repeateadly) the type :Ack to look for that word in other files of the project.

Peppers answered 19/2, 2019 at 11:7 Comment(2)
To be precise, this behaviour is not related to visual selection or search. Just put your cursor on any word in normal mode and use :Ack, and it will search for the word under the cursor.Stay
@Pierre-AdrienBuisson thanks, I updated the answer adding your contribution.Peppers

© 2022 - 2024 — McMap. All rights reserved.