vim Editing - how to edit the highlighted segment of a search or convert to visual block - vim

In vim, if I have the following text:
hello there
Say I search for hell in the text above.
/hell
and press n to move to the next instance.
hello
hell is now highlighted in vim and my cursor is on the 'h'.
What is the most efficient way to now yank/delete that highlighted text.
Is there a way to 'yank to the end of the highlighted text'?
Or 'create visual block from highlighted text'?
I know I can use %s/hell/whatever/gc to step through as an alternative.
TIA Tom

y//e, or d//e should do the trick.
:let #" = #/ as well, even if you have moved the cursor.

I don't know of a built in mapping (Edit: but see Luc Hermitte's answer below as that's a much better solution than my bodges End Edit), but you could do the yank or select with a couple of mappings:
nmap ,y y/<C-R>/\zs<CR>
nmap ,v v/<C-R>/<BS>\zs<CR>
The ,y mapping uses the '/' register to pull in the last search term search, adds \zs to make the search point be the end and the yank proceeds up to that point. The ,v mapping does a visual selection, but has to delete the last character of the search (with <BS>) to make it end at the right place.
For what it's worth, you can simplify the %s/hell/whatever/gc that you suggested by refining your search with / and then using a shortened form:
/hell
:%s//whatever/gc
This is because :s uses the last search term by default.

Related

How to cut and then paste previous item in clipboard in vim?

In vim I copied the string ABCD to my clipboard. Now I want to replace certain words in a paragraph of text by doing c-e (this deletes and immediately puts me in insert mode). But when I paste it will paste the thing I just cut overriding my ABCD.
One solution I came up with is:
c-e
ctrl-o
"0P
But that just seems way too long. Is there a faster alternative to that?
While copying a string, place the cursor at the start of the string, then type
"ayw
where "a means the name of the register(its like a storage) and yw means copy(yank) the word into that register.
There are 26 registers, one for each letter of the alphabet.
When you want to paste the content, type
"ap
where p means to paste. Then use the vim repeat command . to repeat last action
You can store strings in different registers and paste them whenever u want.
to set contents of all registers type
:reg
For more information go to
Using Vim's named registers
Vim registers: The basics and beyond
Vim tips and tricks
You can use yankstack, which simplifies your use case.
This make it easy to cycle against your yank buffer when you paste.
I've personally remapped the keys to space (my leader key).
Here is an extract of my .vimrc:
if &runtimepath =~ 'vim-yankstack'
let g:yankstack_map_keys = 0
nmap <leader>p <Plug>yankstack_substitute_older_paste
nmap <leader>P <Plug>yankstack_substitute_newer_paste
endif
In your case, you just press space p right after having pasted your text. This will replace the word pasted with the last entry in your buffer. You can repeat pressingspace p if you need to go deeper in the history.
Use the "stamping" approach; this allows you to stay in normal mode and is just one keystroke. Add this mapping:
nnoremap S diw"0P
Then press S to "stamp" over any word your cursor is on with what you've yanked.
So, an example of the full routine:
1. ye
2. <move cursor over the word you wish to replace with the yank>
3. S
And, of course, if S is used, chose something else, e.g. ww.
This is a well known approach; see this documentation for variations and details: http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text.
The last thing you yanked is always available in register 0 so, in your case, you only have to do:
<C-e><C-r>0<Esc>
See :help i_ctrl-r.
But yeah, there's a much simpler way for one-off puts:
vep
or, if you need to do those changes several times:
ve"0p

Cycling and/or highlighting through a vim selection

