Re: detaching instance of a process ran in Vim's command mode

14 views
Skip to first unread message

Manas Gupta

unread,
Mar 31, 2020, 3:11:12 AM3/31/20
to v...@vim.org
So the situation is as follows:

I want to run an instance of Okular for every markdown file using bang (!) in Vim's command mode (like this :!okular % &). And I leave the Okular instance open on the side. But when I make changes in the file and save it, Okular also makes the changes in its instance which I don't want.
I also added & at the end of shell command in hope that it will detach that process but that doesn't seem to work as I want.

Can someone suggest something?

Thanks


--
Manas

Cameron Simpson

unread,
Apr 1, 2020, 1:15:44 PM4/1/20
to vim...@googlegroups.com, v...@vim.org, manas...@iiitd.ac.in
On 31Mar2020 12:40, Manas Gupta <manas...@iiitd.ac.in> wrote:
>I want to run an instance of Okular for every markdown file using bang
>(!)
>in Vim's command mode (like this :!okular % &). And I leave the Okular
>instance open on the side. But when I make changes in the file and save it,
>Okular also makes the changes in its instance which I don't want.

So this means that Okular monitors the contents of the filename why you
gave it.

If that is true, this means that Okular needs to be given a copy of your
file, not the original (because you want to overwrite the original
without disturbing Okular).

On that basis I would write a script which does 3 things:
- take a copy of the filename handed to it
- dispatches Okular against the copy
- after the instance of Okular exits, remove the copy file

Something like:

#!/bin/sh
set -ue
cmd=$( basename "$0" )
[ $# = 1 ] || { echo "Usage: $cmd filename" >&2; exit 2; }
filename=$1
fbase=$( basename "$filename" )
tmpf=${TMPDIR:-/tmp}/$cmd.$$.$fbase
( okular "$tmpf"
rm "$tmpf"
) >/dev/null &

which makes the copy and runs an asynchronous okular with cleanup.

>I also added & at the end of shell command in hope that it will detach that
>process but that doesn't seem to work as I want.

No, because your problem is stopping Okular watching the file you're
editing, hence the copy above.

The "&" isn't magic - it just says "continue without waiting for this
command to finish".

Cheers,
Cameron Simpson <c...@cskk.id.au>
Reply all
Reply to author
Forward
0 new messages