11

Is it possible to edit a directory inside Vim? (Some commands are working for directories).

I want to know what kind of operations can be done in a directory inside Vim. Vim can open the directories for editing, I believe it will have commands also to work with. But usual commands behave differently for directories.

For example, pressing I and typing a filename and saving the directory. When I give ls, it should show that file.

2
  • I would love the ability to rename files when viewing a directory listing as a text file. I don't see anything out there :( Commented Aug 28, 2020 at 4:19
  • Actually you can do :e some/directory. I'm not sure what it does though... Commented Feb 3, 2023 at 21:17

2 Answers 2

10

I'm assuming you are using netrw here, as it's the default action when you do vim . or vim some/directory/.

You can create a file/directory from netrw with the following commands:

d : create a directory, you will be prompted to input the directory name
% : create a file, same workflow

Have a look at :h netrw for more informations.

1
  • See :h netrw-quickmap for bindings.
    – mcp
    Commented May 14, 2023 at 1:49
3

Totally a different angle, but it works without having to install any plugins ...

When I want to create or rename a directory from a running Vim session, I'll simply type :! followed by the appropriate shell command, doing something like the following:

For Linux

  • :! mkdir newdir to create a directory
  • :! mv newdir renamed_dir to rename a directory

For Windows

  • :! mkdir newdir to create a directory
  • :! move newdir renamed_dir to rename a directory

As far as I know, you can literally execute whatever shell command you like this way without having to exit from Vim or having to open a separate terminal window.

2
  • Thanks. but I am not asking about shell commands. I want to explore the options related to editing directory, like we edit a file.
    – SibiCoder
    Commented May 5, 2016 at 13:44
  • 3
    It's worth knowing the github.com/tpope/vim-eunuch plugin also.
    – nobe4
    Commented May 5, 2016 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.