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.
You can make your neovim your default editor by this line in your .bash_profile or .bahrc.
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.
You might want to consider to checkout my function for searching files or directories and then opening them in neovim. It is quite useful..
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.
How to set NERDTree to change to Current working directory?
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.