I have recently found out that Ctrl+C can be used instead of Esc in certain instances. Getting out of insert mode for instance. Other things can only exclusively exited using escape.
So my question is, is it possible to completely replace the escape key with Ctrl+C? So that it works exactly the same?
- 1,245
- 1
- 13
- 27
- 299
- 3
- 7
-
nice answer here: https://stackoverflow.com/questions/5030164/whats-the-difference-between-ctrlc-and-ctrl – go2null Jan 26 '24 at 00:16
2 Answers
There are some (often subtle) differences between Esc and CTRL+C to leave Insert mode.
For instance, :help i_CTRL-C mentions some of them:
CTRL-C: Quit insert mode, go back to Normal mode. Do not check for abbreviations. Does not trigger theInsertLeaveautocommand event.
Another difference happens when you enter Insert mode with a count, in which case CTRL+C will cancel the repetition from the count. To illustrate, 3iabc<Esc> will insert abcabcabc, while 3iabc<C-c> will insert simply abc.
If you want to make CTRL+C exactly equivalent to Esc, you can use a mapping to do so:
imap <C-c> <Esc>
Another option (if you're looking for a replacement for the Esc key, either because your keyboard doesn't have one or because you find its location inconvenient) is to use CTRL+[, which is exactly equivalent to Esc, it produces the exact same keystroke so both key combinations are effectively indistinguishable to Vim.
- 28,785
- 3
- 26
- 71
-
Yeah problem is, my keyboard layout makes control-] unreachable. Is there really no way to just remap it to c-c – user2741831 Jun 06 '20 at 18:19
-
1@user2741831 Yes there is, see the answer,
imap <C-c> <Esc>might be exactly what you want to make<C-c>behave exactly like<Esc>. You might want to also map that in other modes (Visual, Select, etc.) Perhaps in all modes:map <C-c> <Esc>andmap! <C-c> <Esc>will do that. – filbranden Jun 06 '20 at 18:21 -
2
-
@D.BenKnoble I thought this was one case where using a recursive map was appropriate, since it's likely you want to replace the key in all contexts and that potentially includes other mappings. Does that make sense? – filbranden Jun 07 '20 at 13:35
-
3@filbranden kind of? The mapping being recursive only means remapping escape also affects ctrl-c.. – D. Ben Knoble Jun 07 '20 at 13:53
-
Interestingly,
C-[also works in gVim, where, technically, it should be distinguishable fromEsc(via GDK events). Moreover, even QtCreator's FakeVim implements this shortcut. – Ruslan Jun 07 '20 at 20:14 -
@Ruslan Yeah, some of Vim's limitations are somewhat artificial, they're baked in due to the terminal origin of vi/vim and I guess for simplicity and consistency between different target platforms... – filbranden Jun 07 '20 at 23:05
-
1@filbranden Note that it's quite easy in all OSes to swap CapsLock and Escape. It's a solution adopted by a lot of vimmers and makes life much easier. – cassepipe Jan 12 '24 at 13:58
I do not think it is good idea to use CTRL-C instead of ESC since it does not do some useful things like following (from :h i_CTRL-C) and can break some plugins.
*i_CTRL-C*
CTRL-C Quit insert mode, go back to Normal mode. Do not check for
abbreviations. Does not trigger the |InsertLeave| autocommand
event.
But if really want it you can remap:
inoremap <ESC> <C-C>
Also you maybe want to know that <C-[> is equal to <ESC>. And if you worry about speed of typing think about this mapping:
inoremap jj <Esc>
- 416
- 2
- 5
-
1Note that your first
inoremapis backwards, you're mapping Esc to Ctrl-C and not Ctrl-C to Esc. With the second mapping, you might want to explain what that does and why you'd use that, it might not be obvious to a beginner... – filbranden Jun 07 '20 at 13:33