OS X + Vim: Open Finder for a Folder in NerdTree (Vim)

Solution 1:

This is available out-of-the-box with NERDTree now as part of the fs_menu.vim plugin. In fact, this feature only works on the Mac:

if has("gui_mac") || has("gui_macvim") || has("mac")
    call NERDTreeAddMenuItem({'text': '(r)eveal in Finder the current node', 'shortcut': 'r', 'callback': 'NERDTreeRevealInFinder'})
    call NERDTreeAddMenuItem({'text': '(o)pen the current node with system editor', 'shortcut': 'o', 'callback': 'NERDTreeExecuteFile'})
    call NERDTreeAddMenuItem({'text': '(q)uicklook the current node', 'shortcut': 'q', 'callback': 'NERDTreeQuickLook'})
endif

Navigate to a node in the NERDTree view, type m to bring up the fs menu, then o to open the current node. Directories will be opened in a new Finder window, files will be opened in the application you have set as the default editor for the file's type.

This is accomplished compliments of the open command:

function! NERDTreeExecuteFile()
    let treenode = g:NERDTreeFileNode.GetSelected()
    if treenode != {}
        call system("open '" . treenode.path.str() . "'")
    endif
endfunction

Solution 2:

The shell command to open a folder is

$ open /path/to/folder

In a Vim function, this command could be invoked like this:

let g:mypath = /path/to/folder
execute "silent !open " . g:mypath

NERDTree's documentation has a simple and easy to follow example:

call NERDTreeAddKeyMap({
       \ 'key': 'foo',
       \ 'callback': 'NERDTreeCDHandler',
       \ 'quickhelpText': 'echo full path of current node',
       \ 'scope': 'DirNode' })

function! NERDTreeCDHandler(dirnode)
    call a:dirnode.changeToDir()
endfunction

that you can expand using the global objects listed a few lines above:

g:NERDTreePath
g:NERDTreeDirNode
g:NERDTreeFileNode
g:NERDTreeBookmark