首页 > 解决方案 > 在作为参数给出的目录中打开 neovim/nerdtree

问题描述

我正在将neovim与nerdtree一起使用。当以目录作为参数打开nvim时,我想将其用作 neovim 和 nerdtree 的根目录。开箱即用,nerdtree 在那个目录中打开,但是一旦我打开一个文件,nerdtree 的根目录就会变回 shell 的当前目录。我希望 nerdtree 将该目录保留为根目录。

我目前在我的中使用以下解决方法.bashrc

nvim_cd()
{
    if [ -d "${1}" ]; then
        local dir="${1}"
        shift 1
        ( cd "${dir}" && nvim "." "${@}" )
    else
        \nvim "${@}"
    fi
}
alias nvim=nvim_cd
alias vim=nvim

这工作得很好,但我想知道:有没有办法做到这一点init.vim

请注意,我只想在 nvim 以目录作为参数调用的特定情况下在启动期间更改根目录,所以 set autochdir 这不是我想要的。

标签: neovimnerdtree

解决方案


您可以通过 .bash_profile 或 .bahrc 中的这一行将 neovim 设置为默认编辑器。

export EDITOR='nvim'

然后,您可以使用此脚本指定一个目录并通过 xargs 命令将其导入neovim。您可以将此行作为别名或函数。

$HOME/your/path/to/dir/  | xargs -r -I % $EDITOR %;

您可能要考虑检查我的搜索文件或目录的功能,然后在 neovim 中打开它们。蛮好用的。。

function ds() {
   find $HOME -type d -name "*" ! -path "*/.git*" ! -path # You can exclude directories by specifiying their names.
   "*/__pycache__*" ! -path "*/venv*"  2>/dev/null | fzf | xargs -r -I % $EDITOR %;
}

此函数搜索您的目录并将所选目录作为参数通过管道传输到 neovim。上面的函数搜索你的主目录。您可能想考虑它可能会弹出一些不需要的目录,例如 library 或 .locals 文件夹。因此,使用此功能仅通过您的密码进行搜索。

function ds() {
   find . -type d -name "*" ! -path "*/.git*" ! -path # You can exclude directories by specifiying their names.
   "*/__pycache__*" ! -path "*/venv*"  2>/dev/null | fzf | xargs -r -I % $EDITOR %;
}

如何设置 NERDTree 以更改为当前工作目录?

autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

这将自动切换您的 NERDTree 并匹配您当前的工作目录。

如果你发现这个惊人的结帐我的 NeoVim 配置链接。


推荐阅读