How to efficiently edit strings after dot in vim - 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.

Related

Vim: Is there a way to replace all occurrences of a word without typing it?

I have the following code:
long_word: ISubscription['long_word'];
This is what I normally do:
shift v
:s/long_word/new_word/g
It's tedious to have to type the word I'm trying to replace. So sometimes I just do
ciw
new_word
esc
$
hhhh
.
which feels inefficient.
Is there a way to do something like ciw but on the whole line?
You could do:
:s/short-string/new_word
to set the replacement pattern, then put the cursor on long_word and type * or # to set the search pattern, and then do:
g&
to replace all occurrences of long_word with new_word. It's not super elegant, but it's not terrible.
Make sure short-string is easy to type and not actually a string that occurs on the current line (you don't actually want to replace it!). I find short-string usually ends up being something like kjkjkjj. Not elegant, but effective.
First things first: V is not necessary because the default range for :help :s is the current line. So that's one improvement.
Then there is the unavoidable fact that the string to substitute has to be defined, somehow. You can't just have your cursor anywhere on the line and expect Vim to figure it out on its own. The only way to start the process is to literally point Vim at the string in question… by placing your cursor on it.
So, assuming the string to substitute is under your cursor and matches Vim's definition of a word (see :help word), you can use :help c_ctrl-r_ctrl-w to insert it in the command-line:
:s/<C-r><C-w>/new-word/g<CR>
If you have to do that often, you could save yourself a lot of effort with a simple mapping:
nnoremap <key> :s/\<<C-r><C-w>\>//g<Left><Left>
Note the \< and \>, which makes sure that only whole words are matched.
Now, the string to substitute might not be a word (or a WORD, in which case you would use :help c_ctrl-r_ctrl-a). In that case, you can still visually select it, yank it, and insert it in the command-line:
v{motion}
y
:s/<C-r>"/new-word/g<CR>
which, of course, could also be turned into a mapping:
xnoremap <key> y:s/<C-r>"//g<Left><Left>

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

Reverse order of string to find and string to replace in vim

I often find myself doing
:s/foo/bar/g
*move to different line*
:s/bar/foo/g
on different lines. Is there an easy way to swap them around so that I can execute the second version quickly?
You could try the Abolish plugin (git homepage):
:Subvert/{foo,bar}/{bar,foo}/g
Without plugin:
:%s/foo\|bar/\=submatch(0) ==# 'foo' ? 'bar' : 'foo'/g
The quick use once only option is to do the following
:s/~/<c-r>//g<cr>
~ matches the last substitution and <c-r>/ will insert the current search string from the "/ register. Therefore flipping the substitution. A word of warning is that ~ can only be used once because after the substitution it will be changed. Also doing a search between substitutions will result in the "/ register changing.
As an alternative you could try to use the command-line window to edit the command like text in any other window.
Use q: to open the command-line window from normal mode or press ctrl-f from the command line (assuming the default setting for 'cedit').
Drew Neil has a vimcasts episode that deals with refining search patterns via the command-window which is similar.
:h /~
:h c_CTRL-R
:h quote/
:h cmdwin
:h q:
:h 'cedit'
You can use dict in vim
:%s/foo\|bar/\={'foo':'bar', 'bar':'foo'}[submatch(0)]/g

How to jump to the next tag in vim help file

I want to learn the vim documentation given in the standard help file. But I am stuck on a navigating issue - I just cannot go to the next tag without having to position the cursor manually. I think you would agree that it is more productive to:
go to the next tag with some
keystroke
press Ctrl-] to read corresponding
topic
press Ctrl-o to return
continue reading initial text
PS. while I was writing this question, I tried some ideas on how to resolve this. I found that searching pipe character with /| is pretty close to what I want. But the tag is surrounded with two pipe '|' characters, so it's still not really optimized to use.
Use the :tn and :tp sequences to navigate between tags.
If you want to look for the next tag on the same help page, try this search:
/|.\{-}|
This means to search for:
The character |
Any characters up to the next |, matching as few as possible (that's what \{-} does).
Another character |
This identifies the tags in the VIM help file.
If you want to browse tags occasionally only, without mapping the search string to keyboard,
/|.*|
also does the trick, which is slightly easier to type in than the suggested
/|.\{-}|
For the case, that the "|" signs for the links in the help file are not visible, you can enable them with
:set conceallevel=0
To establish this setting permanently, please refer to Defining the settings for the vim help file
Well, I don't really see the point. When I want to read everything, I simply use <pagedown> (or <c-f> with some terminals)
" .vim/ftplugin/help/navigate.vim
nnoremap <buffer> <tab> /\*\S\+\*/<cr>zt
?
Or do you mean:
nnoremap <buffer> <tab> /\|\zs\S\{-}\|/<cr><c-]>
?
You could simply remap something like:
nmap ^\ /<Bar><Bslash>zs<Bslash>k<Bslash>+<Bar><CR>
where ^\ is entered as (on my keyboard) Ctrl-V Ctrl-#: choose whatever shortcut you want.
This does a single key search for a | followed by one or more keyword characters and then a |. It puts the cursor on the first keyword character. The and bits are there due to the way map works, see
:help :map-special-chars
As an aside, I imagine that ctrl-t would make more sense than ctrl-o as it's a more direct opposite of ctrl-], but it's up to you. Having said that, ctrl-o will allow you to go back to before the search as well.

Resources