Vim Tips Wiki
(comment from 1324)
(remove 2 finished links)
Line 18: Line 18:
 
Pressing Ctrl-[ (control plus left square bracket) is equivalent to pressing Esc.
 
Pressing Ctrl-[ (control plus left square bracket) is equivalent to pressing Esc.
   
===Related tips (probably merge to here)===
+
===Related tips (may merge somewhere)===
 
*[[VimTip377|377 Microsoft Natural Multimedia Keyboard Scancodes]]
 
*[[VimTip377|377 Microsoft Natural Multimedia Keyboard Scancodes]]
 
*[[VimTip459|459 Use Ctrl-O instead of Esc in insert mode mappings]]
 
*[[VimTip459|459 Use Ctrl-O instead of Esc in insert mode mappings]]
 
*[[VimTip770|770 Toggle normal-insert modes without Escape]]
 
*[[VimTip770|770 Toggle normal-insert modes without Escape]]
*[[VimTip775|775 Remapping Caps to Shift]]
 
 
*[[VimTip1183|1183 Insert-mode only Caps Lock]]
 
*[[VimTip1183|1183 Insert-mode only Caps Lock]]
  +
*[[VimTip1275|1275 Toggle between insert and normal mode with Esc]]
 
   
 
==Comments==
 
==Comments==

Revision as of 10:49, 28 May 2009

Tip 285 Printable Monobook Previous Next

created 2002 · complexity basic · author David A. Rogers · version 6.0


This tip discusses how to exit from insert mode without needing to press Esc. Many touch typists appreciate the fact that they can leave their hands on the keyboard home row while using Vim, so there are several ideas for avoiding the Esc key (pressing Esc generally requires stretching to the top of the keyboard).

No utility programs or operating system tweaks are required for this tip. For other suggestions involving mapping keys with the operating system or utilities, see Map caps lock to escape in XWindows (for Unix-based systems), or Map caps lock to escape in Windows (for Windows).

Avoiding the Esc key

Pressing Ctrl-[ (control plus left square bracket) is equivalent to pressing Esc.

Related tips (may merge somewhere)


Comments

Simple map to use backtick as Esc, when in insert mode:

:imap ` <Esc>

I've always had situations where I had to "make a quick insert" - go into insert mode, insert a quick word, then come out into normal mode, and navigate elsewhere. To make a long story short, this is the solution that I came up with -

:map! <S-space> <Esc>

or even

:map! <S-space><S-space> <Esc>

if you find the first mapping getting in the way of your typing

With this quick combo ("Shift", + <Space>), one can easily (and might I add, intuitively) go into and come out of insert mode. I guess I always thought the <Space> would be a good way to do this sort of thing, since it is after all, so intuitive in the typing process. So why not make it such that it can "escape" you out into normal mode as well? Just type 'i', to go into insert mode, type in your stuff, and once you're done, hit Shift-space!

(note: I havent automated 'i' into the whole process yet, but does anybody think that it would be possible? I would certainly like to hear about it if you do work out something)


I have a better tip. You can use some useful 'imap' keymaps for inserting mode. For example:

<C-j> <Esc>lji
<C-k> <Esc>lki
<C-u> <Esc>i
<C-l> <Esc> l

<C-h> is crash with scancode of Insert key. so I use <C-u> keymap


Since on the french canadian keyboard the key combination Ctrl-[ is performed with Ctrl+AltCar+"the cedilla key" it's not very convenient.

But the tip of using CTRL-C key combination work well on any keyboard mapping. It doesn't required you to modify any keymap, so it will work on any vanilla installation. From my point of view (switching from PC with English_US and Frech_CA keyboard layout) using CTRL-C instead of the hard to reach ESC key is the real TIP here.


I have tried mapping "qq" and "jf" to <Esc> but I'm going to try to shift-space.

Incidentally, I mapped the space bar to i. So I will press <Space> to get into insert mode and <S-space> to get out.


:map <S-space> i
:imap <S-space> <Esc>

From command mode <S-space> takes you to insert mode, from insert mode <S-space> takes you to command mode.


You can also remap the Caps Lock key to Control in Win9x using Microsoft's Windows 95 Kernel Toys (find them at http://www.microsoft.com/windows95/downloads/contents/WUToys/W95KernelToy/Default.asp); one of them ("keyboard remap") will let you do this. Despite their saying they're Win95 only, I've used this utility under Win98 and Win98SE without problems.


> I cannot get 'map! <S-space> <Esc>' to work for me.

Since you are in INSERT mode when you want to press escape, it should be:

imap <S-space> <Esc>

On Mac OS X:

I, too, am having problems getting the imap <S-space> <Esc> scheme to work. I've noticed some really funky stuff. First, it doesn't work at all using App Terminal version of Vim, nor does it work using the GUI version for Jaguar either. Where the story gets strange is that it DOES work using the X-Windows version of gvim (via Apple X11) but not the xterm version.

To sum up, it ONLY works on gvim via Apple's X11.

Hopefully someone can help us figure out what's going on here.


In Mac OS X, mapping <S-space> does not work, but using the following works and is better (<D-space> means the "apple" key and the space key):

imap <D-space> <Esc>

Okay a solution. Get the shareware utility "Keyboard Maestro". It allows you to remap keystrokes for all sorts of tasks. You can add a general remap for the Esc key to what ever you like, S-Space, or Ctrl-space, etc. Not exactly elegant via Vim, but it works.


Try:

map! <S-Enter> <Esc>

instead of <S-Space>

works a lot better and won't get pressed accidentally.


If you're having trouble getting <C-space> to map in Vim, it may be that <C-space> is being mapped to another keystoke. My keyboard was using <Nul> for <C-space>. So, you could put in your vimrc:

" Map <C-space> to <Esc>. CTRL-@ will also be mapped to <Esc>
map <Nul> <Esc>

To get the <> value for a particular keystroke, try pressing CTRL-K before the keystroke, while in insert mode.


imap <Nul> <C-p>

This is really works. But what is <Nul>? Is it keyboard or X11 defines it?


For Win2k and WinXP users, KeyTweak is probably your best bet, since it automates the registry access. I just downloaded it to swap my escape and capslock keys and it works great.

http://webpages.charter.net/krumsick/


And for those of us who prefer the console:

Alt-[key] is usually actually sent as <Esc>[key], and generally if you're coming out of insert mode it's because you want to execute a normal mode command, so:

Instead of <Esc>: to start an Ex command from insert mode, just press Alt-:. Sim. for everything else.


It's a good idea to use <ctrl-c> to get back to normal mode. these shift-space-things aren't easier to use and are user defined, so if you use another Vim installation, you have to define these two mappings first. so why not using <ctrl-c> instead? however, the only difference between <ctrl-c> and ESC is that incomplete abbreviations won't be expanded.


I use ":imap ;; <Esc>"

The ";" key is right at the fingertips, and I almost never actually need to type ";;". The <s-space> is good too but it doesn't work when I use vi in a terminal (with putty).


For Windows users:

Remap whatever you want any way you want: Use Autohotkey (www.autohotkey.com, an open source project)! You can map single keys to key combinations, you can map key combinations to single keys, you can even map double key strokes to single keys (e.g. double CAPS to ESC). But it's still much more. It's an extended, extremely powerful scripting language. There are scripts that allow the "command mode" style navigation everywhere in windows! Plus you can always write scripts yourself.


Why not use:

map! ii <Esc>

This allows the use of the same key to both enter and exit insert mode. It's also extra handy for dvorak users, since we can mash the i with the left index finger. And outside of roman numerals, how often does anyone need to type two consecutive i's?


I use AutoHotkey. The following script will map CapsLock to Ctrl in PuTTY. Every other app will use CapsLock like normal.

classname = ""
keystate = ""

*CapsLock::
 WinGetClass, classname, A
 if (classname = "PuTTY")
   send,{Ctrl down}
 else
   GetKeyState, keystate, CapsLock, T
 if (keystate = "D")
   SetCapsLockState, Off
 else
   SetCapsLockState, On
 return

*CapsLock up::
 WinGetClass, classname, A
 if (classname = "PuTTY")
   send,{Ctrl up}
 return

I prefer this over losing the CapsLock key alltogether. Also this method is easily adaptable and I was already using AutoHotkey anyway.


If you have :set insertmode, then CTRL-L moves you from INSERT mode to NORMAL mode


Mapping ii to <Esc> is cool!

I have quite long fingers, therefore the upper keys (like numbers, even function keys) are somewhat easier to reach as the lower keys (like ctrl). So mapping <F5> to <Esc> is very helpful to me (maybe <F9> too, for the right hand).

in .vimrc:

imap <F5> <Esc>

I am forced to use Windows at work, I tried www.autohotkey.com (the open source project for remapping keys) and it rocks!

To make Capslock work just like Escape, just add this to their default script:

Capslock::Esc

As for the shift+space problem, PuTTY seems to refuse to send the shift key along with space (or with enter, for that matter). I don't know why, but I'm hoping that "imap ii <Esc>" will suffice.

From tip 314 (now removed)

Here is a small snippet from my mappings file, ready to speed things up. The key overloading might be a somewhat confusing at first.

imap <S-Space> <Esc>l
imap <C-CR> <Esc>o
imap <S-CR> <Esc>O

nmap <S-Space> i
nmap <Space><Space> i
nnoremap <CR> o
nmap <S-CR> O

From tip 783 (now removed)

I found myself spending way too much time in insert mode, because it takes so much effort to hit <Esc>, or alternatively <C-[>. To train myself to use insert mode only for what it's meant for - namely inserting text - I wanted to make it as easy as possible for myself to get out of insert mode.

My solution: map <CR> to <Esc>! Pressing the return key at the end of an insert operation feels very natural, and it is one of the more accessible keys on the keyboard. For inserting multiline text, you can use Shift-Enter, which doesn't take that much getting used to.

If you want to give it a try, put the following in your vimrc:

inoremap <CR> <Esc>

It might be better to map Shift-Enter to Escape. Change the map to:

inoremap <S-CR> <Esc>

This works in the GUI-versions on Linux, Mac OS X and Windows, but not in terminal versions.

From tip 1117 (now removed)

People get tired of hitting Escape all the time to get into normal mode. Others have given advice of using Ctrl-[ or mapping CapsLock to Escape.

I now suggest using Tab to switch to normal mode.

Tab is far faster than Escape or Ctrl-[, and there is no burden of configuring every machine you use to map CapsLock to Escape.

Now that you've read my advice and decided to start using Tab for Escape, the thought arises, "how will I insert tabs?".

You can always shift lines using << and >> when in normal mode. When in insert mode, you can use Ctrl-T and Ctrl-D to do what >> and << do, respectively. I personally like to map Alt-i to insert a Tab, because it's the fastest key combination.

Enough talk; here's the setup for your vimrc:

inoremap <m-i> <Tab>
nnoremap <Tab> <Esc>
vnoremap <Tab> <Esc>gV
onoremap <Tab> <Esc>
inoremap <Tab> <Esc>`^

The gV makes sure to deselect the selection; and the `^ puts the cursor back to where it was last in insert mode (so there is no going back a char when escaping to normal mode).


Vim, by default, uses ctrl-i to insert a tab character. If you use the above hint, there's no reason to introduce a new mapping with alt-i, just restore the default mapping by putting it after all the tab remappings:

nnoremap <Tab> <Esc>
vnoremap <Tab> <Esc>gV
onoremap <Tab> <Esc>
inoremap <Tab> <Esc>`^
inoremap <C-I> <Tab>
I don't think the above works (at least, not on all systems).
Vim uses the keys as sent by the operating system, and it's the OS which sends ctrl-i when the Tab key is pressed (given the way Vim reads keys).
At any rate, on two systems I've tried, the inoremap for <Tab> works well, but if I then do the second inoremap for <C-I> I find that the first inoremap no longer works (because tab and c-i are equivalent to Vim).

The best way to leave insert mode is:

imap jj <Esc>

Comments from tip 1324 (now removed)

You need a US keyboard to find Ctrl-[ convenient. I need to press <AltrGr-8> for '['.


Use alt-space as escape -- works even on a (Linux) console:

inoremap <M-Space> <Esc>
or
inoremap ^[^V_ ^[
(_ is space)