Is there a way to dynamically refresh the less command?

I like the watch command, but it has its limitations.

I’m curious to know whether I could mimic the functionality of watch with less. I’m mainly looking for the ability to scroll through my directory as it dynamically gets modified via a running script.

Asked By: Zaid

||

The “F” key when running less will do a “follow” similar to tail -f, but I’m not sure if that will achieve what you’re looking for here.

Answered By: Dave Jennings

Shift+F will make less similar to the command tail -f. That is, it gets refreshed if more data is appended to the file.

Answered By: balki

You could pipe it to tail -f instead, it would result in you following the output. You’d be losing the ability to move (scroll) through your output though.

Answered By: Gert

In less, you can type F (Shift+F) to keep reading at the end of a file (like tail -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:

  • Modern file managers (e.g. Nautilus, Thunar, Konqueror, Dolphin, Finder) refresh views in real time.
  • Emacs doesn’t have real-time refresh, but with auto-revert-mode, it will reload the file or directory every 5 seconds (the delay is configurable).
  • Although w3m is primarily a web browser, it makes a passable directory and text file viewer. Press R to reload the (local) URL.

You can use vim to read the file then add the following mapping to your .vimrc file and you can easily reload a file with ,r:

let mapleader = ","
nnoremap <leader>r :edit <CR>

Note if you edited the file already, vim will complain. Just change to

let mapleader = ","
nnoremap <leader>r :edit! <CR>

To ignore changes.

Answered By: puk

I normally just type G to tail the output on a one-time basis. I find it especially helpful over a a network file system like CIFS.

Answered By: jorb

Simply type:

less +F filename

This emulates pressing “F” within the editor.

Answered By: jhclark

man pages can be very informative. Don’t be intimidated by them. Among everything else, man less says you can use the R command to:

   R      Repaint the screen, discarding any buffered input.  Useful if the file is changing while it is being viewed.

(I realize this question is over 6 years old, but it comes up on google searches, so I’m not the only one that clicked the link to get here.)

Answered By: Dennis Estenson

R for repaint does not always reload the file.[1]

A workaround that always reloads the file is to press hq, which will open the Help page, then quit. It has a side effect of forcing the file to reload.


[1] Here are some examples of situations that R do and do not reload:

  • > and >> changes: DO get reloaded
  • sed -i, gEdit, TextEdit: DO NOT get reloaded
  • On Linux, vi changes: DO get reloaded
  • On Mac, vi changes: DO NOT get reloaded

I believe the difference comes down to whether the inode changes (you can check with ls -i foo.txt). If the inode changes, then R will not work.

Answered By: wisbucky

I just found this thread like anyone else.
I would like to add the solution of when you are already at the end of the file, using ‘g’ followed by ‘G’ will force a refresh of the file.

I ended up making a macro button for this in my terminal program (SecureCRT).
The macro is simply ‘gG’.

Answered By: WhoKnowsIfThisIsRight

I worked around this problem using a shell script that auto-reloads less on exit:

while true; do less -K file.txt || exit; done

With this script, I can hit q to reload the file, and CTRL+C to exit back to the bash shell. The CTRL+C behavior is enabled via the -K option. Your last search term will be preserved.

This can be further refactored by using the colon (:) to create an empty expression, via do :

while less -K file.txt; do : ; done

Drawbacks

The current viewing position will always be rest to line 0.

Practical Example using mintty

In my windows (GitBash) environment I set up a script that opens a new terminal window (mintty) for less-viewing a file:

lesswin() { mintty bash -c "while less -K "$@"; do : ; done;" & }
Answered By: jstine
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.