Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs 'Ctrl+s Ctrl+w' equivalent in Vim

Tags:

vim

I've used emacs for a while and I'm trying out Vim. In emacs, to select multiple words in the search buffer , I would move the cursor to the starting of the word of interest and then hit "Ctrl+s" which would bring up search mode . then I would hit Ctrl+w to select the words to search incrementally and finally hit Ctrl+s again to search the now selected text.

What is a Vim equivalent for this? I learned that * in vim would search the word under the cursor . But what if I wanted to search more than just that one word ? Like 2 words after the cursor

like image 216
nogeek001 Avatar asked Dec 25 '22 00:12

nogeek001


2 Answers

Vim’s closest equivalent

Vim doesn’t have any direct equivalent of this, but here is a way to get the same effect. This method takes four more keystrokes than in Emacs.

Say this is your document, with the cursor between the []:

I have [s]ome text.
Will I find some text?
Yes. Some people can find text, and I can find some text.

You want to search for “some text”. In Emacs you would type the four keystrokes C-s C-w C-w C-s. Here’s what you would type in Vim:

v e e y / <C-R> " <CR>

Keystroke-by-keystroke explanation

Each keystroke below is a link to that command’s documentation.

  • v – enter Visual mode, selecting the current character, “s”
  • ee – move forward two words, selecting the phrase “some text”
  • y – yank (copy) that text to the default register, the unnamed register "
  • / – start searching; prompts for a search term
  • <C-R>" – insert the contents of the unnamed register ", which you just yanked, into the command line. (<C-R> means Control+R.)
  • <CR> – execute the search. (<CR> is Carriage Return, typed with Enter or Return.)

Improving upon this solution

If you don’t need visual feedback of what you’re selecting, and you already know how many words you need to select, you can save keystrokes by yanking with a motion instead of selecting with visual mode and then yanking. In this case, you would replace v e e y with y 2 e, which means “yank everything from the cursor to the end of the second-next word”.

The above instructions might not search correctly if the search term contains punctuation, because the punctuation might be interpreted as a regex search. To avoid this, write \V (“very nomagic”) right after typing /, which says to treat all characters after it in the pattern literally (except backslash). (I find \V so helpful, I put custom mappings in my .vimrc to make / search literally by default.)

A custom mapping to make this easy

Searching for what is currently visually selected would be a good candidate for the v_* mapping (meaning * in visual mode). By default v_* just works the same as *, but I have never found that behavior for v_* helpful.

Here is a pair of custom mappings for v_* and v_# that you can put in your .vimrc. They let you search literally forwards or backwards for the visually selected text:

xnoremap * y/\V<C-R>"<CR>
xnoremap # y?\V<C-R>"<CR>

With these mappings, to search for “some text” in my example above, you would only need to press v e e *.

If you wanted to make this mapping even more robust, you could extend it to call a function that checks if the selection stops at word boundaries, and adds the word boundary search characters \< and \> to the left and right ends if so. You could also make the function check for and escape backslashes and forward slashes so that search terms containing those would always work.

like image 154
Rory O'Kane Avatar answered Jan 25 '23 14:01

Rory O'Kane


I frequently use that plugin that allows you to use * in visual mode: https://github.com/thinca/vim-visualstar.

With it, your ^S^W^W^W^S sequence in emacs would be veee* in Vim.

like image 33
mcarton Avatar answered Jan 25 '23 12:01

mcarton