When using the vim editor with the NERDTree plugin to navigate through the tree of your project, is there an easy way to create a new source code file under the currently highlighted directory?
Currently I go into my shell, add the file and then refresh the tree. There must be a better way.
Activate the NERDTree and navigate to the directory in which the new file should live. Then press m to bring up the NERDTree Filesystem Menu and choose a for "add child node". Then simply enter the file's (or directory's name) and you're done.
From vim you can run shell commands. So in this case I use:
:!touch somefile.txt
and then hit r to reload the nerdtree window.
The other thing to do is to just start the new file from within vim.
:e somefile.txt
One handy thing for this is that in my .vimrc I auto change the cwd to the directory my current file is in:
" Auto change the directory to the current file I'm working on
autocmd BufEnter * lcd %:p:h
This way if I'm editing a file and want another one in the same place the path is changed right there. Opening any file from NERDTree sets the directory to the one that file is in.
Related
I would like to open NERDtree automatically but only when passing a folder argument in command line, and that it would not open two tree viewers, only NERDtree. For example, if I am on folder 'rootFolder', which has a 'childFolder' and I run:
nvim childFolder
I would like it to open vim like if I had done:
cd childFolder
nvim
:NERDtree
And if I open a file or empty I would like it to not show nerdtree. That way if I just work on a single file like:
nvim
or
nvim test.js
or
nvim reminder.txt
I don't need to see NERDtree, because I don't need it, it's just a test script or a notes file I am leaving myself.
I found a workaround, add these two lines in .vimrc:
let g:NERDTreeHijackNetrw = 1
au VimEnter NERD_tree_1 enew | execute 'NERDTree '.argv()[0]
That's exactly how NERDTree behaves if 'NERDTreeHijackNetrw' is set to 1 (which it is by default.)
If I open a folder in vim like this:
$ mvim . # or vim .
NERDTree opens by default in full width:
How can I prevent this from happening and show default VIM welcome screen instead?
You are explicitly telling vim to start with a listing of the current directory, if you don't want that, just do $ vim.
Run vim to get the Welcome Screen.
If you open vim . you'll get a directory listing of the current working directory (Netrw or NERDTree Directory Listing).
NERDTree overrides the default file browser (netrw).
To disable directory listing by NERDTree at startup, add let g:NERDTreeHijackNetrw=0 to your ".vimrc".
This Option tells NERD tree whether to replace the netrw autocommands for exploring local directories.
Run vim --noplugin . and you see an empty buffer.
I open up a project in Vim. Lets call it portfolio. I open up the index.php but now want to leave that and go to another file, controller.php. I learned that the command :e. will take me out but then it may lead me outside the scope of my project.
How do I stay within the scope of my project when leaving a file in vim?
If I understand your question correctly, you want :e. to show you the files in the same directory as the first file. If that's correct, and if the files are in one directory, use :cd path-to-your-project-directory
That will change your working directory to your project directory, and subsequently :e. will show the files in that directory.
Note too that once you use :e. (or :Explore :Texplore or :Sexplore), you can navigate through your directory hierarchy by navigating to subdirectories and pressing enter, or to the .. entry and pressing enter to move up a directory.
You may also be interested in the NERDTree plugin.
Your initial working directory in vim is the directory you're in when you start vim. If you're in a directory other than your project directory when you start vim, :e will not change your current directory to your project directory when you open a file, so if you just use :e to open different files, you'll have to specify the path each time.
eg: say your project path is /project/foo and you cd to /project and open vim. To open /project/foo/a.txt, you'll have to :e foo/a.txt. If you then want to open file /project/foo/b.txt, you'll have to :e foo/b.txt. :e doesn't change the current working directory in vim. If you cd /project/foo then start vim, you can just do :e a.txt and :e b.txt
You could also do :cd foo from within vim in the scenario above.
Hope this helps
I would say, just open controller.php with :sp controller.php you can then edit both files and switch between then with: ^w k and ^w j
I agree with other answers:
:e. provides a listing of the current working directory.
Plugins such as NERDTree build on this basic idea but also allow you to store bookmarks to commonly visited files and other cool features, see
If you augment this with plugins like bufkill, and tagbar you can actually build up quite a feature rich IDE where you can edit project files within the same context. I put together an installer script you can grab from here
Another alternative is the autochdir option which changes your working directory to that of the current buffer (so each time you change tabs/windows/buffers it changes the current directory to that of the file being edited). Link
It's not usually recommended because it doesn't play nicely with some plugins.
I'm a new user of NERD tree in Vim and am obviously not that much familiar with its features.
When I'm using :NERDTreeToggle, the sidebar window always opens my home directory, ~. How can I change the default directory (like open a project in Sublime Text)?
Also, how can I keep this sidebar window open in all of the new tab windows (something like the Sublime Text sidebar)? Or at least, is there another alternative to this task?
In your .vimrc file, add the following code, which will by default open Vim with a NERD tree sidebar of the current directory. So if you are in the projects directory and you type "vim" it will open Vim with a sidebar on the left showing all the files and directories in the projects folder
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
If you want toggle on and off the sidebar, just add this into your .vimrc file so that if you want to toggle the sidebar, just type Ctrl + N:
map <C-n> :NERDTreeToggle<CR>
Vim has no concept of "project".
The closest you can get without installing clunky plugins is the "current directory": when you start Vim, the "current directory" is set to the directory where you started Vim.
In your shell, this is easy to manage:
$ cd /path/to/project
$ vim
:pwd --> /path/to/project
If you use gVim or MacVim, the "current directory" is usually set automatically to $HOME if you start Vim without a file so, either you find a way to start Vim in an arbitrary directory or you use :cd /path/to/dir as soon as possible.
Without argument, the :NERDTree* commands open the NERD tree window in the "current directory".
You can use :NERDTreeToggle /path/to/dir to make it display the content of a specific directory.
Or you can make sure you start Vim from your project's directory and NERD tree will do what you want it to do.
I find myself in the position where I want to create a new file in the same directory as the one that the open file is in. How do I create a new file in the directory of the open file in vim? Also, is there a a place where I can learn these things on my own? Googling didn't help.
From within Vim, new files are created like existing files are edited, via commands like :edit filename or :split filename. To persist them to disk, you need to (optionally type in contents and) persist them via :write.
Like a command prompt, Vim has a notion of current directory (:pwd lists it). All file paths are relative to it. You don't need to duplicate the path to your current file, there are some nice shortcuts for them: % refers to the current file, :h is a modifier for its directory, minus the file name (cp. :help filename-modifiers). So,
:e %:h/filename
:w
will create a new file named filename in the same directory as the currently open file, and write it.
Alternatively, some people like Vim to always change to the current file's directory. This can be configured by placing
:set autochdir
into your ~/.vimrc file (which is read on Vim startup). Then, above becomes simply
:e filename
:w
Finally, Vim has a great built-in :help. Learn to navigate and search it!
you should have a try with "nerdtree" plugin.
In the nerdtree window, you typed key m, and file operation choices will display to you
If you want to create a new file and also show it in the window next to your current file, you can try this:
:vsp newfile
The vsp stands for vertical split, and it splits the screen in half, one showing your current file, the other showing your new file (also works with just sp, which is a horizontal split).
Per #MartinLyne's comment above, this will create the file in the directory of the file in which you opened vim. To adjust for this, you can change the current working directory as follows:
:cd %:p:h
This command changes the current working directory to the directory of the active file, meaning that running the vsp command (or any of the commands above) will create the file in that directory.
I usually use:
:tabnew my-file
Then add some content and:
:w
It will create new tab with new file.
(I use Vim 8)
When you have opened vim in non existent location like
$ vim /etc/<some_folder/<next_folder>/file.cfg
then to create a new directory while being inside vim, just run in normal mode
:! mkdir -p /etc/<some_folder/<next_folder>
next save your doc as usual :w :x ZZ (whatever you like)
that's it
I'm quite late to the party, but another option is to open NERDtree with :E or :Explore (or its splitting alternatives :Vexplore/:Sexplore == :Vex/:Sex).
In NerdTree you can create a new file with %, and type the name. It will automatically open the file, and create it after you :w/save it.
This is for Gvim!
Enter this to see the current directory.
:cd
then change it with
:cd desktop/somefolder
then save or make new file there
:enew asd.cpp
now again see the file
:cd
With NERDtree
ma <FILENAME>
ma <DIRECTORY NAME> + /