In vim, is there a way to search for a word in a different window?

2

I'm working with a file where definitions, and references to those definitions are in different parts of the file. I'm looking at the file in a split window - one half looking at the definition, the other half looking at a reference, so that I can update both, and then easily move onto the next definition.

What I want to be able to do, is find a word in one pane, and then search for that word in the other pane. Much like putting the cursor over a word in command mode, and pressing *, except that the current pane shouldn't move - the other should.

Is there a way to do this in vim?

Or, can anyone suggest a different way of working that would make this easier?

SpoonMeiser

Posted 2009-12-08T13:13:58.487

Reputation: 163

Answers

3

This page contains a snippet similar to what you're looking for. It takes the current word (as if you've pressed *), switches to the other window and searches for it with n:

:nnoremap <Leader>w :let @/=expand("<cword>")<Bar>wincmd w<Bar>normal n<CR>

You can change the <Leader>w part to any combination you like (even to something like *, thereby replacing its default functionality).

Liquid_Fire

Posted 2009-12-08T13:13:58.487

Reputation: 372

0

I've found Vim's tabs to be more convenient than split windows. These tabs are available in the console version as well as the GUI. You may need more than a minimal build of Vim for the feature (in particular, the "+windows" compile option needs to be enabled).

Use :tabnew or :tabnew <file> to open a new tab. Use :tabnext (shortcut: gt) or :tabprev (shortcut: gT) to move to the next/previous tab. Use :close to close the current tab window.

See :help tabpage.txt for a basic reference.

As to the question of how to execute a search command in the former tab and have the latter move, I'm not sure -- Leader_Fire's answer may work, but might need tweaking.

quack quixote

Posted 2009-12-08T13:13:58.487

Reputation: 37 382

Yeah yeah, tabs are great. I use them all the time when editing multiple files. In this case I want to view two parts of a file side by side, doing this in different tabs would defeat the purpose - I might as well search with *, and jump back with '' – SpoonMeiser – 2009-12-09T21:55:21.760