DEV Community

Shriharsh
Shriharsh

Posted on

Today I Learned: Some VIM tricks

VIM Trick 1 - Create new file using NERDTree plugin

Many VIM users depend on NERDTree plugin to browse directory but rare use it to create a new file on without leaving Vim (at least I was one such Vim user).

I found nice blog entry which told me how to do so - Link here

To summarize -

  • Open NERDTree panel
  • Navigate to the directory where you want to create new file or directory.
  • Press m - which opens up NERDTree Filesystem Menu.
  • Type a to create the child node and type the name of file or directory. For directory append / at the end.
  • That's it.

VIM Trick 2 - Create new file using VIM built-in feature.

While I was experimenting on the steps above I made a silly mistake. I didn't open the newly created file before typing my experiment with NERDTree. So I typed my content in an un-named buffer. To wiggle out, I decided to rely on VIM help. This is what I did -

  • Type :h save - this opened up the help window with VIM
  • The saveas command in VIM helps us same the current buffer into a file.
  • Use ! option to save to an existing file.
  • I will let the reader experiment with this with their own hands-on exercise.

Top comments (5)

Collapse
 
jorinvo profile image
jorin

You can also use :e path/to/file.txt to open any file or if you want to write a buffer to another file you can do :w path/to//other/file.txt.

Also not that you can open a new empty buffer with :new (in split view) or :enew.

If you want to get rid of a buffer without saving it, you can do :bd!.

Collapse
 
shriharshmishra profile image
Shriharsh

Thanks Jorin. :w with filepath is new to me. :)

Collapse
 
alexparker profile image
Alex Parker

I love nerd tree, and do probably half of my file management in it, moving copying, renaming, etc with that m command you can do a whole slew of things!

Collapse
 
moopet profile image
Ben Sinclair

I'm the opposite. I don't understand its appeal, and would rather just use the built-in stuff.

Collapse
 
rossijonas profile image
Jonas B. R.

Same as you, I don't understand the hype... it seems like changing desktop wallpaper.. no difference at all on your workflow