Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: delete all but the current line

Tags:

vim

I know this is a bit of vim-golfing, but I am interested in other opinions / ideas. Sometimes I have to edit a file and to delete all but the current line. My go to solution involves ex mode :t0|2,$d

These are as many as 8 keystrokes! (I am aware that this command can be mapped easily to some convenient combination on the keyboard.) Other suggestions, ideas?

like image 987
user1924627 Avatar asked Mar 14 '23 00:03

user1924627


2 Answers

first of all your approach is not 8 keystrokes, if we don't count the save (ZZ), they are 9 keystrokes. Because you have to press Enter.

There is one way, with 6 keystrokes:

YggVGp

Apart from golfing, I feel my approach is more straightforward than yours. Perhaps because I like normal mode in vim. :-)

For practice, if you need do it many times, you may want to have a mapping.

like image 187
Kent Avatar answered Mar 20 '23 13:03

Kent


Another solution:

:.w!|e<CR>
like image 39
romainl Avatar answered Mar 20 '23 13:03

romainl