I have this problem where I try to yank/cut and paste a block of text with Vim and it has inconsistent behavior.
What happens is this:
In this text bellow, I what to cut the middle column and paste after the third. So I block-select (ctrl-v) the text, cut it (x), take the cursor to the end of the third column (now 2nd) and paste (p)
A 1 a
B 2 b
C 3 c
D 4 d
Sometimes Vim does it right and the result is this:
A a 1
B b 2
C c 3
D d 4
and some times, the result is this:
A a
1
2
3
4
B b
C c
D d
I need to control that behavior instead of being a hostage of it... How do I control this behavior?
Well, one answer is the UnconditionalPaste plugin (https://vimawesome.com/plugin/unconditionalpaste)
with a 'gbp' you paste a block inline (I remapped to [LEADER]pb)
it works...
I recommend you reading the help topics for visual selection,
:h visual.txt. The section on visual-operators and its notes might
be particularly useful. Selection behavior is pretty consistent and easy
to understand though. If you cut or copy something, expect it to be
placed in the same mode: characters, lines, or blocks.
I what to cut the middl ecolumn and paste after the third. So I
block-select (ctrl-v) the text, yank it (y), take the cursor to the
end of the third column (now 2nd) and paste (p)
That's not how you should do it. To "cut" the middle column, as you
describe, you came to use x and not y. Yanking will just copy it and
leave it there.
Regarding your last example where content was pasted in a linewise
fashion, that should not happen when using p since it conserves the
blockwise information. The only possible explanation that comes to my
mind is you deleted a column and used :put to paste it. That will
convert it to a linewise paste. Alternatively:
You yanked something likewise (say from V) and you are trying to
paste it blockwise
Your example does not translate well your problem
There is some sort of plugin or configuration affecting this
Random unknown keys were slammed in the process
Related
I would like to paste a column of text at the end of irregular-length lines.
For example, I would like to paste the following:
SRR447882.fastq.gz
SRR447883.fastq.gz
SRR447944.fastq.gz
at the end of these lines:
TIL01_
TIL01_
TIL04-TIP285_
Many times in the past, I simply create enough space on the first line that pasting will not come before the end of the existing text in the longest line. But then I need to go back and remove whitespace.
I have tried googling "vim column paste irregular length rows" and similar queries.
You could try to do the following four steps:
block-wise select the first 3 lines (you want to paste later), and press y
line-wise select (V) the 3 lines ending with _, press :right
then move cursor to the end of the first line($), paste the yanked text
gv re-select the lines, press :left
It looks like this:
You can do it like this:
Start on the first line of the second block
qq, start recording the q macro
4k, go up four lines
d$, delete till the end of line
4j, go back to the previous line
$p, paste the line at the end of the line
q, stop recording the macro
jVG, go down one line and select the remaining lines
:norm! #q, apply the macro to the selection
It does however leave space where the previous text was. #Kent one's is still easier.
My UnconditionalPaste plugin has (among others) gBp / gBP mappings that paste register contents as a minimal fitting (not rectangular) block with a jagged right edge.
demo
Step 1 - goto to the start of the SPP... lines, then start Visual mode linewise with V (capital V), press j two times to get the desired lines selected then press y.
Step 2 - goto the start of the TIL0... lines, then start Visual mode linewise with V (capital V), press j two times to get the desired lines selected then type...
:s;$;\=' ' . split(#")[line('.')-line("'<")];g`
and press Enter.
Here's another way to do it with a different feel to it:
set ve=all to permit insert/paste at arbitrary columns past eol. Block-copy your source text, then at your first target line paste it with 100|P (100 being any column number longer than your target lines), then :'[,']s, *\%100c,,
If you do p instead of P you'll get a space separator.
I have this text:
a .55
b .23
c .12
d .45
And I want to align the second column, to have this:
a .55
b .23
c .12
d .45
My first try was to put cursor in the first line, at the column where I want to align the text and press dt., and later pressing j and . I can repeat it for all the lines, but since I have a lot of lines, I want to make a visual block and repeat the action for all the lines, is it possible to repeat a command in visual block mode? If not, which are the alternatives?
For all things aligning you probably want to use specialized plugins, e.g. Tabular or Align.
If you don't want to use plugins, I see 2 other possibilities:
1)
mark lines in visual line mode (V)
:normal 5ldt.
2)
mark lines in visual line mode (V)
:s/\w\zs\s\+\ze\./ /
With my visualrepeat plugin, you can do this: After modifying the first line with dt., select the following lines in linewise visual mode Vjj, move the cursor to the column where you want to repeat 4l, and repeat ..
(Though I agree with mhinz that an alignment plugin is preferable in this particular case.)
In vi/vim editor, I need to copy a block. There are many ways, but one way is very quick.
label the first line by some way,
then label the end line by some way,
then put some command to copy the labeled lines.
then copy, may using 'p', but not sure.
Anybody know the commands (not yy or 10yy)?
just use V to select lines or v to select chars or Ctrlv to select a block.
When the selection spans the area you'd like to copy just hit y and use p to paste it anywhere you like...
Their Documentation says:
Cut and paste:
Position the cursor where you want to begin cutting.
Press v to select characters (or uppercase V to select whole lines).
Move the cursor to the end of what you want to cut.
Press d to cut (or y to copy).
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after.
Copy and paste is performed with the same steps except for step 4 where you would press y instead of d:
d = delete = cut
y = yank = copy
Another option which may be easier to remember would be to place marks on the two lines with ma and mb, then run :'a,'byank.
Many different ways to accomplish this task, just offering another.
I found the below command much more convenient. If you want to copy lines from 6 to 12 and paste from the current cursor position.
:6,12 co .
If you want to copy lines from 6 to 12 and paste from 100th line.
:6,12t100
Source: https://www.reddit.com/r/vim/comments/8i6vbd/efficient_ways_of_copying_few_lines/
It sounds like you want to place marks in the file.
mx places a mark named x under the cursor
y'x yanks everything between the cursor's current position and the line containing mark x.
You can use 'x to simply move the cursor to the line with your mark.
You can use `x (a back-tick) to move to the exact location of the mark.
One thing I do all the time is yank everything between the cursor and mark x into the clipboard.
You can do that like this:
"+y'x
NOTE: In some environments the clipboard buffer is represented by a * in stead of a +.
Similar questions with some good answers:
How to copy/paste text from vi to different applications
How to paste from buffer in ex mode of vim?
Keyboard shortcuts to that are:
For copy: Place cursor on starting of block and press md and then goto end of block and press y'd. This will select the block to paste it press p
For cut: Place cursor on starting of block and press ma and then goto end of block and press d'a. This will select the block to paste it press p
You can do it as you do in vi, for example to yank lines from 3020 to the end, execute this command (write the block to a file):
:3020,$ w /tmp/yank
And to write this block in another line/file, go to the desired position and execute next command (insert file written before):
:r /tmp/yank
(Reminder: don't forget to remove file: /tmp/yank)
I'm running vim 7.3 on a Mac 10.7.2 and I'm having some trouble cutting and pasting several lines.
On my old Linux setup (which was stolen so I don't know versions), I could type "dd" multiple times and then "p" would yank all of them back. For example: type: "dd dd" and two lines would be deleted. Now type "p" and both lines are pasted back into the buffer.
I know I can accomplish what I want by typing "2dd", and then "p" - but I would like to be able to "dd"-out lines without counting the number of lines ahead of time.
Any ideas?
Have you considered using visual mode?
You could just go:
Press V
Select everything you want to cut without counting
Press d
Go to where you want to paste
Press p
This should yield approximately half as many keystrokes as the dd method since you press one key per line rather than two. Bonus points if you use 5j (or similar) to select multiple lines at a time.
You could type:
d<n>d
where <n> is the number of lines that you want to cut, and then you could paste them with:
p
For example, to cut and paste 3 lines:
d3d
p
To cut and paste by line numbers (do :set number to see the line numbers), for lines x to y do:
:x,yd
or if your cursor is already on line x, do
:,yd
Then go to where you want to paste and press p
Not sure if this is close enough to what you're trying, but one thing you could do is use a specific register, and capitalize your register name. That tells vim to append to the register rather than replace it, so if you have the lines:
one
two
three
you can enter
"qdd
"Qdd
"Qdd
and then subsequently if you enter
"qp
it will paste back the original lines
To copy and paste 4 lines:
y4y (with the cursor on the starting line you wanna copy)
p (with cursor on the line you wanna paste after)
I agree with #Ben S. that this is the preferred way to accomplish this but if you are just looking to replicate your old behavior you can remap dd to append to a specified register, and then map p to paste from that register and clear it.
This will have the disadvantage of causing p to only work with things deleted using dd (using d} to delete to the end of the paragraph would not put the text in the correct register to be pasted later).
Add the following to your vimrc
noremap dd "Ddd "Appends the contents of the current line into register d
noremap p "dp:let #d=""<CR> "Pastes from register d and then clears it out
if you don't want pasting to clear out the contents of the register
noremap p "dp "Paste from register d
but this will cause that register to grow without ever clearing it out
I'm wondering if there's a command in Vim I'm missing that does columnar motion. What I'm thinking of is common in some spreadsheets, where there's a key combination that will take you downward in the column to either the next blank cell, or if you're in a blank cell then it will take you to the next non-blank cell. Some text may illustate:
a-------
-------
-------
b-------
c-------
-------
-------
d-------
e-------
-------
What I'm looking for is most efficient way to move from a to b, from b to c, from c to d, from d to e. I know this can be done using / searches, but is there a more efficient way using something analous to the line-based motion commands? Seems like there should be, but I can't find it.
I'm not sure if this fits your request exactly but vim has sentence, paragraph, and section movement keys built-in. In your case, I think you probably want the paragraph ( '{' and '}' ) or section ( '[' and ']' ) motion keys.
Try this to jump between non-empty lines:
:nmap j /^.\+$<CR>