I had the problem of looking at contents of a file, update the file externally, display the changed file and have the option to edit it when needed. I solved it with the Vim server/remote command line options, inspired by this answer.
With the following commands a file can be reloaded via an external command. First, open the file in question with Vim, while starting a server instance.
vim --servername JABBERWOCKY /tmp/alice.log
Let's assume the file has been changed externally, e.g. by a echo "Curiouser and curiouser" >> /tmp/alice.log
. When necessary, send a command to the server instance to reload the file. This command will finish quickly and not spawn a new editor.
vim --servername JABBERWOCKY --remote-send ":e<CR>"
If the opened file was changed in Vim and externally, there will be a conflict during reload (a warning message will appear). For that case a decision has to be made: Leave it with that and keep changes in Vim, or discard local changes and force a reload.
The command with --remote-send
above requires manual reloading. However, that command could be tied to a simple loop, using Bash's for
/while
, or using a hacky one-liner in Linux/Unix:
watch -n 1 'vim --servername JABBERWOCKY --remote-send ":e<CR>"'
Although not tested, I'm certain that something like this could be realized in PowerShell, too.
On Linux/Unix an event-driven approach can be realized by listening to file system events using e.g. the inotify library, if your not using too crazy file systems. Personally I like using the tool entr a lot, which seems to be included in major Linux distributions. Then I get:
echo /tmp/alice.log | entr vim --servername JABBERWOCKY --remote-send ":e<CR>"
" Compare buffer with saved version
nnoremap gd :DiffSaved<CR>
:h vim-faq
and search/reload
. The hard to memorize tag is:h faq-5.8
.