Vim Tips Wiki
(tweak wording and formatting)
(Change <tt> to <code>, perhaps also minor tweak.)
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1645
|previous=0
+
|previous=1644
|next=0
+
|next=1646
 
|created=January 4, 2010
 
|created=January 4, 2010
 
|complexity=basic
 
|complexity=basic
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>
 
<pre>
  +
source ~/.bash_profile # necessary to locate mvim
file_name = ARGV[0].split(' ').join('\ ')
 
  +
FILE=$@ # the session file to open
system(%Q!mvim -S "#{file_name}"!)
 
  +
FILE=${FILE// /\\ } # replace spaces with escaped spaces
  +
mvim -S "$FILE" # -S open MacVim sourcing the file
 
</pre>
 
</pre>
  +
''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]]
Save the application.
 
  +
*Save the Automator Application in some folder that is already in your path.
 
Set the Automator application as the default app for your session files. You can do this using File Inspector, check Google.
+
*Set this application as the default app to open Vim session files (borrowed from the Finder Help):
  +
*#Select the document in Finder 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==
 
==Result==
Whenever you double-click a session file in Finder, the session will automatically open and source in Vim.
+
Whenever you double-click a session file in Finder, will open Vim or MacVim sourcing the saved session.<br />
  +
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==
 
==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)
 

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