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.
Related
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.
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'[
Normally, I am using <shift-v> and <up(or)down> to select some lines of code in vim. Then I use <y-y> to yank the lines. Then, I do <p> for pasting the code at the desired location.
I used this step for a long time and had no issues. Recently, I started noticing that when I yank more than 10 lines and paste them, only 2 of the lines are pasted.
What could be the reason for this? If anybody knows alternate ways of selecting multiple lines, yanking and pasting it, please let me know.
I have never seen where lines get lost when I paste them. However, when you use visual mode (shift-v), usually you only need the first "y" to yank lines. It's likely that when you press the second "y" and move down, you are actually yanking the top two lines (which is what is supposed to happen). You probably just need to stop pressing "y" twice. (Hint: press "y" once in visual mode, or twice when you want to yank the current line in normal mode).
You can yank multiple lines in a variety of other ways. For example, 14yy will yank 14 lines. If you use gvim, you can use the mouse cursor to select text. You can also do y14j to yank the next fourteen lines, since y, followed by a movement command, copies everything in that movement command.
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.
Most time when I copy or delete some code in Vim use yy or dd, I also got the indent spaces. Is there a quick command that I can yank a line without leading or trailing spaces?
I'm not a wizard, but:
^v$gey
works for me. You can always make an alias.
EDIT: here's a better one that doesn't rely on visual mode.
^yg_
There's another way to solve your implied problem. After you yank line(s) into the buffer, you can then paste them using the appropriate indent using ]p or ]P. These paste commands automatically adjust the indent of the pasted line(s) to match the indent of the line where the cursor is.