Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically determine when MacVim finishes editing a file

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.)

like image 600
Chris Knadler Avatar asked Feb 15 '23 06:02

Chris Knadler


2 Answers

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
like image 88
benjifisher Avatar answered Apr 27 '23 07:04

benjifisher


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

like image 20
Chris Knadler Avatar answered Apr 27 '23 07:04

Chris Knadler