How do you delete all text above a certain line. For deletion below a line I use "d shift g"
dgg
will delete everything from your current line to the top of the file.
d is the deletion command, and gg is a movement command that says go to the top of the file, so when used together, it means delete from my current position to the top of the file.
Also
dG
will delete all lines at or below the current one
:1,.d deletes lines 1 to current.
:1,.-1d deletes lines 1 to above current.
(Personally I'd use dgg or kdgg like the other answers, but TMTOWTDI.)
kdgg
delete all lines above the current one.
Providing you know these vim commands:
1G -> go to first line in file
G -> go to last line in file
then, the following make more sense, are more unitary and easier to remember IMHO:
d1G -> delete starting from the line you are on, to the first line of file
dG -> delete starting from the line you are on, to the last line of file
Cheers.
d1G = delete to top including current line (vi)
:.,$-3d deletes from current line to 3 lines from end
Related
while giving the dp command in vimdiff it replaces the entire block(2 lines) with pink colour on left hand side file to right hand side file.
In that block if i want to merge only the second line. First line should not be Merged.
You can just copy the part of the code you want to copy over using:
Hit the V key and then select just the text you want to move; and then hit yy to yank it.
Press CTRL+ww to switch windows, and then move your cursor to the desired location
Finally, press p to paste it in the desired location.
Go to the one line that you need to be overwritten:
:.diffget
Or just:
:.diffg
If you want to re-calculate the "pink areas" now:
:diffu
If the line is absent, it's best to hit O to add an empty line and then do a :.diffg
Just adding it as an answer, so people can vote on it. For me it works better (when bound to a key combo) than do or dp.
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")
How to do that in vim, lets say im in line 100 and i want to delete 20 lines upward? how to do that in vim linux?
You can do it relative if you know the number of lines:
d20k
Or absolute if you know the line number:
d80G
d = delete, 20k = 20 lines up, 80G = goto line 80
Or without taking you current position into account:
:80,100d
Personally, I use visual mode a lot becuase it gives me a nice feedback:
hit V to enter visual line mode
move 20 lines up with 20k
adjust selection with k and j if necessary
press d to delete selection
In normal mode you can use d and the movenment key k to delete upwards. And then you prefix that command with the number of times you want to repeat the command like 100dk.
If you just want to delete to the beginning you can use gg along with d, gg sends the cursor to the first character in the file. So ggd will delete the first line to the line you are standing on.
You can do it upwards with the range.
:-20,.d
In this you can specify the range of line to delete it .
Will deletes 20 lines upwards to the current and this is kind of an ex-mode command.
I want to copy N lines above the selected position. yNy works for copying the below N lines.
What is the command for copy N line above the selected position?
yNk will copy the line you're on and the N preceding lines.
Or, use :<range>yank (see :he range for all possible uses of range)
:-3,-1y
this does precisely what you ask: yank only (e.g. 3) lines before the current line. You could
:-1y
:-2y
to yank just the previous (or pre-previous) line etc...
:1,-1y
to yank everything till the last line
:1,.y
for that including the current line (of course, you could do that with ygg)
IS there a way to add 4 lines before the last line of the file in vim?
I'm new to vim and recently found out that vim does multiline search which is awesome. Now only if I could find how to add 4 lines before the last line in the file I would totally save immense amount of time
G 4 O Esc
G jumps to the end of the file. Then press O to insert a line before that last line. Now hit Enter three times and you have four lines before the last line.