Open neovim/nerdtree in directory given as argument

1.9k Views Asked by At

I'm using neovim with nerdtree. When opening nvim with a directory as argument, I'd like to use that as the root directory for both neovim and 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. I'd like nerdtree to keep that directory as root instead.

I'm currently using the following workaround in my .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?

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.

2

There are 2 best solutions below

1
Aaditya Pyarla On

You can make your neovim your default editor by this line in your .bash_profile or .bahrc.

export EDITOR='nvim'

Then you can use this script to specify a directory and pipe it into neovim via the xargs command. 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. 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. 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. 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?

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.

If you find this amazing checkout my link to my NeoVim configs.

0
hilipati On

You can change directory based on arguments with Vimscript

if argc() == 1 && isdirectory(argv(0)) | cd `=argv(0)` | endif

Adding the above line to init.vim (or .vimrc for Vim) should do the trick. For init.lua, surround with vim.cmd([[ and ]]).

It should work just like your bash function, but I'm not certain how it'll affect NerdTree (I use Neo-tree, which doesn't have the mentioned problem as far as I can tell).