Henry
Henry

Reputation: 6620

Delete text *after* cursor in vim (leaving what is under the cursor)

Let's say I've got this text on a line:

myNewFunction(argument); // some junk I don't need
                       ^
                   my cursor

What is the fastest way to delete everything after the semicolon (which I'm on). So for example, D will not work, because it deletes the semicolon too. I know I can do lD (move right one, then delete)

Is there a general way to delete after cursor though? Ideally I could even do something like Da3 (delete everything three characters after the cursor position)

EDIT: This often happens after I'm finished typing a semicolon and then I press Esc - now my cursor is on the semicolon.

Upvotes: 2

Views: 2138

Answers (3)

Amadan
Amadan

Reputation: 198314

lol.... Okay, I'll type it up.

So - If you're in insert mode, <C-O> switches it off for one command only, without moving the cursor. So if you do <C-O>D, you will delete the rest of the line and get dropped back into insert mode.

If you are in normal mode, lD should be fast enough.

Upvotes: 3

Birei
Birei

Reputation: 36252

Try to create a function and use it with a normal mapping.

This gets character under current cursor position and instead of D use C that sets insert mode and lets us to put the character saves previosly:

function! DeleteUntilEOL()
    let c = getline('.')[col('.')-1]
    execute 'normal C' . c 
endfunction

Now create the mapping:

:nnoremap <leader>D :call DeleteUntilEOL()<CR>

Upvotes: 0

rbernabe
rbernabe

Reputation: 1072

I think it's not posible without mappings or repeating a substitution.

If you don't have problem 'repeating' a substitution, you can do a first substitution:

:s/;\ze.*$//

And then, repeat it using & is one key stroke, but you nead to make the substitution and then, repeat it. Just to be clear, pressing & repeats the last substitution. It's like the . command but only for substitutions

Upvotes: 0

Related Questions