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

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/

Related

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.

How to hold the highlighted section for sometime in VIM editor

I am right now analyzing some code using VI editor. In my use case, I have selected code spanning 2 Pages by using ESC SHIFT v & selecting all the lines (Spanning 2 Pages). Now the issue I have is I am not able to hold the highlight until I need. As soon as I press ESC and move the cursor the highlight goes off.
How do I hold the highlight until my need
If you just want to reselect whatever you previously selected when you leave visual mode you can use gv. You can't keep highlight when leaving a visual mode, though.
Edit:
If you just need to view selected text and you don't want to be distracted by surrounding text, you can simply copy it to an empty buffer. To do so select your text in visual mode, press y then :new then P. When you finish you can close newly created buffer with :bd!.

How do I paste text at multi-line selection in vi?

I know how to use this with manual typing:
Use Ctrl+V to enter visual block mode
Move Up/Downto select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line.
But i don't want to want type the text. I want just to paste it.. (because is a long string..)
Thanks, Mor.
Once you are in insert mode (after I), you can press <C-r>" to insert the content of the default register or <C-r>a for register a to z.
You can also use completion in that context: <C-n> for example.
If the text you want to use is in a register, use <c-r> (CtrlR). So, after you press I, instead of typing, press CtrlR, and the register name you want.
Since the OS clipboard is in the + register, you would do: <c-r>+ (CtrlR++).

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.

Search for selection in 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)

Resources