Comment And Uncomment Lines in Vim

I used to use IDE and I’m sure all of you familiar with command Ctrl + / or Cmd + / to add comment to your codes.

And here is how to bring that magic to VIM. Put these following lines to your .vimrc

" Commenting blocks of code.
autocmd FileType c,cpp,java,scala let b:comment_leader = '// '
autocmd FileType sh,ruby,python let b:comment_leader = '# '
autocmd FileType conf,fstab let b:comment_leader = '# '
autocmd FileType tex let b:comment_leader = '% '
autocmd FileType mail let b:comment_leader = '> '
autocmd FileType vim let b:comment_leader = '" '
noremap ,cc :silent s/^/=escape(b:comment_leader,'\/')/:nohlsearch
noremap ,cu :silent s/^\V=escape(b:comment_leader,'\/')//e:nohlsearch

Now you can select your text and type ,cc to comment and ,cu to uncomment.


Posted in Vim

VIM: Create New File/Folder in NERDTree

Days 2 using vim as my editor. NERDTree is very important plugin that you have to install to your VIM for exploring your project files.
Here are how to create file/folder through NERDTree. Once you opening NERDTree, then press “m”, it will show you these options.

NERDTree Menu. Use j/k/enter and the shortcuts indicated
> (a)dd a childnode
  (m)ove the current node
  (d)elete the current node
  (c)opy the current node
  (l)ist the current node

After that you just need to press “a” to add new file/folder, and it will show you this prompt.

Enter the dir/file name to be created. Dirs end with a '/'

Then type your filename or folder. Bear in mind, if you adding “/” to your name it will create you a folder, otherwise it will create a new file.