Search for selection in Vim - vim

I use Vim and Vim plugins for Visual Studio when writing C++. Often, I find myself wanting to search for a string within a function, for example every call to object->public_member.memberfunc().
I know Vim offers a convenient way to search for a single word, by pressing * and #, and it can also search for typed strings using the ubiquitous slash / command. When trying to search for all the instances of a longer string like the one above, it takes a while to re-type after /.
Is there a way to search for the selection? For example, highlight with v, then copy with y, is there a way to paste after /? Is there an easier shortcut?

Check this Vim tip: Search for visually selected text
Or you can simply yank the selected text with y and go to search mode /, then you can paste the last yanked text with Ctrl+R 0

Answer
Yank the text you want to search for
q/p
Enter
Explanation
q/ works similarly to vanilla search / except you're in command mode so p actually does "paste" instead of typing the character p. So the above will copy the text you're searching for and paste it into a search.
For more details type :help q/

Use q / instead of just /. (Same with q :). Now you can VIM-edit through your command and search history! (Try Ctrl-N and Ctrl-P sometime).

I just learned (through the excellent book Practical Vim) that there is a plugin for that.
You can find the plugin on GitHub.
The plugin lets you search for a visual selection with * and #.

You can actually select text visually and press * and # to search for the next occurrence... It will work the same, the only caveat is that:
Whitespace in the selection matches any whitespace, when searching (searching for "hello world" will also find "hello" at the end of a line, with "world" at the start of the next line).
http://vim.wikia.com/wiki/Search_for_visually_selected_text

--> if you want to highlight a text occurrences in gvim
Select the text & copy
then ?paste the selected text (Note: This will not work for insert mode)

Related

Use visual character-wise selection as auto-fill for Vim substitute command

Imagine that [ ] represents a selection. So given
This is pi with the 10th through 20th digits obscured: 3.14159265[3589793238]46264338327950288419716939937510...
the visual selection would be 3589793238.
In Sublime Text, when you have some text selected, and then press Ctrl+h to start the search/replace feature, it will auto-fill the search box with the currently selected text. Is there a way to emulate this in Vim? That is, assuming I am in character-wise visual mode and have made the selection indicated above, I would then press some shortcut combo and Vim would yield :%s/3589793238/ in the command line. That way I could simply complete it like so :%s/3589793238/___/ to replace 3589793238 with ___ throughout the currently open file (without having to manually type 3589793238 into the Vim command-line).
In command mode, you can input the content of any register with <C-R> followed by its name. So you could yank the currently selected text (yanked text is stored in register 0) and then input it with <C-R>0 when writing your substitute command.
To fully automate this, you could add a mapping like so:
xmap <leader>s y:%s/<C-R>0/

Is it possible to simultaneously get the contents of a visual selection into a register and replace all the characters?

Consider the following text file.
Replace and yank this portion Ignore this portion
Suppose I have visually selected the part that says Replace and yank this portion.
I can take one of the following actions at this point.
I can use y to yank the contents into a register, but this destroys the visual selection.
I can use rx to replace each of the characters with an x, but this also destroys the visual selection.
Is it possible to simultaneously put the visual selection into a register and replace each of the characters in the visual selection with an x?
That is, I'm looking for a sequence of commands that result in the selected text being in a register, and each character in the selected text replaced by x. I'm not picky about which register.
Immediately after posting this question, I realized that all I needed was to be able to re-select the text that was just selected.
A quick Google search led to using gv for re-selection.
Thus, the final command sequence to achieve the desired effect is ygvrx. This will first yank the sequence into the register, re-select the previous selection, and then replace the characters.
Visually select the text and press c for change. Type the text you want and press <esc>. The text that was there before (in this case Replace and yank this portion) is now in your "" register, so you can just hit p as soon as you want to paste it.
type :h reg to see a list of all registers and what text you have inside them.

Vim command: how to select a word under cursor in normal mode

I'm looking for a Vim command to select a word under cursor in normal mode, like double-clicking by mouse. Does it exist like this?
You can use * and/or # to search for the word under the cursor or viw to visually select the word under the cursor.
viw does a visual select inside the word. Similarly yiw copies (yanks) the word.
Personally, I am prefering vaw, yaw instead of viw or yiw as a indicates around.
On the similar lines. I use
vat to select tag.
va( or va{
v% to select matching closing tag. In some other places ev%
It is making more sense to me as the intention is to select the complete word not inside.
At the end, it all comes down to our personal preference.
To select a word under a cursor I use the combination of bve
Be aware though that if the cursor points to a first character you just need ve combination.
After using *, if you want to use the text in a command you can do the <C-r> <C-w> trick too (after pressing : or your equivalent).

Restore the previous highlighted chars quickly?

Is there a shortcut (or some other quick way) which could restore previous highlighted items quickly, like the one highlighted by / when hlsearch is on?
EDIT: for previous highlighted word, I mean the word highlighted by previous searches via /. 2 search commands with different words are involved here at least.
Vim saves previous searches in the search history. You can recall previous searches by pressing ↑ in the search command-line (which you enter via /). This even considers the typed prefix, so with /foo<Up>, you'll recall previous searches that started with foo.
Alternatively, you can enter the command-line window for searches with q/. There, you can use the default Vim commands to navigate, move around, and edit, and finally select an entry to search for via Enter.
gv re-selects the last visual selection
gn selects the next search match

Find and Replace within selection in `vi`

How do I do a Find and Replace within a selection in vi?
Select the text in visual mode (I assume that's what you're doing), then press : to start typing a command, you'll see something like this appear in the command line:
:'<,'>
That means that the command will apply to the selection. Then type s/search/replace/ and hit enter. (Add a g after the third slash if you want to replace all matches, and a c if you want a confirmation for every replace)
Most of the other solutions suggested here work over the ENTIRE line in which the selection occurs, which may not be what you want.
To search and replace ONLY in the selection, first visually select the text, then use a command like so:
:%s/\%VSEARCH/REPLACE/g
This will do the search and replace only in the visually selected section, replacing SEARCH with REPLACE. If you have more than one line selected, this will work over multiple lines too.
If you used Visual Mode to select, then:
:'<,'>s/regex/replacement/options
VIM will place the range ('<,'>) automatically if you go into Command Line Mode (by pressing ':') from within Visual Mode.
Some more help here Search and replace in a visual selection
The range of Ex commands are specified line-wise (see *cmdline-ranges*), and when : is pressed while there is a visual selection, the line range is automatically specified on the command line as '<,'> (see *v_:*), which makes the :s[ubstitute] command operate on the whole lines unless the visual selection boundaries are specified in the search pattern with \%V (see */\%V*), e.g. /\%Vvi\%Vm matches "vim" only within the visual selection, where the end of the selection is specified right before the end of the search pattern since each \%V specifies the next character as the start or end of the visual selection, and thus /\%Vvim\%V would require the visual selection to continue after 'm' to match "vim". Note that using the second \%V in a search pattern isn't necessary unless a match is required to be right at the border of or only partly in the visual selection.
If you want to do a global search and replace (with optional regexes) for all instances in the file, I would do the following:
:%s/foo/bar/g
Omit the g to do a local replace.

Resources