Dread
Dread

Reputation: 861

Save file in NERDTree

Here's how I currently save a file in Vim if I want to save in a sub folder:

:w /home/username/notes/file.txt

Is there a way to use NERDTree to select a folder and save the current document? If not, what's the best way to avoid typing out the path as I've done above?

Upvotes: 0

Views: 764

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172540

I would use NERDTree's cd command to change the directory to the target one; then, you can simply save the file with :w file.txt in that directory.

Alternatively, you can extend the plugin with custom mappings that yank the current filespec / path into a register. You can then insert the contents into the :write command-line via <C-R>{register}. Put the following into a file ~/.vim/nerdtree_plugin/yank_mapping.vim:

function! NERDTreeYankCurrentNode( modifiers )
    let l:node = g:NERDTreeFileNode.GetSelected()
    if l:node != {}
        let l:contents = fnamemodify(l:node.path.str(), a:modifiers)
        call setreg(v:register, l:contents, 'v')

        " It's helpful to print the contents, too.
        echomsg l:contents
    endif
endfunction
function! NERDTreeYankCurrentNodeFilename()
    return NERDTreeYankCurrentNode(':t')
endfunction
function! NERDTreeYankCurrentNodeAbsoluteFilespec()
    return NERDTreeYankCurrentNode(':p')
endfunction
function! NERDTreeYankCurrentNodePathspec()
    return NERDTreeYankCurrentNode(':p:h')
endfunction
function! NERDTreeYankCurrentNodeRelativeFilespec()
    return NERDTreeYankCurrentNode(':~:.')
endfunction


call NERDTreeAddKeyMap({
\   'key': 'yr^',
\   'callback': 'NERDTreeYankCurrentNodeFilename',
\   'quickhelpText': 'yank file name of current node in the passed register'
\})
call NERDTreeAddKeyMap({
\   'key': 'yr>',
\   'callback': 'NERDTreeYankCurrentNodeAbsoluteFilespec',
\   'quickhelpText': 'yank full absolute filespec of current node in the passed register'
\})
call NERDTreeAddKeyMap({
\   'key': 'yr<',
\   'callback': 'NERDTreeYankCurrentNodePathspec',
\   'quickhelpText': 'yank full absolute pathspec of current node in the passed register'
\})
call NERDTreeAddKeyMap({
\   'key': 'yy',
\   'callback': 'NERDTreeYankCurrentNodeRelativeFilespec',
\   'quickhelpText': 'yank filespec relative to CWD of current node in the passed register'
\})

Upvotes: 2

Related Questions