12

According to http://vimdoc.sourceforge.net/htmldoc/usr_41.html#function-list vim script has functions setline() and append() to modify the current buffer but how do i delete a line from within a script? With setline(1, "") the line is only emptied but I want to get rid of it.

3 Answers 3

12

You use the ex command :d. :5d deletes line 5.

3
  • 3
    I'm still a bit puzzled why there is no API function for doing something so important but apart from that 'exe ":1," . lastline . "d"' does everything I want (actually I wanted to delete a whole range of lines).
    – ahe
    Mar 28, 2010 at 22:14
  • 2
    Because there's no need for a function to do that. You have a command that can do it. Vimscript is a base of all the ex commands normally available plus additional functions to provide functionality that isn't available (or easily available) through the ex commands.
    – jamessan
    Mar 29, 2010 at 11:56
  • 4
    It would be nice to be able to delete a line without affecting the undo list, though.
    – Max Cantor
    Sep 10, 2012 at 20:33
9

One addition: don’t do execute line 'delete', do execute line 'delete _': deleting to black hole register has a minimal number of side-effects. Without _ it will delete to default register, numbered register 1 and will move all other numbered registers (except 0 which is for yanks): 1->2, 2->3, …, 9->void.

1
@ahe
1delete
%delete
1,$delete

no need for execute here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.