How to map PageDown and PageUp Keys to function normally

5,955

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 ...

  1. 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.

  2. 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).

  3. Ensure that TERM is actually set to what it should be (echo $TERM).

  4. Ensure the terminfo entry for that TERM values defines the keys you want (see above).

  5. Ensure the PgUp key sends what it should. In Vim enter insert mode and press Ctrl+V then PgUp

Share:
5,955

Related videos on Youtube

mtk
Author by

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, 2022

Comments

  • mtk
    mtk over 1 year

    When I open a document using vi or vim, 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 in insert 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
      user5249203 over 11 years
      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?
    • Sridhar Sarnobat
      Sridhar Sarnobat about 10 years
      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
    Sridhar Sarnobat about 10 years
    Thank 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
    Mokubai about 3 years
    How 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
    Martin Kealey almost 3 years
    @SridharSarnobat Modifying your .vimrc is not ideal, for two reasons. Firstly, it will only fix vim, not other programs (such as less). Secondly, it may stop things from working properly if you later connect from a different type of terminal. Instead, you should make sure that TERM=mintty is in the environment sent by Mintty before your shell starts, and that your terminfo library has a definition for mintty. If your terminfo library does not have such a definition, reconfigure Mintty so that it sends a different (but broadly compatible) terminal type, such as xterm.