You can use GNU's source-highlight, as shown here (path may differ, see below):
export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"
export LESS=' -R '
As of Debian Stretch and Fedora 25, package names and script paths differ
Debian:
sudo apt install libsource-highlight-common source-highlight
dpkg -L libsource-highlight-common | grep lesspipe
# /usr/share/source-highlight/src-hilite-lesspipe.sh
Fedora:
sudo dnf install source-highlight
rpm -ql source-highlight | grep lesspipe
# /usr/bin/source-highlight/src-hilite-lesspipe.sh
Simple, using inotifywait (install your distribution's inotify-tools
package):
while inotifywait -e close_write myfile.py; do ./myfile.py; done
or
inotifywait -q -m -e close_write myfile.py |
while read -r filename event; do
./myfile.py # or "./$filename"
done
The first snippet is simpler, but it has a significant downside: it will miss changes performed while inotifywait
isn't running (in particular while myfile
is running). The second snippet doesn't have this defect. However, beware that it assumes that the file name doesn't contain whitespace. If that's a problem, use the --format
option to change the output to not include the file name:
inotifywait -q -m -e close_write --format %e myfile.py |
while read events; do
./myfile.py
done
Either way, there is a limitation: if some program replaces myfile.py
with a different file, rather than writing to the existing myfile
, inotifywait
will die. Many editors work that way.
To overcome this limitation, use inotifywait
on the directory:
inotifywait -e close_write,moved_to,create -m . |
while read -r directory events filename; do
if [ "$filename" = "myfile.py" ]; then
./myfile.py
fi
done
Alternatively, use another tool that uses the same underlying functionality, such as incron (lets you register events when a file is modified) or fswatch (a tool that also works on many other Unix variants, using each variant's analog of Linux's inotify).
Best Answer
There are two possibly relevant commands detailed in the fine manual for
less(1)