"Edit in External Editor..." feature from the command line (or from mutt)

187 views
Skip to first unread message

Cyclify

unread,
Mar 18, 2008, 7:37:23 PM3/18/08
to vim_mac
The "Edit in External Editor..." feature is great. I would like to
have the same functionality from the command line (specifically, from
my email client, mutt). Can you suggest how this might be done?

The standard approach is to call Vi in the terminal. However, I would
like to edit all my files in MacVim. I can't simply issue an "open -a
MacVim" command since this returns; the call must be a blocking one.
Perhaps one of the scripts (gvim) blocks?

Ideally, I would like the call to open the file in a buffer in a new
tab in an existing window of MacVim, and to return once I've deleted
the buffer. Should this be done with the remote client/server
functionality?

Currently I have this working, through the following route: I call a
script instead of MacVim. The script creates a lock file, calls
MacVim, and then waits for the lock file to be removed. When I'm done
editing the file, I issue a command in Vim that writes the file,
deletes the buffer and removes the lock file. The script and the
function are copied below.

My solution works, but I'm wondering if there's a more direct route --
one that doesn't write lock files. Any thoughts or comments would be
appreciated.

Kudos for doing such a great job with MacVim.

--Marc


Script that creates a lock file, calls MacVim, and then waits for the
lock file to be removed:

#! /bin/tcsh

if !(-f $1) touch $1
touch $1.lck
open -a MacVim $1
while (-f $1.lck)
sleep 0.1
end

Vim function that writes the file, deletes the buffer, and then
removes the lock file:

function! <SID>WriteDeleteUnlock()
let l:lockfile = expand("%:p").'.lck'
write
execute("bdelete")
call system("rm -f ".l:lockfile)
endfunction

Ben Schmidt

unread,
Mar 18, 2008, 8:36:53 PM3/18/08
to vim...@googlegroups.com
Cyclify wrote:
> The "Edit in External Editor..." feature is great. I would like to
> have the same functionality from the command line (specifically, from
> my email client, mutt). Can you suggest how this might be done?
>
> The standard approach is to call Vi in the terminal. However, I would
> like to edit all my files in MacVim. I can't simply issue an "open -a
> MacVim" command since this returns; the call must be a blocking one.
> Perhaps one of the scripts (gvim) blocks?

I think you should be able to do

/Applications/MacVim.app/Contents/MacOS/Vim -f "$1"

or similar, using the mvim script included with MacVim. It will open a Vim
instance (MacVim window) but remain in the foreground (block) until that window is
closed (and thus the corresponding Vim instance exits).

> Ideally, I would like the call to open the file in a buffer in a new
> tab in an existing window of MacVim, and to return once I've deleted
> the buffer. Should this be done with the remote client/server
> functionality?

The --remote-tab-wait Vim option is for precisely this, I believe, so something like

/Applications/MacVim.app/Contents/MacOS/Vim --remote-tab-wait "$1"

or with the mvim script.

See :help --remote-tab-wait

Ben.


travis

unread,
Mar 18, 2008, 9:28:29 PM3/18/08
to vim_mac
In your .muttrc file just do 'set editor = mvim'
and call MacVim in the terminal by mvim, is that too simple or am I
missing something?

Timothy Knox

unread,
Mar 18, 2008, 9:32:49 PM3/18/08
to vim...@googlegroups.com
Somewhere on Shadow Earth, at Tue, Mar 18, 2008 at 06:28:29PM -0700, travis wrote:
>
> In your .muttrc file just do 'set editor = mvim'
> and call MacVim in the terminal by mvim, is that too simple or am I
> missing something?

Yes, unfortunately, you are. mutt (and many other Unix-y programs that support
an external editor) need the editor to not exit until the edit is done. For
example, I use mutt and vim to process my mail, and I have gvim wrapped in a
shell script called gvimf, that just does a 'gvim -f "$@"' so that it will stick
around until I am done, then return the edited file to mutt to send.

--
Timothy Knox <mailto:t...@thelbane.com>
The one thing I've learned about freedom of expression is that you really
ought to keep that sort of thing to yourself.
-- Scott Adams, _I'm Not Anti-Business, I'm Anti-Idiot_

Nico Weber

unread,
Mar 19, 2008, 2:55:09 AM3/19/08
to vim...@googlegroups.com
>> In your .muttrc file just do 'set editor = mvim'
>> and call MacVim in the terminal by mvim, is that too simple or am I
>> missing something?
>
> Yes, unfortunately, you are. mutt (and many other Unix-y programs
> that support
> an external editor) need the editor to not exit until the edit is
> done. For
> example, I use mutt and vim to process my mail, and I have gvim
> wrapped in a
> shell script called gvimf, that just does a 'gvim -f "$@"' so that
> it will stick
> around until I am done, then return the edited file to mutt to send.

set editor ="mvim -f"

should work, shouldn't it? At least `export EDITOR="mvim -f"` works
for git and svn.

Nico

Ben Schmidt

unread,
Mar 19, 2008, 9:35:27 AM3/19/08
to vim...@googlegroups.com

Yes, I think so. Or

editor="mvim --remote-tab-wait"

I wonder where my earlier post that suggested those two things went.

Ben.


Cyclify

unread,
Mar 19, 2008, 5:23:53 PM3/19/08
to vim_mac
Ben and Nico,

Thanks for your reply!

"mvim --remote-tab-wait" is exactly the functionality that I need.

--Marc

John_108

unread,
Apr 13, 2012, 12:27:17 PM4/13/12
to vim...@googlegroups.com
This worked great for me:


> set editor ="mvim -f"

except that after writing the file and closing MacVim, I had to switch back to the terminal to get back to mutt.

In the current MacVim version 7.3.390 (and maybe for much longer) the solution is found in "help: hints":

mvim -f -c "au VimLeave * maca hide:

Works like a charm. When I start to compose in Mutt, MacVim opens, and when I finish the focus returns to mutt automatically.

Reply all
Reply to author
Forward
0 new messages