Setting path with autocommand within a directory tree in Vim - vim

Let's say I have project residing in /home/myname/project. There are couple of subfolders in it too. Can I make a autocommand, so that it sets the path to /home/myname/project/** whenever I open a file from any subdirectory of the project?

Try the command below.
:autocmd BufNewFile,BufRead /home/myname/project/* sil! cd %:h
Use the :lcd command instead of :cd to change the current directory only
for the current window.

Related

Change Netrw's browsing directory to Vim's current directory?

While browsing in Netrw, how do I change Netrw's browsing directory to Vim's current directory?
Typing cd changes Vim's current directory to Netrw's browsing directory, but I want the opposite.
Type :e . in the netrw window to reset to your vim current directory.

set .vim home directory to something specific

My projects are all under /Users/username/workspace/my_project where /Users/username/ is $HOME.
I want macvim to always have the home as /Users/username/workspace/my_project instead of /Users/username/.
I understand set autochdir sets the home to whatever the current file's directory is, but this is not what I want.
Also I'm using NerdTree if that helps.
Do you want Vim's working directory to be ~/my_project?
Add cd ~/my_project to your ~/.vimrc.
Do you want all your plugins and colorschemes to be loaded from ~/my_project, however silly it is?
See :help runtimepath.
add autocmd VimEnter * :cd ~/my_project/ to your vimrc
the directory will change only when your open vim, and you can decide change to another directory whenever you want use :cd /directory/

Create a new file in the directory of the open file in vim?

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> + /

NERDTree Load Particular Directory Automatically

I'm using gvim in windows.By default nerdtree loads C:\ drive as default.I like to change it to E:\ drive.How to achieve this?
Whenever I try to start NERDTree using :NERDTree command, I get this error E492: Not an editor command: NERDTree
I have the following code in my vimrc file
cd ~/documents
map <F2> :NERDTreeToggle<CR>
" open Nerd Tree in folder of file in active buffer
map <Leader>nt :NERDTree %:p:h<CR>
The cd command is not NerdTree specific. It just changes the working directory when Vim starts to something which for me is sensible.
From the NERDTree help-file:
:NERDTree [<start-directory> | <bookmark>] *:NERDTree*
Opens a fresh NERD tree. The root of the tree depends on the argument
given. There are 3 cases: If no argument is given, the current directory
will be used. If a directory is given, that will be used. If a bookmark
name is given, the corresponding directory will be used. For example: >
:NERDTree /home/marty/vim7/src
:NERDTree foo (foo is the name of a bookmark)
In my vimrc file,I use this autocmd vimenter * NERDTree G:\.The G:\ should be set your default driveļ¼Œbut, it's only the NERDTree's work dir. add cd G:\change the vim start work dir.Just like Jeromy Anglim said that.

vim and NERD Tree extension - adding a file

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.

Resources