Is there a command in vim which will delete n lines in the up direction.
I know I can use 4dd which will delete 4 lines downwards.
In VIM, 3dk would delete 4 lines in the upward direction. Further documentation can be found at http://www.vim.org/docs.php
V3kd would do it.
Thats "V" to enter visual line select mode, "3k" to move up 3 lines, and then "d" to delete the 4 lines you have selected.
You can do it with a backwards range.
:-4,.d
Deletes from minus 4 lines to current. But this is an ex mode command.
Position the cursor where you want to begin cutting.
Press v (or upper case V if you want to cut whole lines).
Move the cursor to the end of what you want to cut.
Press d.
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after.
http://vim.wikia.com/wiki/Copy,_cut_and_paste
stand at the end of the last line
hold Backspace and wait until the last character of the first line is deleted
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.
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)
There are a lot of different ways in which one can yank complete single/multiple lines. Is there a way in which we can copy partial lines in vi, like just 10 characters of the line.
I would guess the most common partial yanks are:
yaw: yank the word the cursor is currently in
2yaw: yank the word the cursor in currently in and the next (2 words total)
ya(: yank the matched parentheses containing the cursor
yf.: yank from the cursor to the next .
y$: yank from the cursor to the end of the line
Any movement keys can be used.
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
Resource:
vim.wikia.com: Copy, cut and paste
You can do yMovement, so for 10 characters: y10l yanks 10 characters from (and including) the current cursor position
I prefer just pressing the v key, then using the cursor keys to move your selection. Then press the y key when you satisfied to yank the selection.
you can also do
yt<char> - yank 'till char - i use that a lot
or y/<pattern>/ - yank until pattern
Sure, with the cursor at the beginning of the line, type:
y10l
This yanks 10 characters to the right. If you need to do this repeatedly for some reason, just add this temporary kep mapping:
:noremap ,m ^y10l
Which will yank the first 10 chars of any line every time you press ,m
If you have multiple lines to copy, try visual block mode,
" beginning of line
C-v
" up-down move 10j or 5k
10l
" copy & paste
y
p
more detail, see wiki
This is one place mouse may actually beat keyboard, especially if the current mouse cursor is far from your copy target, or if you want to select multiple lines with partial start line or end line.
Use :set mouse=a to enable mouse support. Then select whatever irregular text blocks with mouse, and then press y
When I cut and paste in VIM by pressing v, and go to the end of the line using $, and press d, the next line gets moved up to the same line I'm cutting.
How do I stop this?
It moves up because you have removed all the characters including line return/feed.
There are multiple solutions as usual with Vim. There is no "one true way" but you can try the following commands.
You can use D (capital) in normal mode which will erase everything until the end of line.
See :help D
Using another motion
What you could do instead of using $ to move to the end of the line, use g_. It will move to the last non blank character of the line and won't select line return.
See :help g_
So vg_d should work as you want.
Using Replace
Alternatively, what you could do instead of cutting, you could replace the erased character by a blank using the space character.
So v$rSPACE should work to erase but it will not save the replaced characters in register (for pasting later for example).
To cut everything from current cursor position until the end, use C.
:he C will help you:
Delete from the cursor position to the end of the
line and [count]-1 more lines [into register x], and
start insert. Synonym for c$ (not |linewise|).
Doing so will cause the current line (assuming you are on the start of the line when hitting C) to become empty and the content is (by default) yanked into register "
Edit:
As Xavier notes in his comment (and his answer), the same could be achieved with D. It also cuts everything from current cursor position until the end of the line but doesn't go in insert mode after doing it.
If you use these keystroke sequence then next line would not move up.
v $ h d
It is moving up because EOL character $ is also getting deleted without moving cursor 1 character back.
Just skip the visual mode and swap the other two commands, ie. press d $.
This is shorter than your starting one and doesn't break your tradition introducing other keystrokes you may not be familiar with.
As a novice vim user, I used d[count]<Enter> to delete lines.
It striked me as odd that there were always count+1 lines deleted.
If I wanted to delete 2 lines, I typed d1, 3 lines took d2, ...
I finally took the time trying to understand why and it appears I should have been using :d<count>.
That does beg for the question though, why is :d1<Enter> <> d1<Enter>
d<count> in normal mode doesn't do anything, because the count isn't followed by a motion. So presumably you've been hitting d<count><Enter>, in which case the motion associated with d is <count><Enter>, which moves <count> lines downward. Since <Enter> is a linewise motion, the d will also be linewise, deleting all lines from the current one to the line <count> downward, inclusive.
The command you actually wanted is <count>dd.
d{motion} deletes the text that {motion} moves over. When you type 3<ENTER>, the cursor moves 3 lines below the current and therefore d3<ENTER> deletes that area.
:d[count] simply deletes [count] lines.
The difference is that {motion} is not the same as count.
To get around that, you could use the visual mode and select what you're going to delete and then simply press d.