Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
(6 intermediate revisions by 3 users not shown)
Line 11: Line 11:
 
|category2=Mac OS X
 
|category2=Mac OS X
 
}}
 
}}
On Mac OS X systems, you can automatically source Vim sessions by creating an Automator Application that forwards the file to Vim with the <tt>-S</tt> option.
+
On Mac OS X systems, you can automatically source Vim sessions by creating an Automator Application that forwards the file to Vim with the <code>-S</code> option.
   
 
==Procedure==
 
==Procedure==
Name all your Vim sessions with the same extension, for example "<tt>.vis</tt>".
+
*Name all your Vim sessions with the same extension, for example "<code>.vis</code>".
 
*Open ''Automator'' and create a workflow, using ''File > New''.
 
 
*Choose ''Application'' as the type for your document.
Create an Automator application. File > New
 
 
*In the ''Library'' of ''Actions'', choose ''Utilities''.
{{clear}}
 
 
*From the ''Utilities'' section, drag the ''Run Shell Script'' action.
[[File:Automator_new_application_sheet.png|Create a new application in Automator]]
 
 
*In the ''Run Shell Script'' window, choose ''/bin/bash'' in ''Shell'' pop-up button and ''as arguments'' in ''Pass input'' pop-up button.
 
 
*Write the script:
 
Add a "Run Shell Script" action.
 
{{clear}}
 
[[File:Session_automator_action.png|Action in automator]]
 
 
Edit the details of the action to start MacVim with the <tt>-S</tt> option applied to the argument (with a backslash preceding each space, if any, in the file name).
 
<pre>
 
file_name = ARGV[0].split(' ').join('\ ')
 
system(%Q!mvim -S "#{file_name}"!)
 
</pre>
 
 
Save the application.
 
 
Set the Automator application as the default app for your session files (from the Finder Help):
 
# Select the document and choose File > Get Info.
 
# Click the triangle next to “Open with” to expand the section.
 
# Choose an application from the pop-up menu, or choose Other to locate a different application.
 
# If you want to use that application to open all documents of the same type as the current document and created by the same application, click Change All.
 
 
==Result==
 
Whenever you double-click a session file in Finder, the session will automatically open and source in Vim.
 
 
==Comments==
 
I have applied a more standard formatting, and tweaked some wording. I suspect that some steps are missing ("check Google" and a bit more), and it would be helpful if someone with some MacVim smarts could confirm that the tip is adequate. I replaced [[:File:Shell script code.png]] with the actual text so it can be copied. Later, that image should be deleted as redundant. [[User:JohnBeckett|JohnBeckett]] 04:24, May 16, 2010 (UTC)
 
----
 
The image doesn't just show what text to enter, it also shows where to enter the text for those who don't know already. I think it adds value. It would add more value if it also showed how to bring up the text entry box shown, but maybe this is self-evident. Certainly we need to complete the instructions, and I'd like to expand this tip to include other mappings, like using --remote-tab-silent to launch files in tabs. We should cross-reference with the Windows and Unix file association tips.
 
 
--[[User:Fritzophrenic|Fritzophrenic]] 04:30, June 24, 2010 (UTC)
 
