For example, I have some code and I use "*" to search for something within that code. Not finding what I want in that file, I'd like to use something like ack
or grep
to search for it within the local directory. I know I can do :! ack whatever
to do the search from within vim, but what I'd like to know is is there a way to replace whatever
with the currently searched for word within vim.
views:
89answers:
4
A:
You can yank the word into a register and use the @regnum in the command mode line
"1yw :!grep @1
ThePosey
2010-04-29 16:53:58
The last search string is already in the / register
Dave Kirby
2010-04-29 20:21:49
A:
I haven't found a way to (easily) pass the contents of a search register to an external program, without resorting to key mappings and eval statmements.
However, you can use <cword>
to pass the word currently under the cursor to an external program:
:!echo <cword>
or
:!ack <cword>
John Weldon
2010-04-29 16:54:41
yes, probably because ack and grep don't handle the `\<` and the `\>` the same..
John Weldon
2010-04-29 17:00:24
I do exactly that (`:! echo @/`), but the output of echo is literally '@/'.
Tim Henigan
2010-04-29 17:08:28
@Tim Henigan, my mistake, `:echo @/` will show you the contents of the `/` register...
John Weldon
2010-04-29 17:28:47
+3
A:
You can use Ctrl-r followed by / to insert the last-search register.
:!grep <Ctrl-r> / file_list
See this Vim Tips Wiki entry for more info.
Update:
The *
search command command always includes word boundaries.
However, the g*
search command behaves the same as *
, but without word boundaries.
This could be used to work around your issue rather than using the custom macro in rampion's answer.
Tim Henigan
2010-04-29 16:56:58
This works, although when used in conjunction with `*` it also has the word boundaries. Is there anyway to get the text without them?
Paul Wicks
2010-04-29 17:11:10
The `*` search sets up the search pattern to include the word boundaries. So you can't use `*` to get the pattern without them.
Tim Henigan
2010-04-29 17:17:02
Awesome, this is just what I was looking for. I did modify your mapping a little bit to avoid having to hit the enter key twice:`cmap <C-R>/ <C-R>=substitute(substitute(@/, '^\\<', '', ''), '\\>$', '', '') <CR>`
Paul Wicks
2010-04-29 19:51:36
Another option is to use the `g*` search command rather than `*`. See my updated answer for details: http://stackoverflow.com/questions/2739135/how-to-retrieve-the-currently-searched-for-text-in-vim/2739223#2739223
Tim Henigan
2010-04-29 20:11:10
`ctrl-r` followed by `/` inserts the text from the search register, but if you were to do `ctrl-r` followed by any letter on the keyboard, it would insert the contents of the corresponding register. Besides the letters, there are other special registers, such as `.` (the last entered text), `%` (the current filename) etc. By remapping `ctrl-r` you lock away all of the potential useful shortcuts that can be constructed from these various registers.
nelstrom
2010-04-29 22:05:42
@nelstrom: I'm not remapping `CTRL-R`, I'm remapping `CTRL-R /`. All the other `CTRL-R` commands still work.
rampion
2010-04-30 14:55:07