Amirul Asyraf
Posted on March 11, 2021
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
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
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;
:Sexplore
:Explore
For :Sexplore
, it will be like this;
to this
For :Explore
, it will be like this;
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
ork
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;
- Navigate to the directory where you want to create the new file/directory.
- 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; - Type
a
to add a child node and then simply enter the filename. - 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.
Posted on March 11, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.