In command mode there is no delete from cursor to the end of line (like CTRL-k in bash). So to delete a long line of trailing text, I press and hold delete, which works fine until it runs out of characters to the right of cursor, and which points it starts steam rolling over the part of the command I wish to keep (i.e. starts backspacing).

Is there anyway to turn off this unusual behaviour, as confirmed by the help:

<Del>       Delete the character under the cursor (at end of line:
            character before the cursor).

PS I know about CTRL-F to see the cmdline, but sometimes I wish to quickly change the end of an existing command.

  • Have you tried <C-u>? I think it works IIRC (edit: misread. You need to delete the other way. Hm.) – D. Ben Knoble Mar 27 '20 at 16:46

To change the behavior of <Del> so that it only deletes the character under the cursor and doesn't behave like backspace when at the end of the line, you can use the following mapping:

cnoremap <expr> <Del> getcmdpos() <= strlen(getcmdline()) ? "\<Del>" : ""

See @D.BenKnoble's answer for a solution to deleting until the end of the line (similar to what <C-K> does in bash and other readline-based programs.)


You can use the following function:

function KillEnd()
  let l:cmd = getcmdline()
  let l:pos = getcmdpos()
  let l:newcmd = strpart(l:cmd, 0, l:pos - 1)
  return l:newcmd

To use it on the command line, you need to do (I choose <C-x> arbitrarily since it wasn't used for anything else, and reminds me of X in normal mode):

cnoremap <C-x> <C-\>eKillEnd()<CR>

See :help c_CTRL-\_e for details on the mapping; the <C-\>e sequence uses an expression to replace the entire command line.

The function returns a substring of the command line based on the cursor position.

If you wanted a one-liner:

cnoremap <C-x> <C-\>estrpart(getcmdline(), 0, getcmdpos() - 1)<CR>
  • 1
    PS I didn't actually answer the question, I just addressed the problem of not having a kill-to-end. I don't recommend <C-k> for the mapping, since that's for digraphs. – D. Ben Knoble Mar 27 '20 at 17:02
  • 1
    Great answer, I wish I could of marked yours as the correct answer too. I generally dont need to type in diagraphs into the command line, so I am happy to change it to be more bash like. – run_the_race Mar 30 '20 at 11:37

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.