:Click [[:File:Shell script code.png]] to see the image that I removed (replacing it with the actual text; this [http://vim.wikia.com/index.php?title=Vim-sessions_under_Mac&oldid=25960 old version] of the tip shows how it appeared). I have no problem with the images that are currently in the article. [[User:JohnBeckett|JohnBeckett]] 04:49, June 26, 2010 (UTC)
 
----
 
[[User:Nfuria|Newton Furia]] 20:00, Mar 12, 2012 (BRT)
 
 
This Automator Application using ruby does not work properly in Mac OS Lion.<br />
 
I created an Automator ''MacVimSessions.app'' and setted it as the default app for my session files (*.vis).<br />
 
Then, when I open a session file from the terminal using <tt>$ open test.vis</tt> the ''MacvimSessions.app'' runs correctly, opening MacVim and loading the session.<br />
 
But when I double click the same ''test.vis'' file it doesn't work.<br />
 
And more strange, if I run the Application inside Automator (adding a temporary ''Get Specified Finder Items'' action to capture the session file), it works...
 
 
So, I did a modified version, that works correctly, using bin/bash as shell that is more simple:
 
 
<pre>
 
<pre>
 
source ~/.bash_profile # necessary to locate mvim
 
source ~/.bash_profile # necessary to locate mvim
FILE=$@ # the file to open
+
FILE=$@ # the session file to open
FILE=${FILE// /\\ } # replace spaces (who uses spaces in vim files?)
 
mvim -S "$FILE" # open MacVim
 
</pre>
 
Also, as already commented, I think that the procedure is not so clear and only one image showing more is better.
 
Bellow, commented (Edit to see), a new version of the whole tip to be reviewed.
 
 
<!--
 
----
 
==Procedure==
 
* Name all your Vim sessions with the same extension, for example "<tt>.vis</tt>".
 
* Open ''Automator'' and create a workflow, using ''File > New''.
 
* Choose ''Application'' as the type for your document.
 
* In the ''Library'' of ''Actions'', choose ''Utilities''.
 
* From the ''Utilities'' section, drag the ''Run Shell Script'' action.
 
* In the ''Run Shell Script'' window, choose ''/bin/bash'' in ''Shell'' pop-up button and ''as arguments'' in ''Pass input'' pop-up button.
 
* Write the script:
 
<pre>
 
source ~/.bash_profile # necessary to locate mvim
 
FILE=$@ # the file to open
 
 
FILE=${FILE// /\\ } # replace spaces with escaped spaces
 
FILE=${FILE// /\\ } # replace spaces with escaped spaces
mvim -S "$FILE" # open MacVim loading the file
+
mvim -S "$FILE" # -S open MacVim sourcing the file
 
</pre>
 
</pre>
''It's possible to write the script in some other language, but bash is more simple.''
+
''It is possible to write the script in some other language, but bash is more simple.''
   
 
[[File:automator_bash.png|Creating an Application in automator]]
 
[[File:automator_bash.png|Creating an Application in automator]]
* Save the Automator Application in some folder that is in your path.
+
*Save the Automator Application in some folder that is already in your path.
* Set these application as the default app to open vim session files (borrowed from the Finder Help):
+
*Set this application as the default app to open Vim session files (borrowed from the Finder Help):
*# Select the document and choose File > Get Info.
+
*#Select the document in Finder and choose File > Get Info.
*# Click the triangle next to “Open with” to expand the section.
+
*#Click the triangle next to “Open with” to expand the section.
*# Choose an application from the pop-up menu, or choose Other to locate a different application.
+
*#Choose an application from the pop-up menu, or choose Other to locate a different application.
*# If you want to use that application to open all documents of the same type as the current document and created by the same application, click Change All.
+
*#If you want to use that application to open all documents of the same type as the current document and created by the same application, click Change All.
  +
 
==Result==
 
==Result==
Whenever you double-click a session file in Finder, the session will automatically open and source in Vim or MacVim.<br />
+
Whenever you double-click a session file in Finder, will open Vim or MacVim sourcing the saved session.<br />
And it also works using a commnad line in Terminal: <tt>$ open test.vis</tt>
+
It also works using a command line in ''Terminal'': <code>$ open test.vis</code> <br />
  +
And can be useful when Vim is already running: using ''netrw browser'' and hitting <code>"x"</code> while cursor is on file name ''test.vis'' open a new MacVim window with this saved session.
   
 
==Comments==
-->
 

Revision as of 06:43, 13 July 2012

Tip 1645 Printable Monobook Previous Next

created January 4, 2010 · complexity basic · author Lotusone · version 7.0


On Mac OS X systems, you can automatically source Vim sessions by creating an Automator Application that forwards the file to Vim with the -S option.

Procedure

  • Name all your Vim sessions with the same extension, for example ".vis".
  • Open Automator and create a workflow, using File > New.
  • Choose Application as the type for your document.
  • In the Library of Actions, choose Utilities.
  • From the Utilities section, drag the Run Shell Script action.
  • In the Run Shell Script window, choose /bin/bash in Shell pop-up button and as arguments in Pass input pop-up button.
  • Write the script:
source ~/.bash_profile  # necessary to locate mvim
FILE=$@                 # the session file to open
FILE=${FILE// /\\ }     # replace spaces with escaped spaces
mvim -S "$FILE"         # -S open MacVim sourcing the file

It is possible to write the script in some other language, but bash is more simple.

Creating an Application in automator

  • Save the Automator Application in some folder that is already in your path.
  • Set this application as the default app to open Vim session files (borrowed from the Finder Help):
    1. Select the document in Finder and choose File > Get Info.
    2. Click the triangle next to “Open with” to expand the section.
    3. Choose an application from the pop-up menu, or choose Other to locate a different application.
    4. If you want to use that application to open all documents of the same type as the current document and created by the same application, click Change All.

Result

Whenever you double-click a session file in Finder, will open Vim or MacVim sourcing the saved session.
It also works using a command line in Terminal: $ open test.vis
And can be useful when Vim is already running: using netrw browser and hitting "x" while cursor is on file name test.vis open a new MacVim window with this saved session.

Comments