What is the difference between <C-C> and <C-[> in vim? - vim

One of the best tips for using vim that I have learned so far has been that one can press Ctrl+C or Ctrl+[ instead of the Esc key. However I use a dvorak keyboard so Ctrl+[ is a little out of reach for me as well so I mostly use Ctrl+C. Now I've read somewhere that these two key combinations don't actually have exactly the same behaviour and that it is better to use Ctrl+[. I haven't come across any problems so far though so I'd like to know what exactly is the difference between the two?

According to Vim's documentation, Ctrl+C does not check for abbreviations and does not trigger the InsertLeave autocommand event while Ctrl+[ does.
One option is to use the following to remap Ctrl+C
inoremap <C-c> <Esc><Esc>

Extremely late answer, but I just had the same question and found one practical example which helps explain the difference, so why not.
If you select a visual block and then change it with c or append something to the end of it with A, if you then exit with <Esc>, the same change will happen on all the lines of the visual block (which is really useful! See :help v_b_A); if you exit with <C-c>, this doesn't happen, only one line gets the change. There are probably other similar things I didn't realize I was missing with <C-c>...

As it turns out, <C-[> is exactly identical to Esc, they are the same character. So no need to wonder about any difference there. :)

Related

Is it possible to easily insert a single word in vim

I often want to insert a single word with vim. Is there a way to insert as many Chars as you want until you type a <Space> which then acts as an <esc>?
I know that a normal i with an <esc> doesn't take too long, but I would like to have such a command and the <esc> is quiet hard to reach.
You should consider the following
remap caps lock to esc or;
remap caps lock to control, and then use
the alias for esc which is control-[.
Otherwise, though this isn't quite what you're looking for, see this post for a possible approach, but also read Ingo Karkat's answer there for why it may not be such a great idea to make such mappings.
Please mention the most underrated <C-c>

Effective way to put ; at the end of line

Sorry for a noob question, but i find it struggling to just put a ";" at the end of line after writing a function. For example, I am coding in C and many time i need to write things like:
f(a);
what i usually type is (from normal mode, using bracket autopair-like feature):
if(a<ESC><SHIFT-a>;
and it need changing mode twice! Comparing to normal editor (sublime):
f(a<right>;
does anyone have more efficient way do do those typing? thanks for any help.
I think you have some "auto-close" plugin installed.
I have that kind of plugin too, and I don't press arrow keys either, since I don't have them on my keyboard. I have this:
" moving cursor out of (right of ) autoClosed brackets
inoremap <c-l> <esc>%%a
So with your example: it would be (assume already in INSERT mode)
f(a<ctrl-l>;
Thus, your fingers never leave the home row.
If you're a vim user, you can hit Shift-a.
Shift-a takes you from normal mode to insert mode, and starts your cursor at the end of the line.
(If you want to be an efficient vim user, you should remap esc to something like caps-lock.)
Comparing to normal editor (sublime):
f(a<right>;
Well… that's exactly how you would do it in Vim if you use Delimitmate or some other "autoclosing" plugin. Why do you insist on making things more complicated than they are?

repurpose default key in vim

I heard that there is no internal function to call when typing a key combo in vim, unlike emacs. So I want to repurpose Ctrl-A to ^ (like, nnoremap < C-A> ^); however, the original function of Ctrl-A is also attractive, so I want to map it to some other keys, say Ctrl-S. How can I do this the vim way?
This question might have been asked but I searched around and did not find anything close. Thanks in advance.
PS: the stupid vim-emacs war is spamming everywhere and nowhere can i find a real solution that makes a desired feature to work in another environment.
The following should do:
nnoremap <C-A> ^
nnoremap <C-A>A <C-A>
This works because the noremap tells vim to do not apply any other mapping, thus the rhs of the second mapping above is the default <Ctrl-A>.
You will notice that the first mapping will take a delay to execute, because vim waits a while to allow you to type the second mapping. Vim-FAQ 20.16 may help if this bothers you.
But I'd advice against changing the default mapping because, as explained here, vim has many default mappings that may seems silly when you start using it. When you understand it better you will have a hard time finding another key to map it or to get used to the old/default mappings.

Please explain this Vim mapping alias(?)

I've found a feature, quite a while ago actually but only now am I interested in knowing what it does. It's the following:
nm <space> [space]
nn [space] <NOP>
nm [space]F :VimFiler<CR>
What exactly is this doing? What are the advantages of using this instead of just <space>? Except that it seems to work as an alias, so you can change what [space] means and that changes all the keybinds that are used by this.
How does this work? Official documentation or something that explains it in detail how it works would be more than appreciated. :)
There isn't anything special about [space]. The first mapping makes it so that pressing Space is the same as having typed those characters. The second makes it so that typing [space] on its own doesn't do anything (<NOP> is special). The last makes it so that typing [space]F does that command. You can see that this is true by actually pressing the keys [space]F and seeing that it does it too. It seems kind of like a way to emulate the <Leader> and <LocalLeader> functionality, but IMO a rather poor way of doing it.

Making inserting a single character in Vim an atomic operation

I've long used this very useful shortcut in vim:
nmap <space> i <esc>r
this means that if I press spacef, for example, it will insert a single character f at the given position.
unfortunately, however, this is not atomic, ie, if I press spacef and then navigate somewhere else, then press ., I get the equivalent of rf, not spacef.
all this makes sense, but here's the question: is there a way of making this atomic, so that . will repeat the 'insert character' operation, and so that undo etc all treat it as one operation, too?
Awesome! Michael's answer pointed me to the plugin I needed to finish my plugin, which can now do what you want - I had been trying to figure out how to do this for ages!
1) Install Tim Pope's plugin
2) Install my plugin
3) Add a mapping to your .vimrc:
nnoremap <space> :<C-U>call InsertChar#insert(v:count1)<CR>
Does this work for you?
noremap <silent> <space> :exe "normal i".nr2char(getchar())<CR>
You might want to have a look at this plugin script. It may be possible to configure your map so it can be supported. Read the supporting docs
http://www.vim.org/scripts/script.php?script_id=2136
Sorry I can't provide a specific answer to your problem but I will note that I tend to use the . key when I have to reproduce quite a lot of commands (e.g. I want to insert f 5 or more times).
If that is the case here, I don't think the saving of using your macro is worth it. You save one keystroke by using your macro rather than ifesc and that operation is atomic so you could then . to your heart's content.
I would just use the non-macro version if I know I want to repeat it a lot.
P.S. You know I'm starting to like the <kbd> tag quite a bit :-)

Resources