Home/End Keys in the less Command

When using the man command or when you want to divide a long document into pages, less is a command that is often used together with the more command.

You can use the Home key and the End key to move to the very beginning and the very end of the document, respectively. However, these keys may not function as expected depending on the terminal environment.

You can solve this issue by choosing one of the following:

  • Make sure that the Home/End key values ​​are in rxvt mode. (Xshell 7 build 0167 or later)
    By default, the Home/End keys generate values ​​^[[1~ and ^[[4~ in the terminal. The less command, however, does not recognize these key values ​​as Home/End keys. After upgrading to Xshell 7 build 0167 or later, set the Home/End key values ​​to be sent as rxvt type key values. This can be configured in the session’s properties:

    image-20240729-164202.png


    The Home/End key values ​​in the rxvt format are ^[[H and ^[[F, respectively. Note that these key values ​​may not be recognized as Home/End keys in some applications.

  • Change the TERM environment variable to linux, etc.
    The less command does not recognize Home/End keys when the TERM environment variable is xterm, xterm-256color, etc. You can allow the keys to be properly recognized by changing the TERM environment variable to a format other than xterm-*.

    Note that in this case, functions that can only be used in xterm terminals may be limited.

  • Have the TERM changed only when using the less command via aliases

    alias less='_less() { export OLDTERM=$TERM; export TERM=linux; \less $@; export TERM=$OLDTERM;}; _less'

    Note that this method will not work when using less is followed by a pipe (|).

  • Configure the less command’s settings (Recommended)

    1. Create a new command file

      $ vi ~/lesskey.txt \e[1~ goto-line \e[4~ goto-end

       

    2. Create the ~/.less file using the lesskey command

      $ lesskey -o ~/.less ~/lesskey.txt $ file ~/.less /home/user/.less: data

This setting is recommended as it only applies to less without changing the existing user experience.