Dawg
Dawg

Reputation: 1653

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.

Upvotes: 147

Views: 232728

Answers (10)

Dan Andreasson
Dan Andreasson

Reputation: 16232

If you want to create a new file based on your current file's location, without leaving normal mode, you can use the following;

function! CreateFile(path)
    " If cancelled
    if a:path == ""
      return
    endif        

    " Split the path into directory and file parts
    let s:dir = fnamemodify(a:path, ':h')

    " Check if the directory exists, if not create it
    if !isdirectory(s:dir)
        call mkdir(s:dir, 'p')
    endif

    " Do we already have an extension?
    if match(a:path, '\.') >= 0
      " Open the new file
      exe 'vsp ' . a:path
    else
      " Open the new file and append the current file's extension
      exe 'vsp ' . a:path . '.' . expand("%:e")
    endif

endfunction

nnoremap <leader>o :call CreateFile(input('New file path: ', expand("%:p:h") . "/", "file"))<CR>

It does a few things;

  • I have it mapped to leader o
  • It prompts for new file path.
  • It pre-fills the current directory (not the working directory, but the directory of the file which is currently open) to the prompt.
  • If the directory and/or subdirectories doesn't exist it will create those.
  • If you don't include a extension, it will use the same extension as the current file's
  • Opens the new file in a split

Upvotes: 0

Harsh Verma
Harsh Verma

Reputation: 933

For convenience, you could create a custom command called :CreateFile in your vim config as follows:

" Command to create file in the opened file's directory
" Takes file_name as argument.
:command -nargs=1 CreateFile edit %:h/<args>

Usage:

:Command test.txt will create test.txt in the currently opened file's directory.

Was inspired by @Ingo's answer and this nice tutorial on creating custom commands in vim.

Upvotes: 0

Chau Giang
Chau Giang

Reputation: 1554

I usually use:

:tabnew my-file

Then add some content and:

:w

It will create new tab for new file.

(I use Vim 8)

Upvotes: 6

mati kepa
mati kepa

Reputation: 3241

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

Upvotes: 6

logbasex
logbasex

Reputation: 2312

With NERDtree

 ma <FILENAME>
 ma <DIRECTORY NAME> + /

Upvotes: 2

The Flash
The Flash

Reputation: 67

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

Upvotes: 2

mazunki
mazunki

Reputation: 732

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.

Upvotes: 4

harris
harris

Reputation: 1543

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

Upvotes: 24

Ingo Karkat
Ingo Karkat

Reputation: 172768

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!

Upvotes: 284

RocketDonkey
RocketDonkey

Reputation: 37279

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.

Upvotes: 19

Related Questions