The feature I am thinking of is kind of inspired by a feature that I really like about sublime text.
In sublime text, if you select a sequence of characters, it automatically puts a little box around it (to distinguish it from the word that you just highlighted). For me, this is very helpful because I can see and find specific things of the code much faster.
It would be awesome to have something similar to my vim environment. It does not have to be exactly the same as the one in sublime though, but it would be awesome if it were as similar as possible plus the additional feature to easy cycling through similar words.
Currently, what I am doing is highlight the work I want and then manually typing it to the search command /. It would be much better if I can just highlight it in visual mode and then automatically highlight similar words on the current screen with a different colour from the highlighting in visual mode and then have a quick key short cut to cycling through them, if I wished to do that.
I am not sure if a there exists a plugin or something that already does that, but that would cool! Ideally, I would want to to know as many details of the commands/changes to the vimrc file, so that I have the most control over this feature and be able to customize it as I wish.
You can get the highlighting you are looking for by enabling the hlsearch option:
:set hlsearch
It will highlight every occurrence of the last search pattern and thus work after all the following commands (and their relatives):
/foo<CR>
?bar<CR>
:s/fizz/buzz/g
*
#
You can use n to jump to the next occurrence in the direction of your search and N to do the same in the opposite direction.
To highlight every occurrence of the current word "without" moving the cursor, you can simply do:
*N
or:
*``
to jump to the next occurrence and jump back immediately.
Doing the same for visually selected text is a bit trickier but still possible…
either via a lightweight plugin like visualstar or The Search Party,
or with a tiny bit of crude vimscript in your ~/.vimrc:
" this function cleans up the search pattern
function! GetVisualSelection()
let old_reg = #v
normal! gv"vy
let raw_search = #v
let #v = old_reg
return substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction
" map * and # in visual mode so that they do the same as *N and #N in normal mode
xnoremap * :<C-u>/<C-r>=GetVisualSelection()<CR><CR>N
xnoremap # :<C-u>?<C-r>=GetVisualSelection()<CR><CR>N
My SearchHighlighting plugin changes the * command so that it just toggles the highlighting for the current word, without the movement to the next match (for which you can press n, or pass a count). This also works in visual mode, using the selection. I find this very handy for highlighting all matches.
There's also a mode that automatically highlights the current word / selection, like what many IDEs offer.
Other plugins
If you want more permanent highlighting, separate from searching, the Mark plugin offers that.
To get an orientation about the number of matches (without highlighting them), I have the SearchPosition plugin.

macvim: how to paste several times the same yanked word?

