简体   繁体   English

在作为参数给出的目录中打开 neovim/nerdtree

[英]Open neovim/nerdtree in directory given as argument

I'm using neovim with nerdtree.我正在将 Neovim 与 nerdtree 一起使用。 When opening nvim with a directory as argument, I'd like to use that as the root directory for both neovim and nerdtree.当使用目录作为参数打开nvim时,我想将它用作 neovim 和 nerdtree 的根目录。 Out of the box, nerdtree is opened in that directory, but once I open a file, the root directory of nerdtree changes back to the shell's current directory.开箱即用,nerdtree 在该目录中打开,但是一旦我打开一个文件,nerdtree 的根目录就会变回 shell 的当前目录。 I'd like nerdtree to keep that directory as root instead.我希望 nerdtree 将该目录保留为根目录。

I'm currently using the following workaround in my .bashrc :我目前在我的.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

This works just fine, but I'm wondering: Is there a way to achieve this in init.vim ?这工作得很好,但我想知道:有没有办法在init.vim实现这init.vim

Note that I only want to change the root directory during startup in the specific case that nvim is invoked with a directory as an argument, so set autochdir is not what I'm looking for.请注意,在使用目录作为参数调用nvim的特定情况下,我只想在启动期间更改根目录,因此set autochdir不是我要找的。

You can make your neovim your default editor by this line in your .bash_profile or .bahrc.您可以通过 .bash_profile 或 .bahrc 中的这一行将您的 neovim 设为默认编辑器。

export EDITOR='nvim'

Then you can use this script to specify a directory and pipe it into neovim via the xargs command.然后你可以使用这个脚本来指定一个目录并通过 xargs 命令将其通过管道传输到 neovim。 You can make this line as an alias or a function.您可以将此行作为别名或函数。

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

You might want to consider to checkout my function for searching files or directories and then opening them in neovim.您可能需要考虑查看我的功能来搜索文件或目录,然后在 neovim 中打开它们。 It is quite useful..挺有用的。。

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 %;
}

This function searches through your dirs and pipes the selected directory into neovim as an argument.此函数搜索您的目录并将所选目录作为参数通过管道传输到 neovim。 The above function searches through your home directory.上述函数搜索您的主目录。 You might wanna consider that it may pop-up some dirs that won't be necessary like the library or .locals folder.您可能想考虑它可能会弹出一些不需要的目录,例如库或 .locals 文件夹。 So use this function to search only through your pwd.因此,使用此功能仅搜索您的密码。

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 %;
}

How to set NERDTree to change to Current working directory?如何设置 NERDTree 更改为当前工作目录?

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

This will automatically toggle your NERDTree and also match your current working directory.这将自动切换您的 NERDTree 并匹配您当前的工作目录。

If you find this amazing checkout my link to my NeoVim configs.如果您发现这个惊人的结帐,请查看我的 NeoVim 配置链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM