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

1
  • FYI, "command mode" is synonymous with "normal mode". What you are talking about is "command-line mode". See :help vim-modes. – romainl Dec 1 '14 at 9:34
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.

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.

1
  • 3
    And use q: to start the command-line window directly. – romainl Dec 1 '14 at 7:34

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.