How to delete until (and not including) the last char on the line

Viewed 292

I would like to delete a sequence ending with plenty parenthesis and rather than count the exact number of parenthesis to do a d[count]f), I want to delete up to the before last character in a line.

I tried dt$ but with no success.

EDIT: Eventually, can we exclude more characters ?

2 Answers

So as I see the real question is "How to delete until (and not including) the last char on the line.

And the answer is: dv$. See :h exclusive and :h o_v.

You can use marks or visual selection. For example with marks:

ma$d`a

Splitting in commands:

ma Mark the current cursor position with mark a
$ Go to the last character in the line
d`a Delete back to the mark a

If you need to preserve a few characters at the end of the line add Nh after $. E.g.:

ma$3hd`a

Visual selection:

v$3hx

Define a command with one required argument:

function! DelToEOL(n)
    execute 'normal v$' . string(a:n + 1) . 'hx'
endfunction

command -nargs=1 DelToEOL :call DelToEOL(<args>)

+ 1 is required because visual selection selects the current character which should be excluded.

Related