Disable selection range insertion in vim macro - vim

I want to save vim macro for reuse. Part of what I want to achieve is:
Make visual selection of part of a line
Search & replace in it with :s/\%Vsearch/replace/e
But the automatic insertion of selection range ('<,'>) after : is causing me issue. How can I disable it or bypassing it for this command?

Use <C-u>:
:<C-u>s/\%Vsearch/replace/e
If you want to use <C-u> in a saved macro you will need to use its literal notation, ^U, that you can obtain by typing <C-v><C-u> in insert mode.
See :h i_ctrl-u.

Related

Vim: Paste seems to be acting weird. Carrying out the keyboard shortcuts of characters that are copy pasted

My vim seems to be acting weird today.When I try to copy paste it is carrying out the vim keyboard shortcuts related to the characters I have copied and pasted. e.g. If i copy and paste two letter ks they will not paste but the cursor will jump up two lines. I think it has something to do with SHIFT+CTRL+V taking me out of insert mode and into normal mode. Then paste is putting the kk as input into normal mode.
How do I get back to the correct copy paste functionality/ stop making shift+ctrl+v exit insert mode? Thanks!
My cluster terminal went into bracketed paste mode while in vim somehow.
Typing reset into the terminal outside of vim fixed it.

how do I do global substitutions?

In vsvim for visual studio, I can highlight a selection and type :s/hello/world/g to get all occurrences of hello inside my selection with world but this isn't working in VsCode Vim, is this a feature not in this version of vim or is the special key sequence different?
After selecting some lines and pressing : you should find that the Vim command line has been defaulted to '<,'> - this is a Vim range that refers to the selected lines. You need to keep this range as part of the command and type the s/... afterwards i.e.
:'<,'>s/hello/world/g
or if you just want to replace all occurrences across the whole file you can skip highlighting the lines and use the % range which indicates the whole file:
:%s/hello/world/g
If curious, you can learn more about some of the other ranges that can be used.

How to copy a selected word to the clipboard using VIM?

I would like to copy a selected word that the cursor is on to the clipboard. Is there a way to do this.
If support for clipboard is built into your Vim, clipboard is mapped to register *, so you use it the same as any other register. So, for example, to copy the word under cursor to clipboard you do "*yiw. To copy current line "*yy. To paste from clipboard "*p
Perhaps you just want to do this:
viwp
which will visually select a new word, and paste over it.
Now, if you don't want to lose your register when doing this, you can also put in your vimrc:
xnoremap p pgvy
First at all you must check if the clipboard feature on your VIM is enable or not. For that use the --version parameter (vim --version)
If not (-clipboard), you can use a gtk vim edition or compile VIM manually.
If yes (+clipboard), in normal mode "+yiw on your word for copy it in your clipboard.
Follow instruction in https://stackoverflow.com/a/65666057/9384511
Once you have set up your vim as per above link, you will be able to copy single or multiple lines from vim to clipboard by pressing Ctrlc. And paste from clipboard to vim by pressing Ctrlp
Now to copy just a single word to clipboard press bveCtrlc

In vim, how to visual select previously pasted text

I usually visual select a block and copy/cut it somewhere else, then I found myself always formatting the pasted text, so is there a way to quickly visually select the text again.
Every command that modified the buffer (and yanks) will set the '[ and '] marks around the changed area. So you can reformat your pasted block via:
`[v`]=
Some people go so far as to use the following mapping to reselect last modified chunk of text:
nnoremap <expr> gV '`[' . getregtype()[0] . '`]'
With this mapping you can just do gV= and format your just pasted text.
However if you have the unimpaired.vim plugin and you are pasting linewise you can use the =p command it provides. This will paste and then reformat the text in one go. It also provides some other alternative paste commands >p for example will paste one indent level deeper.
For visual mode, gv is the standard way to reselect the previous area.
If you want to toggle between start and end positions of the area, just press o.
As other answers have mentionned it, you can apply standard = command on this reselected area.
It works well after a p or P paste.
The advantage is that you do not need any custom mapping.
The way I use is straightforward. The cursor is at the beginning of the pasted text after pasting. The press V to switch to visual selection, the press '] to go the end of the pasted.
They are 3 key presses. If it is too long then you can do mapping for p
map p pV'[
map P PV'[

Yank part of multiple lines and paste

Is there a way I can select a portion of multiple lines in vim and then paste them at the end of another block of lines. For instance if I want everything after the equal sign from:
qw=12345
er=23435
and pasted into:
ty=
ui=
What would I have to do?
This seems real simple, but for some reason, I am completely stuck.
You are looking for block selection mode.
Please note the different keybinding for Windows platforms.

Resources