In VIM, how do you delete to end of line while in command mode :

bashvivim

While in VIM, I have a leader shortcut for the following:

:!git commit % -m 'updated '; git push;                                                                                                                                                                   

My cursor lands on the character after updated once i execute this leader shortcut. Now the question is:

How do I delete to the end of that line using a shortcut key? In bash, it would be "CTRL-k", but that doesnt work here. I just want to delete everything after the word "updated".

enter image description here

Best Answer

There isn't a default shortcut mapping to do what you asked for. A full documentation of command mode editing can be found through :help cmdline-editing. You are welcomed to browse through it to find anything useful to you. But I don't think the function you asked for is in it.

I guess the default way of editing of command-line doesn't require such operation. Usually we remove the word before cursor by <Ctrl-W> without moving cursor around. But of course, if you have your own way of editing it, you can always bend vim's behaviour to your favour.

You can have the shortcut you want by mapping <C-K> to a function defined to suit your required behaviour. Here I write an example. You can just paste the following code to your ~/.vimrc. Re-source the file. And you can have the short-cut working in your own way.

cnoremap <C-k> <C-\>estrpart(getcmdline(),0,getcmdpos()-1)<CR>

If you are interested, this invokes an default command mode shortcut(<C-\>e) for evaluating {expr} and replacing the whole command line with the result. strpart() is an string truncating function given by vim.

Upon your <C-k> keystroke, vim will extract the content from the command mode as a string. And put back only the content from the start of the string to one position ahead of the cursor position. Rest of the content will be truncated and thus deleted.

Related Question