I'm trying to learn VI/VIM. I would like to know how to deleted the text from my cursor to some other spot in the file. I know how to delete a line (dd) and multiple lines (5dd) and to the end of a line (d$), but not, for example, from the cursor to the middle of the next line or the middle of the next two or three lines.
Thanks for any tips.
Cheers!
You can use any motion after a d. For example, to delete two words, you can do d2w. Or to delete 10 characters to the left, you can do d10h, or to delete next two lines, do d2j. For something more complicated like 'delete up to middle of next line', I usually just do v to go into selection mode, select what I need with hjkl, and hit d to delete it. If you do block selection mode Ctrl+v you can select a block that needs deletion and hit d. Hope that helps.
What do you mean "the middle"?
You delete with d{motion}, and that includes things like:
d5w - delete the next 5 words
d/test - delete up to the word test
see
:help d
:help motion
and the motion.txt linked in the help (also online http://vimdoc.sourceforge.net/htmldoc/motion.html )
I used to have vi macros to delete between marks. e.g. use ma to make mark a, and then put your cursor where you want it and:
mb'a"ad'b will make mark b and delete from mark a to b into buffer 'a'
mb'a"ay'b will copy (not delete)
"ap will get the text back.
(from memory this is whole line based, not "position on a line")
Related
I am trying to delete a range of lines into a register a. Is this the easiest way to achieve this?
:5,10d a
The definition of "easiest" depends on what do you have, and what do you want to do
if you have a start line number and end number, e.g.
:2349,5344d a
is the easiest way.
You don't have to consider the questions like
"where is my cursor?"
"how many lines would be removed?"
...
If you are about to remove a small amount of lines, particularly they are on same screen. (You could use relative-linenumber.) for example: "a5dd but you have to move your cursor to the first line you want to delete. And this could be done by option 1 too: 5:d a<CR> (vim will automatically translate it into .,.+5d a<CR>)
If you just know the 1st line of deletion, and find the last line you want to delete by reading your text, (of course, small amount of lines) you could press V, and press j by reading, when it reaches the deletion ending border, press "ad
If the "range" in your question is the "range" concept in vim, The first option would be better. since it could be 234,540, it could be 1;/foo, /foo/,/bar/... :h range see detail
so back to the first sentence in my answer, There is no absolutely easiest way. It all depends on what do you have, and what do you want to do.
The other way to achieve this would be to highlight the range of lines in visual line mode. (Shift-V)
Then type "ad while in visual line mode. This will put the deleted lines into the a register.
" followed by a register puts the next delete, yank or put into that register.
Below is the documentation for " (quote)
*quote*
"{a-zA-Z0-9.%#:-"} Use register {a-zA-Z0-9.%#:-"} for next delete, yank
or put (use uppercase character to append with
delete and yank) ({.%#:} only work with put).
Another example of deleting multiple lines and putting it in a register. To delete 6 lines and put them in a register you can got to the line and type "a6dd. This puts the 6 deleted lines into register a.
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)
Vim noob here. I am trying to select multiple lines of code to copy and paste in other areas. Is there a way to do this without using the mouse?
A few other ways that don't use visual mode at all:
using marks
leave a mark somewhere with ma
move somewhere else
yank from here to there with y'a
using search motions
localize some unique token at the end of the part you want to yank
yank from here to there with y/foo<cr> (forward search) or y?bar<cr> (backward search)
using text-objects
determine what text-object would map to what you want to yank:
inner/outer word, iw/aw
inner/outer pair, i'"([{</a'"([{<
inner/outer html tag, it/at
sentence, s
paragraph, p
"block", ]
…
yank that text-object with, say, yip
using other motions
yank to end of function: y]}
yank to end of file: yG
all of the above solutions with visual mode
V'ay
V/foo<cr>y
V?bar<cr>y
Vipy, etc.
V]}y
VGy
:h motion.txt will hopefully blow your mind, like it did to mine.
You can place your cursor in the first line you want to copy and then type nyy where n is the number of lines you want to copy. For example, type 2yy to copy the two lines under the cursor.
Then, you can paste them using p.
You can also select multiple lines by placing your cursor somewhere and keeping Shift pressed. Move your cursor to the end of the desired selection and stop pressing Shift. Then copy using just y (and not yy) and paste with p.
Yep, in normal mode type V[direction] and you will highlight multiple lines. If you don't want whole lines, use v instead of V. To copy it, hit y and move to the area which you want to paste in and hit p. To delete it, instead of y use x.
Alternatively, you can simply use [number of lines]yy to yank some number of lines or [number of lines]dd to cut some number of lines. In this case pasting is the same.
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.
I have been using vim for quite some time and am aware that selecting blocks of text in visual mode is as simple as SHIFT+V and moving the arrow key up or down line-by-line until I reach the end of the block of text that I want selected.
My question is - is there a faster way in visual mode to select a block of text for example by SHIFT+V followed by specifying the line number in which I want the selection to stop? (via :35 for example, where 35 is the line number I want to select up to - this obviously does not work so my question is to find how if something similar to this can be done...)
In addition to what others have said, you can also expand your selection using pattern searches.
For example, v/foo will select from your current position to the next instance of "foo." If you actually wanted to expand to the next instance of "foo," on line 35, for example, just press n to expand selection to the next instance, and so on.
update
I don't often do it, but I know that some people use marks extensively to make visual selections. For example, if I'm on line 5 and I want to select to line 35, I might press ma to place mark a on line 5, then :35 to move to line 35. Shift + v to enter linewise visual mode, and finally `a to select back to mark a.
G Goto line [count], default last line, on the first
non-blank character linewise. If 'startofline' not
set, keep the same column.
G is a one of jump-motions.
V35G achieves what you want
Vim is a language. To really understand Vim, you have to know the language. Many commands are verbs, and vim also has objects and prepositions.
V100G
V100gg
This means "select the current line up to and including line 100."
Text objects are where a lot of the power is at. They introduce more objects with prepositions.
Vap
This means "select around the current paragraph", that is select the current paragraph and the blank line following it.
V2ap
This means "select around the current paragraph and the next paragraph."
}V-2ap
This means "go to the end of the current paragraph and then visually select it and the preceding paragraph."
Understanding Vim as a language will help you to get the best mileage out of it.
After you have selecting down, then you can combine with other commands:
Vapd
With the above command, you can select around a paragraph and delete it. Change the d to a y to copy or to a c to change or to a p to paste over.
Once you get the hang of how all these commands work together, then you will eventually not need to visually select anything. Instead of visually selecting and then deleting a paragraph, you can just delete the paragraph with the dap command.
v35G will select everything from the cursor up to line 35.
v puts you in select mode, 35 specifies the line number that you want to G go to.
You could also use v} which will select everything up to the beginning of the next paragraph.
For selecting number of lines:
shift+v 9j - select 10 lines
simple just press Shift v line number gg
example: your current line to line 41
Just press Shift v 41 gg
Shift+V n j or Shift+V n k
This selects the current line and the next/previous n lines. I find it very useful.
You can press vi} to select the block surrounded with {} brackets where your cursor is currently located.
It doesn't really matter where you are inside that block (just make sure you are in the outermost one). Also you can change { to anything that has a pair like ) or ].
v%
will select the whole block.
Play with also:
v}, vp, vs, etc.
See help:
:help text-objects
which lists the different ways to select letters, words, sentences, paragraphs, blocks, and so on.
v 35 j
text added for 30 character minimum
Text objects: http://vim.wikia.com/wiki/Creating_new_text_objects
http://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects
You can always just use antecedent numbers to repeat actions:
In visual mode, type 35↓ and the cursor will move down 35 times, selecting the next 35 lines
In normal mode:
delete 35 lines 35dd
paste 35 times 35p
undo 35 changes 35u
etc.
} means move cursor to next paragraph. so, use v} to select entire paragraph.
It could come in handy to know:
In order to select the same ammount of lines for example use 1v
You should have done some modification to be able to use 1v, blockwise or linewise.
Today I saw this amazing tip from here:
:5mark < | 10mark > | normal gvV
:5mark < | 10mark > | normal gv
You can also reset the visual block boundaries doing so:
m< .......... sets the visual mode start point
m> .......... sets the visual mode end point
I use this with fold in indent mode :
v open Visual mode anywhere on the block
zaza toogle it twice
For selecting all in visual:
Type Esc to be sure yor are in normal mode
:0
type ENTER to go to the beginning of file
vG
Presss V to select the current line and enter the line number on keyboard and the press G.