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

4

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

Patoshi パトシ

Posted 2014-11-30T21:07:10.663

Reputation: 1 687

FYI, "command mode" is synonymous with "normal mode". What you are talking about is "command-line mode". See :help vim-modes. – romainl – 2014-12-01T09:34:31.840

Answers

5

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.

Chong

Posted 2014-11-30T21:07:10.663

Reputation: 496

4

After you type : to enter on command mode, or after your insert your command, type CTRL+f. Your history of commands will appear in a new "command-line window". Once there, you could use D from the point where you want to start to delete. Then just type to execute your command.

jherran

Posted 2014-11-30T21:07:10.663

Reputation: 1 693

3And use q: to start the command-line window directly. – romainl – 2014-12-01T07:34:58.603