Try using Ctrl-[
instead of Esc
.
What is the result of:
python -c "print ord(raw_input('char '))"
when you press Esc
and Enter
? It should be "27". What is the result of pressing Ctrl-V
then Esc
?
Have you checked all the settings in PuTTY to see if they're reasonable?
Are you using Bash on the remote system? Look at the output of
bind -p | grep -i '\\e' | less
and see if you see anything unusual. Do you have a file called ~/.inputrc
? Look at its contents to see if anything is unusual.
Look at your ~/.vimrc
and see if everything's OK, too.
Update:
I am using Putty to connect to this machine
Then I guessed correctly and my answer below probably applies.
Original answer:
There's not enough information in your question to be certain of your problem, whilst we are waiting let me provide the audience with a little story:
The Terminal Emulator and the Editor
If you access a Linux computer using an xterm emulator such as Putty, your PgUp key sends four characters Escape
[
5
~
to the Linux system.
The Linux system processes this and then passes any unactioned keystrokes to the application.
The application, vim, uses a system library (I believe) to translate character sequences back into "keys" based on the value of the environment variable TERM and the corresponding entry in terminfo
If terminfo doesn't define PgUp (or any other key) as Escape
[
5
~
, Vim will (probably) process the four characters individually.
"Many cursor key codes start with an Esc. Vim must find out if this is a
single hit of the Esc key or the start of a cursor key sequence. It waits
for a next character to arrive. If it does not arrive within one second a
single Esc is assumed." So vim waits a bit to see if Esc is you wanting to exit insert mode.
[
and 5
are valid Vim commands. But maybe Vim sees the Esc as signalling the start of an escape sequence it doesn't recognize - it has to guess where that sequence ends and restart interpreting characters as commands or data from that point.
~
is a vim command to toggle case of character under cursor.
A Happy Ending
Set TERM correctly (to match Putty/emulator settings) and ensure terminfo has a definition of PgUp etc. In Unix-land, keyboards existed before the IBM PC was born, some of them had a Previous Page key so this key capability is named kpp
.
$ infocmp xterm | grep kpp
knp=\E[6~, kpp=\E[5~, kri=\E[1;2A, mc0=\E[i, mc4=\E[4i,
Postscript
how to map these keys
Putty doesn't have an arbitrary key-mapping capability so far as I know (I think it was in the wish list) So ...
Configure Putty to emulate a well-known and well-defined terminal type. Unfortunately there aren't any so something like xterm will have to do.
Ensure Putty tells the Linux box what terminal type it is emulating. Partly this is by configuring Putty's settings and partly by using a protocol that communicates TERM (e.g. Putty's ssh support) rather than one that doesn't (Putty's telnet support).
Ensure that TERM is actually set to what it should be (echo $TERM
).
Ensure the terminfo entry for that TERM values defines the keys you want (see above).
Ensure the PgUp key sends what it should. In Vim enter insert mode and press Ctrl+V then PgUp
Best Answer
Another upvote for @garyjohn's comment, here.
By following that path, you are certainly shooting yourself in the foot. I see at least two big mistakes in your approach.
As @garyjohn wrote, you are "fighting" against Vim's most crucial aspect. Modality is at the core of everything that makes it the awesome text editor it is. If you consider its proverbially steep learning curve, actively going against its design won't get you far up.
Like many others, you focus your "dumbing down Vim" effort on movements like
<up>
,<down>
and friends. Vim has much better ways to move around in and between buffers:wWeEbB/?tTfF{}[]
and so on. All these motions give you super powers. All these motions depend on modality in one way or another.Rejecting these incredibly useful tools is a very bad mistake in and of itself. Remapping them would be both very tough and utterly worthless.
FYI, when I started Vim, I spent months trying to turn it into a TextMate clone. Trying to create insert mode mappings for everything was probably one the most frustrating endeavour of my whole career. I learned two things in the process, though:
my beloved TextMate was seriously underpowered compared to Vim so it made absolutely no sense to dumb down Vim, and…
modes freaking rule.
Accept Vim's philosophy instead of rejecting it. Or reject it and use Sublime Text 2 instead, for what we care.