I want to write a script that opens a specified file with MacVim and then waits until the file is closed before continuing. My original idea was:
#!/bin/sh
file="some-file.txt"
mvim $file
cat $file | pbcopy
This opens the specified file with MacVim and immediately executes the next command. I've been thinking of something incredibly hackish with lsof
but I was hoping there is a better way.
If this isn't possible with plain shell script, I'm open to ideas in anything else. (AppleScript, etc.)
Set up an autocommand to execute the command when vim (or mvim) exits. I tested with
$ mvim -c 'au VimLeave * !cat % | pbcopy' /tmp/bar.txt
It is a little shorter to use pbcopy < %
.
Be careful of what you ask for. You might change buffers, and % refers to the file when vim exits. Maybe this would be safer:
$ mvim -c 'execute "au VimLeave * !pbcopy < " . expand("%")' /tmp/bar.txt
MacVim has an option to not fork on startup:
-f or --nofork Foreground: Don't fork when starting GUI
via mvim --help
For the above script, the following works:
#!/bin/sh
file="some-file.txt"
mvim --nofork $file
cat $file | pbcopy
GVim also has an identical option:
--nofork Foreground. For the GUI version, Vim will not fork and
detach from the shell it was started in.
via man gvim
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With