How to most easily duplicate a piece of text in Vim? - vim

Let's say I have a piece of text within my code which I want duplicated.
What I've been doing so far is to move to the start, press v to enter visual mode, then move to the end, press y to yank the text, move one character back and then p to put it there.
Is there an easier way? Something like:
Select text with v.
Press some sequence of commands and there is - it's duplicated.

If it's the "move one char back" you're trying to avoid, press P rather than p for the "paste" operation.

You can always define custom macros / shortcuts on the fly.
map ! "+yP
will allow you to enter visual with v, mark the text, and use ! to duplicate it once (for extra duplicates, repeat p or P as needed.

Related

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.

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

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++).

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.

Copy lines in visual mode in vim

Just a question about copy/paste workflow in gVim.
Right now I'm working on a document. I want to select some lines of code and copy and paste. I'm using gVim (Windows).
If I use Control + C and Control + V, gVim takes sometimes 2, 3 seconds to paste...
The other way is using, Shift + v (right, now I'm in visual mode), then I keep the Shift key pressed and drag my selection with the mouse. Now I can copy with 'y' or 'c' and paste with 'p'. This is faster, but I have to keep the Shift key pressed.
Is there a way to use the mouse to select text in visual mode without keeping the Shift key pressed? I want to use y/d/p instead of slow Control + c and Control + v. Is there a better workflow or setup that I should try?
Thanks,
[]'s
Mateus
Stay away from using CTRL-C/V and your mouse in vim, or at least until you're familiar with vim's way of text handling.
--
SHIFT-V to enter visual line mode
Press 3j to go down 3 lines, or press j 3 times
y to yank/copy, x to cut, p to paste after cursor, P to paste before cursor.
Using hjkl will improve your workflow greatly as you don't have to move your hands from the typing position to the arrows and the mouse.
There are a couple of ways to yank multiple lines without entering visual mode. One of which is to type <action><number><direction>. For example, y3j means to yank from your current row to 3 rows down.
If you want to yank the entire paragraph or sentence you're in, type yip (yank IN paragraph) or yis (yank IN sentence) respectively. You can also do yi" (yank IN ") or ya" (yank AROUND ") to yank everything that's surrounded by " on your current position.

How to delete selected text in the vi editor

I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Forget the mouse. To remove 5 lines, either:
Go to the first line and type d5d (dd deletes one line, d5d deletes 5 lines) ~or~
Type Shift-v to enter linewise selection mode, then move the cursor down using j (yes, use h, j, k and l to move left, down, up, right respectively, that's much more efficient than using the arrows) and type d to delete the selection.
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
As I said, either use Shift-v to enter linewise selection mode or v to enter characterwise selection mode or Ctrl-v to enter blockwise selection mode. Then move with h, j, k and l.
I suggest spending some time with the Vim Tutor (run vimtutor) to get more familiar with Vim in a very didactic way.
See also
This answer to What is your most productive shortcut with Vim? (one of my favorite answers on SO).
Efficient Editing With vim
Do it the vi way.
To delete 5 lines press: 5dd ( 5 delete )
To select ( actually copy them to the clipboard ) you type: 10yy
It is a bit hard to grasp, but very handy to learn when using those remote terminals
Be aware of the learning curves for some editors:
(source: calver at unix.rulez.org)
If you want to delete using line numbers you can use:
:startingline, last line d
Example:
:7,20 d
This example will delete line 7 to 20.
Highlighting with your mouse only highlights characters on the terminal. VI doesn't really get this information, so you have to highlight differently.
Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x.
To select lines at a time, press shift+v.
To select blocks, try ctrl+v. That's good for, say, inserting lots of comment lines in front of your code :).
I'm OK with VI, but it took me a while to improve. My work mates recommended me this cheat sheet. I keep a printout on the wall for those odd moments when I forget something.
Happy hacking!
When using a terminal like PuTTY, usually mouse clicks and selections are not transmitted to the remote system. So, vi has no idea that you just selected some text. (There are exceptions to this, but in general mouse actions aren't transmitted.)
To delete multiple lines in vi, use something like 5dd to delete 5 lines.
If you're not using Vim, I would strongly recommend doing so. You can use visual selection, where you press V to start a visual block, move the cursor to the other end, and press d to delete (or any other editing command, such as y to copy).
If you want to remove all lines in a file from your current line number, use dG, it will delete all lines (shift g) mean end of file

Resources