How to map PageDown and PageUp Keys to function normally
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
Related videos on Youtube
mtk
http://mtktechiecode.blogspot.in/p/about-me.html http://mdtareque.github.io/cv/ https://code-bite.blogspot.in/ http://web.iiit.ac.in/~tareque.mohd/
Updated on September 18, 2022Comments
-
mtk over 1 year
When I open a document using
vi
orvim
, I am unable to navigate the page using Page Up and Page Down keys. On pressing these keys, vim seems to behave in abnormal maaner, and changes the case of character beneath the cursor. It then takes few seconds to come back again ininsert
mode.Please let me know, how to map these keys, so that on pressing them I can scroll through pages smoothly just like Ctrl+ (f / b / u / d ).
Update
Machine and Vim info
$ cat /etc/*-release Red Hat Enterprise Linux Server release 5.5 (Tikanga) $ vim --version VIM - Vi IMproved 7.0 (2006 May 7, compiled Jun 12 2009 07:08:36)
Note
I am using Putty to connect to this machine.
-
user5249203 over 11 yearsAre you using vim on Windows or on Linux (or something else), are you using a terminal emulator (such as Putty)? X11 or character mode?
-
Sridhar Sarnobat about 10 yearsI 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 about 10 yearsThank you so much for the explanation. I solved this in my setup (Cygwin + Vim + Mintty) by putting
set term=mintty
in my vimrc file. -
Mokubai about 3 yearsHow does this fix the issue? Why is it a fix? How do you make it permanent? You can edit your answer to improve it.
-
Martin Kealey almost 3 years@SridharSarnobat Modifying your
.vimrc
is not ideal, for two reasons. Firstly, it will only fixvim
, not other programs (such asless
). Secondly, it may stop things from working properly if you later connect from a different type of terminal. Instead, you should make sure thatTERM=mintty
is in the environment sent by Mintty before your shell starts, and that your terminfo library has a definition formintty
. If your terminfo library does not have such a definition, reconfigure Mintty so that it sends a different (but broadly compatible) terminal type, such asxterm
.