在NERDTree中保存文件

时间:2017-11-06 01:43:16

标签: vim nerdtree

如果我想保存在子文件夹中,我现在如何在Vim中保存文件:

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

有没有办法使用NERDTree选择文件夹并保存当前文档?如果没有,那么避免输入路径的最佳方法是什么?我已经完成了上述工作?

1 个答案:

答案 0 :(得分:1)

我会使用NERDTree的cd命令将目录更改为目标目录;然后,您只需将:w file.txt文件保存在该目录中即可。

或者,您可以使用自定义映射扩展插件,将当前文件规范/路径放入寄存器。然后,您可以通过:write将内容插入<C-R>{register}命令行。将以下内容放入文件~/.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'
\})