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?

shime
  • 8,746
  • 1
  • 30
  • 51

3 Answers3

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.

bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • 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. – Andrew Radev Jan 19 '15 at 10:25
  • @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 `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. – bgoldst Jan 19 '15 at 10:35
  • 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 :). – Andrew Radev Jan 19 '15 at 10:37
  • 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. – bgoldst Jan 19 '15 at 10:44
  • 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. – Andrew Radev Jan 19 '15 at 10:49
  • 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! – duma Dec 09 '21 at 16:18
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.

Andrew Radev
  • 3,982
  • 22
  • 35
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.

Gianluca Casati
  • 3,303
  • 1
  • 32
  • 20
  • 1
    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. – Pierre-Adrien Feb 22 '19 at 14:11
  • @Pierre-AdrienBuisson thanks, I updated the answer adding your contribution. – Gianluca Casati Mar 08 '19 at 09:24