Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 588
Dawg Avatar asked Nov 05 '12 20:11

Dawg


2 Answers

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!

like image 123
Ingo Karkat Avatar answered Oct 24 '22 10:10

Ingo Karkat


you should have a try with "nerdtree" plugin. In the nerdtree window, you typed key m, and file operation choices will display to you

like image 28
harris Avatar answered Oct 24 '22 08:10

harris