1

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?

1
  • 1
    Vim is probably not the right tool for the job. You should copy the proxies from your email and paste them into a text file and process that file with your script.
    – romainl
    Aug 8, 2015 at 22:47

2 Answers 2

6

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:
    1. f" - go to the first "
    2. Ctrl-v - start marking
    3. ?;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.

0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.