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
Are you using vim on Windows or on Linux (or something else), are you using a terminal emulator (such as Putty)? X11 or character mode? – RedGrittyBrick – 2012-09-27T10:25:11.433
I have this problem with Cygwin + mintty + vim. I don't fully understand the solution but the post below will hopefully steer me in the right direction. – Sridhar Sarnobat – 2014-02-20T00:33:15.440