DEV Community

Amirul Asyraf
Amirul Asyraf

Posted on

How to make a new file or directory in vim

There are many ways to create a new file or directory in vim tho. Either vanilla UNIX Shell command, with vim itself, or with the plugin. We will go through all of these.

1) UNIX Shell command

First, you need to enter command mode with ESC, and then;

:! touch new-file.txt
:! mkdir new-directory
Enter fullscreen mode Exit fullscreen mode

These will create the directory and file in CURRENT working directory, which is the directory when you started your current vim process in the beginning.

When in doubt, always use :!pwd to check your current working directory.

2) vim-eunuch plugin

A great plugin for these actions is vim-eunuch, which gives you a lot of sugar for the UNIX shell commands. Here's the latter example, using vim-eunuch:

:Mdkir new-directory
Enter fullscreen mode Exit fullscreen mode

same as step 1, but easier to type. There is no need to call external commands with !.

3) Explore mode

Explore mode is really cool. There are two ways two open Explore mode. Type one of these commands;

  1. :Sexplore
  2. :Explore

For :Sexplore, it will be like this;
Alt Text
to this
Alt Text
For :Explore, it will be like this;
Alt Text

The difference is where the explore mode opens. You need to choose one that is better for you.

note: I'm using the NERDTree plugin.

Then, you can easily navigate to whatever sub-directory you like by ;

  • typing j or k to move the cursor
  • press Enter to enter a sub-directory
  • press - to go up a level of directory

Now, you can type;

d to create a directory
% to create a file

4) Using NERDTree

This is my favorite because I am using NERDTree for my Neovim setup. Follow these step;

  1. Navigate to the directory where you want to create the new file/directory.
  2. Press m to bring up the NERDTree Filesystem Menu. This menu allows you to create, rename, and delete files and directories. It will be like this; Alt Text
  3. Type a to add a child node and then simply enter the filename.
  4. You’re done! 🥳

note: To create a directory follow the same steps but append a / to the filename.

That is why I love this step.


resources;
1 2 3

Discussion (0)