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

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.)

Related

Why does vim use registers for almost every command?

I love the simplicity of composing commands that have semantic meaning. Like, for example cib reads like a sentence change in brackets - wonderful.
But why does vim need to copy the old contents into my clipboard? Nowhere in that command have I suggested I want to copy it and the problem goes much deeper.
dw diw etc all copy to the my clipboard/register as well. Why? It seems like this abandons the semantic value of these commands and I would say it is unexpected behaviour.
Am I using these commands wrong, or is there some way to completely disable this feature? Currently I have done a few remappings like this:
nnoremap dd "_dd
nnoremap cc "_cc
but I would like to not do that for every single possible combination of non-explicit copying.
By default, most of the commands you're talking about use the unnamed register, ". It sounds like you're dealing with the clipboard being overwritten for all these things too, which can be a symptom of setting clipboard to unnamed or unnamed_plus.
To go back to standard, you can probably do set clipboard=, if the output of set clipboard? is one of those two options.
*'clipboard'* *'cb'*
'clipboard' 'cb' string (default "autoselect,exclude:cons\|linux"
for X-windows, "" otherwise)
global
{not in Vi}
{only in GUI versions or when the |+xterm_clipboard|
feature is included}
This option is a list of comma separated names.
These names are recognized:
*clipboard-unnamed*
unnamed When included, Vim will use the clipboard register '*'
for all yank, delete, change and put operations which
would normally go to the unnamed register. When a
register is explicitly specified, it will always be
used regardless of whether "unnamed" is in 'clipboard'
or not. The clipboard register can always be
explicitly accessed using the "* notation. Also see
|gui-clipboard|.
*clipboard-unnamedplus*
unnamedplus A variant of the "unnamed" flag which uses the
clipboard register '+' (|quoteplus|) instead of
register '*' for all yank, delete, change and put
operations which would normally go to the unnamed
register. When "unnamed" is also included to the
option, yank operations (but not delete, change or
put) will additionally copy the text into register
'*'.
Only available with the |+X11| feature.
Availability can be checked with:
if has('unnamedplus')
I'll offer a dissenting view, and suggest that you are likely using Vim non-idiomatically. This is in no way unexpected behaviour. Are you using cib as a preparation for pasting, so that cib screws it up? Do vibp instead.
dw diw etc all copy to the my clipboard/register as well. Why? It seems like this abandons the semantic value of these commands and I would say it is unexpected behaviour.
d then p is the normal Vim idiom for cut-and-paste (i.e. move text). This is an operation I do every day, multiple times per day, and it would be really annoying if d did not also yank. You seem to think d is equivalent of Del on other text editors; it is rather the equivalent of ctrl-x (cut). To cancel this, you'd do "_d as you said; I find that I hardly ever need it.
As an advanced example, the default semantics of c and visual-mode p makes it trivial to exchange two objects; for example:
I drank all their food and ate all their whiskey.
Go to "drank", diw (delete the word and yank it), go to "ate", viwp (select a word and paste over it, yanking the previous content), ctrl-o to go back to where "drank" was and P (paste before cursor):
I ate all their food and drank all their whiskey.
(I also have a plugin which defines "function parameter" as a text object, so I use the same idiom in coding if I mix up, or refactor, the parameter order.)
The only thing I want to prevent more commonly is yank-on-paste in visual mode (so that I can paste the same content multiple times); for this, I use
xnoremap <expr> P '"_d"'.v:register.'P'
(from here). This only remaps P in visual mode (which is otherwise identical to p in visual mode). Neither p nor P outside visual mode yank, so it's a non-issue.

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.

how to edit highlight text in vim after searching

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.

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

Change delimiters for navigating word-wise

When programming/writing I heavily use word-wise commands, for example "move to the left/right by one word", "delete next/last word" by pressing Ctrl (+left,backspace...).
The problem I have is, when the text I am editing contains symbols which will not be recognized as words, therefore ctrl + right will jump over a sequence of symbols AND a regular word after that.
Ideally I want to be able to set the delimiting characters for word-wise operations to space, tab, newline and opening and closing brackets - maybe also arithmetic operators (similar to how Eclipse handles it).
I am using Linux. Do you know any way how to change my settings system-wide or alternatively for xterm and (g)vim individually to achieve this?
Most likely, system-wide won't work. VIM is easy, you can set the characters that define the identifier using the iskeyword setting. In your case, there is too much in it, and you need to remove the ones you do need, or redefine it by adding the ones you do want. eg: :set isk=9,32,50-51
This will set keyword detection to spaces, tabs and parentheses.
However, in VIM you can jump based on word and WORDs, where the first is defined by the abovementioned iskeyword setting, while the latter will jump over all non-blank characters. Maybe, that's the motion you want. You can read more about this in the help (:help w).
Instead of holding down the control key and pressing the left/right cursor keys, why not use Vim's normal mode word motion commands?
w/W - move to start of next word/WORD
e/E - move to end of next word/WORD
b/B - move to beginning of previous word/WORD
ge/gE - move to end of previous word/WORD
You can read up on the difference between a word and a WORD by running :help word.

Resources