random guy
random guy

Reputation: 59

how to open vim in a specific directory

Right now I have a project that holds the code for the backend and frontend. Root folder: Project then this folder has this following contents.

backend/
frontend/
package.json

But everytime I open vim in the Project/backend directory, it opens my vim on the Project directory. Is there a way to make my vim open in Project/backend?

I currently have 2 plugin installed. Nerdtree and Vimfugitive

Upvotes: 0

Views: 3405

Answers (2)

romainl
romainl

Reputation: 196876

$ cd Project/backend
$ vim

is the simplest way to start Vim in Project/backend.

Once in Vim, you can use:

  • :help :cd to change the working directory globally,
  • :help :lcd to change the working directory for the current window,
  • :help :tcd to change the working directory for the current tab page.

Example:

:cd ../frontend

Upvotes: 1

David Goldstein
David Goldstein

Reputation: 394

Without any configurations to Nerdtree, vim /path/to/file should open up Nerdtree on file if it is a directory (hijacking Netrw, vim's default file explorer, which would have opened on the directory otherwise). If you're sitting in Project/backend/, you should be able to run vim . to open the backend/ directory.

If you're asking how to open Nerdtree rooted at that directory from within vim, you can run :NERDTree /path/to/dir, and you'll have opened a Nerdtree rooted at dir, which you may now toggle with :NERDTreeToggle. Note that when you open a directory with vim /path/to/dir from the commandline, you'll have opened the directory file with Nerdtree however the vim window's Nerdtree won't actually be rooted anywhere so using :NERDTreeToggle (or :NERDTree) will use vim's current working directory, which will by default be the folder you were in when you ran vim, and not necessarily /path/to/dir.

It might be worthwhile writing a little script in your config to store the initial directory you opened vim in in a variable so that you can have NERDTree original_dir bound to a key if you want to be able to easily open Nerdtree on the original directory you opened. Perhaps someone who knows more about Nerdtree might know of an easier way.

Upvotes: 2

Related Questions