How do I paste text at multi-line selection in vi? - vim

I know how to use this with manual typing:
Use Ctrl+V to enter visual block mode
Move Up/Downto select the columns of text in the lines you want to comment.
Then hit Shift+i and type the text you want to insert.
Then hit Esc, wait 1 second and the inserted text will appear on every line.
But i don't want to want type the text. I want just to paste it.. (because is a long string..)
Thanks, Mor.

Once you are in insert mode (after I), you can press <C-r>" to insert the content of the default register or <C-r>a for register a to z.
You can also use completion in that context: <C-n> for example.

If the text you want to use is in a register, use <c-r> (CtrlR). So, after you press I, instead of typing, press CtrlR, and the register name you want.
Since the OS clipboard is in the + register, you would do: <c-r>+ (CtrlR++).

Related

Use visual character-wise selection as auto-fill for Vim substitute command

Imagine that [ ] represents a selection. So given
This is pi with the 10th through 20th digits obscured: 3.14159265[3589793238]46264338327950288419716939937510...
the visual selection would be 3589793238.
In Sublime Text, when you have some text selected, and then press Ctrl+h to start the search/replace feature, it will auto-fill the search box with the currently selected text. Is there a way to emulate this in Vim? That is, assuming I am in character-wise visual mode and have made the selection indicated above, I would then press some shortcut combo and Vim would yield :%s/3589793238/ in the command line. That way I could simply complete it like so :%s/3589793238/___/ to replace 3589793238 with ___ throughout the currently open file (without having to manually type 3589793238 into the Vim command-line).
In command mode, you can input the content of any register with <C-R> followed by its name. So you could yank the currently selected text (yanked text is stored in register 0) and then input it with <C-R>0 when writing your substitute command.
To fully automate this, you could add a mapping like so:
xmap <leader>s y:%s/<C-R>0/

Is it possible to simultaneously get the contents of a visual selection into a register and replace all the characters?

Consider the following text file.
Replace and yank this portion Ignore this portion
Suppose I have visually selected the part that says Replace and yank this portion.
I can take one of the following actions at this point.
I can use y to yank the contents into a register, but this destroys the visual selection.
I can use rx to replace each of the characters with an x, but this also destroys the visual selection.
Is it possible to simultaneously put the visual selection into a register and replace each of the characters in the visual selection with an x?
That is, I'm looking for a sequence of commands that result in the selected text being in a register, and each character in the selected text replaced by x. I'm not picky about which register.
Immediately after posting this question, I realized that all I needed was to be able to re-select the text that was just selected.
A quick Google search led to using gv for re-selection.
Thus, the final command sequence to achieve the desired effect is ygvrx. This will first yank the sequence into the register, re-select the previous selection, and then replace the characters.
Visually select the text and press c for change. Type the text you want and press <esc>. The text that was there before (in this case Replace and yank this portion) is now in your "" register, so you can just hit p as soon as you want to paste it.
type :h reg to see a list of all registers and what text you have inside them.

Vim: How do I paste a column of text (from clipboard) after a different column of text?

