This is actually a function of the terminal emulator you are using (xterm, gnome-terminal, konsole, screen). An alternate screen, or altscreen, gets launched when programs such as less
or vim
are invoked. This altscreen has no history buffer and exits immediately when you quit the program, switching back to the original screen which restores the previous window content history and placement.
You can prevent less
from launch in an altscreen by passing the argument "-X".
less -X /path/to/some/file
You can also pass "-X" as an environment variable. So if you are using bash
, place this in ~/.bashrc
:
export LESS="-X"
However, this disbles the termcap (terminal capability) initialization and deinitialization, so other views when you use less
may appear off.
Another option would be to use screen
and set the option altscreen off
in your ~/.screenrc
. less
will not clear the screen and should preserve color formatting. Presumably tmux
will have the same option.
This blog entry describes the problem and offers some different solutions specific to gnome-terminal
with varying success.
Less can determine if it has been given a directory. Set the LESSOPEN
environment variable to ~/bin/LESSPIPE
and make ~/bin/LESSPIPE
a script like the following:
#!/bin/sh
if [ -d "$1" ]; then
exec /bin/ls -la "$1"
elif [ -f "$1" ]; then
case "$1" in
*.tar|ztar) exec tar tvvf "$1" 2>/dev/null;;
*.tar.[dg]z|*.tar.z|*.tgz) exec gzip -dc "$1" | tar tvvf - 2>/dev/null;;
*.tar.bz2) exec bzip2 -d <"$1" 2>/dev/null | exec tar tvvf - 2>/dev/null;;
*.tar.Z|*.taz) exec uncompress -c "$1" | exec tar tvvf - 2>/dev/null;;
*.zip) exec unzip -l "$1" 2>/dev/null;;
*.lha) exec lha -v "$1" 2>/dev/null;;
*.7z) exec 7z l "$1" 2>/dev/null;;
*.[rs]pm) exec rpm -qilp "$1";;
*.z|*.[dg]z) exec gzip -dc "$1" 2>/dev/null;;
*.bz2) exec bzip2 -dc "$1";;
*.xz) exec xz -dc "$1";;
*.Z) exec uncompress -c "$1" 2>/dev/null;;
esac
fi
You'll get a listing for directories and archives.
You can use lesskey
instead of the environment variable to set the preprocessor. See the documentation for details.
Best Answer
In
less
, you can typeF
(Shift+F) to keep reading at the end of a file (liketail -f
); you can type:e
and a file name to view a different file, but unfortunately, if you enter the name of the current file,less
doesn't reload the file. However there's a trick to make it re-read the current file, suggested by sabgenton: type:e
and enter the name of a non-existent file; that causes less to display an error message and then reload the current file.If you're looking for an alternative to
watch ls
, here are a few:auto-revert-mode
, it will reload the file or directory every 5 seconds (the delay is configurable).R
to reload the (local) URL.