Each time i copy a word and want to replace it for several words, i do:
yank the word
enter visual mode, select the word to be replaced and paste the yanked word.
After this process, the replaced word will be yanked and cannot continue replacing new words bceause i lost the first yanked word. So, i must copy again the first yanked word.
Could anybody guide to me on how to achieve my goal in an efficient way? It could be enough if my yanked word would not get changed.
I would suggest explicitly using a register for your yank and paste.
"ayw or however you chose to yank your word.
"ap to paste.
In this case I've used the a register but you could use whichever suits you.
It has been answered before: Vim: how to paste over without overwriting register.
Overall, crude vnoremap p "_dP mapping will almost get you there, but it won't work well in a few edge cases (e.g. if a word you're replacing is at the end of the line).
The superior approach is to use this crazy-looking snippet (I wish I knew Vimscript at least half as good as the author of this):
" replace visual selection without overwriting default register
function! RestoreRegister()
let #" = s:restore_reg
return ''
endfunction
function! s:Repl()
let s:restore_reg = #"
return "p#=RestoreRegister()\<cr>"
endfunction
vnoremap <silent> <expr> p <sid>Repl()
Personally, I'd favour doing :s/word/replacement words/gc.
Alternatively, you could use "_de to delete the word to be replaced. "_ says use the "black hole" buffer to prevent losing the existing default buffer contents.
Perhaps a bit better than this is to yank the replacement words into an alternative named buffer (e.g. "a3ye), then you can delete the work to be replaced (de) and paste the named buffer "ap.
One addition to #Randy Morris answer: instead of specifying register explicitly in both cases, you can specify it only in the second one, see :h quote0 («Numbered register 0 contains the text from the most recent yank command...»). In this case using a register is better (as it is much easier to type), but if you say you are replacing words, you may want to use ciw<C-r>0 and then one . for each other word you want to replace.
I use this mapping to replace the currently selected text with default register without yanking it:
vnoremap <leader>p "_dP
I dont use yank, but ciw and then repeat with .
For instance:
Go to somewhere inside the word you want to replace.
Do ciw <type new word> Esc
Go to somewhere inside the next word you want to replace.
Press . to repeat the last replace.
Advanced:
You can also first find the word with /<word> and then use ciw <new word>.
Then you dont have to move to the word yourself before pressing . but you can just use n to go to the next (or N to go to the previous).

In vim, is there a way to rebind "*" to highlight the current word, without advancing to the next? [duplicate]

This question already has answers here:
Vim: search and highlight but do not jump
(14 answers)
Closed 4 years ago.
I often use * to highlight all instances of the current word, and the fact that it advances to the next word is pretty annoying. I'd like to disable this behavior, knowing that I can always use "n" if I actually need to advance.
Any insight?
EDIT: I should add that I'd like to avoid a screen redraw at all costs as it is visually distracting.
Try this:
nnoremap * :let #/ = "\\<<C-R><C-W>\\>"<CR>
(Assumes you have 'hlsearch' on). This just changes the current search pattern to the word under the cursor (surrounded by \< and \> to match word boundaries). If you have hlsearch enabled, it will highlight the word. n and N will then work as normal.
See:
:help :let-#
:help quote/
:help c_CTRL-R_CTRL-W
Try this Vim Tip. I find it very useful. The most interesting is that you can have more matches and every one in different color.
I cannot give an exact answer, but this Vim Tip tells you what you have to add to your .vimrc to simply highlight the word under the cursor when Vim is in idle state.
Works without any key-remapping...
You can remap it to return:
nnoremap * '*N'
(but this redraws the screen)

How to emulate Emacs’ transpose-words in Vim?

Emacs has a useful transpose-words command which lets one exchange the word before the cursor with the word after the cursor, preserving punctuation.
For example, ‘stack |overflow’ + M-t = ‘overflow stack|’ (‘|’ is the cursor position).
<a>|<p> becomes <p><a|>.
Is it possible to emulate it in Vim? I know I can use dwwP, but it doesn’t work well with punctuation.
Update: No, dwwP is really not a solution. Imagine:
SOME_BOOST_PP_BLACK_MAGIC( (a)(b)(c) )
// with cursor here ^
Emacs’ M-t would have exchanged b and c, resulting in (a)(c)(b).
What works is /\w
yiwNviwpnviwgp. But it spoils "" and "/. Is there a cleaner solution?
Update²:
Solved
:nmap gn :s,\v(\w+)(\W*%#\W*)(\w+),\3\2\1\r,<CR>kgJ:nohl<CR>
Imperfect, but works.
Thanks Camflan for bringing the %# item to my attention. Of course, it’s all on the wiki, but I didn’t realize it could solve the problem of exact (Emacs got it completely right) duplication of the transpose-words feature.
These are from my .vimrc and work well for me.
" swap two words
:vnoremap <C-X> <Esc>`.``gvP``P
" Swap word with next word
nmap <silent> gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<cr><c-o><c-l> *N*
Depending on the situation, you can use the W or B commands, as in dWwP. The "capital" versions skip to the next/previous space, including punctuation. The f and t commands can help, as well, for specifying the end of the deleted range.
There's also a discussion on the Vim Tips Wiki about various swapping techniques.
In the middle of a line, go to the first letter of the first word, then do
dw wP
At the end of a line (ie the last two words of the line), go to the space between the words and do
2dw bhP
From the handy Equivalence of VIM & Emacs commands
You could add shortcut keys for those by adding something like the following to your vimrc file:
map L dwwP
map M 2dwbhP
In that case, SHIFT-L (in command-mode) would switch words in the middle of the line and SHIFT-M would do it at the end.
NB: This works best with space-separated words and doesn't handle the OP's specific case very well.
There's a tip on http://vim.wikia.com/wiki/VimTip10. But I choose to roll my own.
My snippet has two obvious advantages over the method mentioned in the tip: 1) it works when the cursor isn't in a word. 2) it won't high-light the entire screen.
It works almost like emacs 'transpose-words', except that when transposition is impossible, it does nothing. (emacs 'transpose-words' would blink and change cursor position to the beginning of current word.)
"transpose words (like emacs `transpose-words')
function! TransposeWords()
if search('\w\+\%#\w*\W\+\w\+')
elseif search('\w\+\W\+\%#\W*\w\+')
endif
let l:pos = getpos('.')
exec 'silent! :s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/'
call setpos('.', l:pos)
let l:_ = search('\(\%#\w\+\W\+\)\#<=\w\+')
normal el
endfunction
nmap <silent> <M-right> :call TransposeWords()<CR>
imap <silent> <M-right> <C-O>:call TransposeWords()<CR>
You can use dwwP or dWwP as Mark and CapnNefarious have said, but I have a few notes of my own:
If the cursor is on the first letter of the second word, as in the example you gave, you can use dwbP (or dWbP to handle punctuation);
If the cursor is in the middle of the word, you can use dawbP/daWbP.
There's a transpose-words script on vim.org that works beautifully.

Resources