Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Switching from TextMate to Vim, I'd like to have Vim launched and opening a specific folder at startup on MacOS?

Manually I can start Vim from the shell with mvim someFolder. I use MacOS 10.6 and NERDTree.



Reading and trying to apply the proposed answers (thanks by the way) I realize I was probably not clear enough in my question:

I want MacVim automatically launched at startup, and with opening a specific folder of mine. Later on during my MacOS session, if for any reason I open anew MacVim, I don't want it to open this specific folder, I want the MacVim default behavior.

I could do that with TextMate by going to MacOS System Preference, Accounts, Login Items and adding here my TextMate project file. As this project file was associated with TextMate, it was enough to have TextMate automatically launched at startup and displaying this project files list.

Is there a way to achieve this with Vim?

share|improve this question
did you read my last comment? I think it's the simplest way to achieve your goal. Try with this in your shell script : cd /path/to/folder/ && mvim +NERDTree . It does exactly what you want. You may combine it with @Jack M's answer for a more customized workspace. – romainl Apr 1 '11 at 9:09
@romainl Sorry, I had missed your comment. I've just commented to your questions, indeed your solution is the easiest, thanks! – Florent2 Apr 1 '11 at 13:20
up vote 2 down vote accepted

If you want MacVim to open the folder every time you might as well add an alias to your .profile or .bashrc like this:

alias vimm='mvim /path/to/some/folder'

This way, you can still launch MacVim for some task unrelated to the content of your folder (like editing your .profile or some config file) with the regular mvim command.

share|improve this answer
thanks, that sounds fine for me, but how then can I have this vimm be executed at MacOs startup? – Florent2 Mar 30 '11 at 16:11
@Florent2 You can create a shell script with the regular shebang plus this line: mvim /path/to/some/folder or simply vimm if you have created the alias, make it executable and add it to the list of startup items under the "Account" pane of System Preferences. Or use launchd or any of the probable dozen of solutions available. – romainl Mar 30 '11 at 19:24
thanks! I got your solution working, was the easiest for me (just had to change the extension of the shell script from .sh to .command, else the shell script would be opened in my default editor instead of being run during the MacOS startup) – Florent2 Apr 1 '11 at 13:18

To open NERDTree to the specific directory you can add the following line to your ~/.gvimrc (or ~/.vimrc if you want it to work in console as well):

au VimEnter * NERDTreeToggle /path/to/your/folder/
share|improve this answer
thanks but when I add NERDTreeToggle /path/to/my/folder/ to ~/.vimrc and then run mvim I get the error Not an editor command: NERDTreeToggle /path/to/my/folder. I'm new to vim, so I probably missed something obvious? – Florent2 Mar 30 '11 at 1:29
The vimrc is sourced before any plugins, so that command is not available. I have edited the answer to use an autocmd that will run when Vim has finished starting. – Raimondi Mar 30 '11 at 1:51
@ElIsra thanks it works but I want this behavior only at the MacOs startup. I've edited my question to make it more clear. – Florent2 Mar 30 '11 at 16:13

To make this launch at startup, you'll have to jump through some hoops.

Create a session file using .vims as the extension:

Open MacVim and open any/all of the files/tabs/NERDTrees you would like to have open. Save this to a session with the .vims extension:

mksession! ~/Desktop/project.vims

This can go in any directory you'd like, I'm just using the Desktop for ease of use.

Create an Automator Application:

Open Automator and create a new Custom application. Add a "Run Shell Script" action to your workflow. Change the Shell to /usr/bin/ruby, and change the "Pass input" to "as arguments", then paste this code into the body of the action:

file_name = ARGV[0].split(' ').join('\ ')
command = %Q!/usr/local/bin/mvim -S "#{file_name}"!

Check the location of mvim with which, and update as necessary. Save the workflow (I put it in ~/Desktop/SU) and change the "File Format" to "Application".

Associate your Automator Application with .vims files.

Right click on ~/Desktop/project.vims and select "Get Info". Under "Open with:", select your Automator Application, and tell it to "Change All...".

Drop into your Login Items.

Open System Preferences, Accounts, Your User, and select the "Login Items" tab. Drag your ~/Desktop/project.vims file into the list.


Log out and back in and MacVim should open automagically.


For some reason Automator keeps running while this session if VIM is active. If anyone knows how to fix this, please let me know.

share|improve this answer
thank for your detailled answer! Finally I use Romainl's solution, was the easiest for me. – Florent2 Apr 1 '11 at 13:23

You can create an applescript to run a shell command and add it to your startup items

-- Open MacVim
do shell script "/path/to/mvim /path/to/folder"


    -- Open Vim on the terminal
    tell application "Terminal"
        do script "/path/to/vim /path/to/folder"
    end tell

using the AppleScript Editor.

share|improve this answer
thanks, finally I used the Romainl's solution, was the easiest for me. I tried yours, but the applescript would not be ran at startup, only be opened in the applescript editor. I suppose there was a some additional thing to setup (for instance for the shell script I had to change the extension from .sh to .command). – Florent2 Apr 1 '11 at 13:22
You can find some choices in the "Save as..." dialog. – Raimondi Apr 1 '11 at 16:44

What I find most useful is to open the project files in tabs using NERDTree and then make a session file:

mksession! project.vim

The exclamation mark is to overwrite the previous project.vim should you add a new file to the project. Then, anytime I want to fire up vim, I just have to call:

vim -S project.vim

I guess you could source the project.vim directly in the vimrc for a more automated approach.

share|improve this answer
thanks I could create the project.vim file but when I run mvim -S project.vim the NERDTree panel is empty. – Florent2 Mar 30 '11 at 1:31
@Florent2. I would add: set autochdir to the end of the generated vim file or to the vimrc. This way, when you source the file it will go straight to the directory of the buffer you are editing, thus filling the nerdtree. – celebdor Mar 30 '11 at 8:14
thanks, unfortunately could not make it work with adding set autochdir in my vimrc file, and adding it to the generated vim file is not possible as I would to think to redo it each time I overwrite it. – Florent2 Mar 30 '11 at 16:24
@Florent2 You are right, I was not aware that the autochdir messed up with sessions. SO maybe you could just map some keystrokes to change into the current buffer's directory like this (proposed in map ,cd :cd %:p:h<CR>:pwd<CR> – celebdor Mar 30 '11 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .