Tuesday, August 4, 2015

Vim: How do you delete all text above a certain line

Asnwer 1:
 
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.

Answer 2:

kdgg
delete all lines above the current one.
And use 5kdgg to delete all lines 5 lines above the current one 

Answer 3:
: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.)


delete to beginning or end of file

To delete all the lines from the beginning of the file to your current cursor position in vim, use this command:
:1,.d
That command can be read as “From line 1 to the current position, delete”, or if you prefer, “Delete from line 1 to the current line position.”
Similarly, to delete everything from the current line to the end of the file, use this vim delete command:
:.,$d
Again, that can be read as “From the current position to the end of file, delete.”

No comments:

Post a Comment