I'm testing proxies with my script that looks like that:
$proxy = "http://name:pass#133.245.122.91:80";
$proxy2 = "http://name:pass#133.245.229.241:80";
$proxy3 = "http://name:pass#133.245.113.197:80";
...
$proxy100 = "http://name:pass#133.245.212.197:80";
I get new proxies by email so can I copy new proxies and insert it instead of the old ones by Vim:
"http://name:pass#133.245.122.91:80";
"http://name:pass#133.245.229.241:80";
"http://name:pass#133.245.113.197:80";
...
"http://name:pass#133.245.212.197:80";
Right know I'm doing it as was described on this page How do I paste a column of text after a different column of text in Vim?
Use visual block (ctrl-v) to cut the letter column. Then move to the
first line of the number column. Move to the end and make one space.
Then paste the letter column.
I'm curious, how it can be done without extra step, just paste data from clipboard?
The short version: you can't. There are ways around it, but they aren't necessarily simpler. Longer version follows.
Vim has three ways of marking regions of text: linewise (you start this mode when you press V), characterwise (triggered when you press v), and blockwise (when you press Ctrl-v). The marked region is copied to a register, and this register has an attribute, the "type", that reflects the way you did the marking, linewise, characterwise, or blockwise. What happens when you paste from a register depends on this type.
Now, when you copy from system's clipboard the result is stored in the * register, and the type is always set to linewise. Thus you can't paste a column mode "without extra step". You can however set the type of the * register to blockwise before pasting:
call setreg('*', #*, 'b')
Thus, replacing the list of your proxies would go something like this:
copy the new list to clipboard, from the mail message
run :call setreg('*', #*, 'b') to set the type of the * register to blockwise
go to the old list, press Ctrl-v and mark it; assuming there's nothing else in the file aside from the proxies, a Vim golfer's way of doing that might be something along the lines of:
f" - go to the first "
Ctrl-v - start marking
?;Enter - go to the last ;
paste the new list over the selection, with "*p.
You can simplify the last step a little, by making the * and + registers always refer to the same value. To do that, add this to your vimrc:
set clipboard=unnamedplus,autoselect,exclude:cons\\\\|linux
With this setting the incantation becomes:
copy the new list from mail
run :call setreg('+', #+, 'b')
go to the old list and mark it with Ctrl-v as above
press p to paste the new list over it.
You don't need this dance if you have the new list in a file that you can open with Vim:
open the file with the old list
open the file with the new list in a separate copy of Vim
mark the new proxies with Ctrl-v and yank them with y
in the other Vim mark the old list with Ctrl-v and paste the new one over it with p.
This still involves using the system clipboard under the hood, but the second copy of Vim takes care of setting the type of the relevant register to blockwise.
I don't know any direct way to do this. If it is really important to you, you will probably need some set up before you do the actual editing, which only adds to the amount of typing you have to do (however you can add commands to your vimrc to make it permanent). You might set up some keyboard macro, or use the following map command:
:imap <CR> <Esc>j011lC
Now move to the first " sign and press C, then start pasting (only works in a terminal). Whenever you paste a newline, the map will move you to column 11 in the next line.
Remember to :iunmap <CR> when you are done.

Vim: How to insert in visual block mode?

How can you insert when you are in visual block mode (by pressing ctrl-V) in Vim?
Try this
After selecting a block of text, press Shift+i or capital I.
Lowercase i will not work.
Then type the things you want and finally to apply it to all lines, press Esc twice.
If this doesn't work...
Check if you have +visualextra enabled in your version of Vim.
You can do this by typing in :ver and scrolling through the list of features. (You might want to copy and paste it into a buffer and do incremental search because the format is odd.)
Enabling it is outside the scope of this question but I'm sure you can find it somewhere.
press ctrl and v // start select
press shift and i // then type in any text
press esc esc // press esc twice
You might also have a use case where you want to delete a block of text and replace it .
Like this
Hello World
Hello World
You can visual block select before "W" and hit Shift+i - Type "Cool" - Hit ESC and then delete "World" by visual block selection .
Alternatively, the cooler way to do it is to just visual block select "World" in both lines. Type c for change. Now you are in the insert mode. Insert the stuff you want and hit ESC. Both gets reflected with lesser keystrokes.
Hello Cool
Hello Cool
if you want to add new text before or after the selected colum:
press ctrl+v
select columns
press shift+i
write your text
press esc
press "jj"

How to paste something between html tags in Vim?

Pressing p pastes things bellow the current line, dit deletes things inside html tags. How do I paste something inside html tags?
Nor here
<p>I want to paste something here</p>
Not here
I usually just do vitp which visually selects the inner contents of the tag, then pastes over what is selected.
Works for me.
The result of pressing P and p depends on what you have in the selected register at the time. If you delete or yank one or more entire lines (e.g. with dd, Y or Vd commands), then pressing P will insert the contents of your register on the line above the current line, whereas p will insert on the line below the cursor.
If you delete or yank a section of text less than a line (e.g. with the D, or yw commands), then P will insert the contents of your register directly before the current cursor position, and p will insert directly after the cursor (i.e. on the same line).
If it helps, you could consider the linewise selection as being analogous to block html elements (such as <div>), and characterwise selection as being analogous to inline html elements (such as span).
So to answer your question: it depends. Supposing you have a linewise section of text in the register, you would want to break the target tag onto two lines before doing the paste operation. In your example, rather than doing dit to delete the contents of the tag, do cit to delete the same section and go into insert mode. Hit return once, to insert a new line, then esc to go back into normal mode, then P to insert your linewise register above the line with the closing tag.
If you didn't want to split the tag onto multiple lines, you would instead have to make sure that you yanked a characterwise selection into the register. Then you could run:
"_ditP
"_ deletes the text into the black hole register, ensuring it doesn't overwrite what is in your default register. dit deletes the contents of the tag, and P pastes the contents of your default register before the cursor position.
remove the current content between the tags with the command
cit
that will 'change in tags' and once that content is gone, you can paste with middle click or if you need go back into command mode and use your normal p/etc.
vitp should handle a linewise paste.
You can press "v" for visual, then go to where the cursor is, and press p or P.

Resources