how to edit highlight text in vim after searching - vim

I like to use "*" to search text in vim. after hight light the target text, I want to edit all of them, is there any way I can do it in vim? for example, after highlight text, I just need to press ctrl+i then the highlight text can be edited simultaneously

Simultaneous editing (like seen in other editors) is not built into Vim (but there are plugins). You don't need them, though. After *, the text is stored in the last search pattern register, and you can just :substitute// without repeating what you're searching for:
:%s//replacement/g
The % is a range and applies this to the whole buffer; the /g is a flag that replaces all (globally) instances, not just the first in each line. Read :help :s for details.

You can check out the vim-multiple-cursors plugin.
Personally, I like #Ingo's solution. The less plugins the better.

As a nice alternative. You can use gn and the . command.
Set your search pattern i.e. * or /foo
Change your highlighted pattern via c operator over the gn motion
cgnbar<esc> will change the highlighted area to bar.
Now you can use . too repeat this change. You can also use n to skip places.
Note: This requires at least 7.4
For more help see:
:h gn
:h .

If you wish to edit the word with another you can use the substitute command. (e.g. :%s/hi/hello/g)
This will change all occurrences of hi to hello in the file.

Related

How to change a word in Vim (and all of its other occurrences)

I frequently use the combination c-a-w to change a word in Vim.
Are there any similar means by which one can quickly also change all other occurrences of said word in the specific file?
Use gn option for this purpose, in my case, I have a slightly different version of it
" allows me to use a smarter cgn
nnoremap c* *<c-o>cgn
nnoremap c# #<C-o>cgn
Now when you have to change a word many times, as long as you have not so many of it, because in this case, a classical substitution would be better, just press c* and then press "dot --> ." to change the next occurrencies of it.
If you want to see how awesomeness gn can give us have a look at: Operating on search matches using gn (vimcasts)
You could try:
%s/<CTRL-R><CTRL-W>/NewWord/g
<CTRL-R><CTRL-W> means keep control key pressed and hit R and W.
This copies the word under the cursor to the command line.
See :help c_CTRL-R_CTRL-W.
The main command for replacement of all occurrences is :substitute. Unfortunately, being an Ex command, it doesn't integrate too well with the single-word replacement (e.g. caw) in normal mode: Though you can insert the previously replaced word into the command-line with <C-R>", you still have to enclose it in \<...\> to enforce a whole word match, and also escape any special characters inside the word.
That said, there are plugins that offer help in that area. One of them is my ChangeGlobally plugin, which offers a gc{motion} alternative to c{motion} that then applies the change (or deletion) to other matches in the same line or entire buffer. (The plugin page has links to alternative plugins.)

How to efficiently edit strings after dot in vim

In vim, I have to change someText.someTextAfterDot string to someText.somethingDifferent. The cursor is at the t in After. What should be the most efficient way to do this?
If I use 'cw' command then that removes the complete someText.someTextAfterDot including someText which I don't think is efficient because now I have to type the someText again.
Normally, I use 'F.ce' (find the last . and edit till end of current word) command and then type somethingDifferent. This seems like a lot of work as in other editors (like WebStorm or Sublime) a simple double click on someTextAfterDot can do the trick.
Is there any other more efficient way to do this in vim?
First of all cw should only go forward so only remove terDot. If cw is doing anything more then I assume you have some bad mappings.
The "inner" word text object, iw, is what you need. e.g. ciw. However if you have added . to 'iskeyword' then word's will include . and the behavior of ciw will be incorrect.
To find out where 'iskeyword' is last set use the following command:
:verbose set iskeyword?
For more help see:
:h 'iskeyword'
:h iw
:h w
:h cw
:h :verbose
My intuition would be to type bdeisomethingDifferent. I dunno that it's a great improvement, but it at least doesn't make you type the . again the way F.ce does.
I normally use change inner word for that. You simply have to type
ciwsomethingDifferent
Check the documentation for text object selection too.

What's the vim way to select multiple instances of current word and change them?

Anyone familiar with Sublime Text's multiple cursor feature will recognize the pattern of doing the following: press a hotkey multiple times to select multiple instances of the word under the cursor and automatically create a new cursor for each of those instances; then edit each instance simultaneously, e.g. by replacing the current word with another word or whatever you want.
The multiple cursors feature is available for vim via plugin. Before using that plugin, I want (as a new vim user), to check whether there is a more natively vim-like way to achieve the same task.
For instance, I know I could use the :s command to do a search and replace (per instructions here), but that requires me to (1) type in the word I want to replace (or use the <C-r><C-a> shortcut to do so), as opposed to simply using the current word and (2) define a range. Perhaps this is the native vim way to do it, perhaps (likely!) there's another way I don't know.
So what is the native vim way?
I use the *, gn, and the . to make changes.
Select current word with * (go back with N)
Change word with gn motion. e.g. cgnfoo<esc>
Repeat via . command
Note: If you have many changes then using a substitution command would probably be better.
There is a nice Vimcasts episode about the gn motion: Operating on search matches using gn.
For more help see:
:h *
:h gn
:h .
You can record macros in Vim by pressing q<letter>. Macros can include the n command to search for the next instance of a word. You can also go into insert mode while recording (e.g. using the c command with a motion such as iw to replace the current word). Press q to stop recording, and then press #<letter> to replay the macro once. After that, you can use ## to repeat the macro as many times as you like.
While waiting for other answers, I'm going to post what I'm experimenting with while waiting for vim experts to answer:
:.,$s/<C-r><C-a>/foobar/gc
to substitute (the s) from the current line (the .) to the last line ($) (with the comma denoting the line range), using the <C-r><C-a> combo to copy the current word into the command, then using gc to change with confirmation, so I can hit yes/no for each instance then quit when I've done enough.

Omnicompletion search suggestions

Is there any way to configure Omnicompletion for / searches? So searching for /be would suggest other words in the text such as:
/be<tab>
Beatles
beer
Beethoven
personally, I think after typing / you can type a regex, auto-completion doesn't make much sense here... vim doesn't know what you want to have, how can it give suggestions? It is not like in Insert mode.
However there is way to achieve it.
you type /
you type Ctrl-F
you type i (go into insert mode)
you type beTAB
now you see the popup menu.
this works not only for /, but also for : (cmd mode)
Ctrl-F is handy when we write long commands
detail:
:h cedit
You can use the CmdlineComplete plugin.
It will be triggered with <C-n> / <C-p>, and won't show a completion menu (but you can cycle through candidates by repeating the trigger).
You can use a combination of 'incsearch' and command-line completion with CtrlR CtrlW (:h c_CTRL-R_CTRL-W) to achieve something quite close to what you want:
:set incsearch.
Start typing your search pattern, e.g. /Be. The cursor moves to the next potential match as you type.
As soon as the cursor lands on the word you want to complete, hit CtrlR CtrlW. This pulls the word down into your search prompt: it effectively "completes" your search pattern.
At stage 3 you could also use these variants instead:
CtrlR CtrlA (:h c_CTRL-R_CTRL-A) pulls down the WORD instead of the word.
CtrlL (:h c_CTRL-L) completes character by character.
I used the aforementioned CmdLineComplete plugin until I learned about
set incsearch

How do I repeatedly search & replace a long string of text in vim?

I'm aware of the vim replace command, which is of the form, eg:
:%s/old/new/gc
But what if either of these strings is long? How can I use something like visual selection mode, the clipboard or vim registers instead of having to type the old/new text in?
You can use q: to bring up a command-line window. This lets you use all the vim editing commands to edit the vim command line, including p to paste. So, you could copy the text into a register, paste it into the command line window, and execute it that way.
I recently discovered this feature via vimcasts.
According to the manual, you can use Ctrl+R to insert the contents of a register into the current position in the command line. The manual also claims that Ctrl+Y inserts the text highlighted with the mouse into the command line. Remember that in X11 and some other systems, you can also paste text into a program from the system clipboard using the middle mouse button or a menu command in your terminal emulator.
I think to avoid have your command line be huge you can use this to solve your issue
:%s/foo/\=#a/g
That replaces "foo" with whatever is in register a.
If you're trying to do a substitute with a long complicated search pattern, here's a good way of going about it:
Try out the search pattern using some test cases and refine it until you have the pattern you want. I find incsearch really helps, especially with complicated regular expressions.
You can then use :%s//new to replace all instances of the last searched for pattern.
If you've entered a pattern and want to copy it out of the search history, you can use q/ to bring up a command line window containing recent search patterns very similar to the q: one that contains recent command history.
On the other hand, if you're asking about how to copy and paste text into the substitute command:
I'd write the pattern out in insert mode and yank the search and replacement into two distinct registers using, say, "ay and "by and then use :%s/<C-R>a/<C-R>b/gc to do the substitute. There are lots of variations of the yank command, but this one should also work automatically when using a visual selection.
If you're copying in text from the clipboard, you can use <C-R>* to paste it's contents in insert mode.
I have the following mapping in my .vimrc
vnoremap <leader>r "ry:%s/^Rr/
So I visually select the thing I want to replace, and hit ,r, type the replacement and hit return. If I want to paste the replacement, I yank it before selecting the text to replace, and then use <C-r>" to paste it as the replacement before hitting return.
Note: to insert ^R in your .vimrc, you actually type <C-v><C-r>.

Resources