Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Saturday, 26 July 2014

Automate your Vim - The power of autocmd

July 26, 2014 Posted by Dinesh , , , No comments


1. Vim templates


I often used to write sample C++ codes to test the functionality. But it is pain to add default code for every test code that I write. Vim has a brilliant solution for it. that is templates.

Create some template files in any of the directory you wish, here I created ~/.vim/templates/skeleton.cpp that contains default c++ code i.e #include, using namespace std, main,..etc.
Then add the below code to your ~/.vimrc  file. Now try to open a new cpp file and see the magic.

"vim to load default templates on opening cpp/c/sh files
augroup templates
autocmd!
"Auto load wile creating new files
autocmd BufNewFile *.cpp 0r ~/.vim/templates/skeleton.cpp
autocmd BufNewFile *.c 0r ~/.vim/templates/skeleton.c
autocmd BufNewFile *.sh 0r ~/.vim/templates/skeleton.sh
augroup END
view raw vimtemplate.vim hosted with ❤ by GitHub

Here we are telling vim that, if I try to open a new cpp file, read the content from ~/.vim/templates/skeleton.cpp file and write to the new file that is opened. Similarly you can have templates for all type of files.

This means, what ever you write in template will be loaded by default for every file you open newly using vim.


2. Auto compile C++ code on saving


We can even compile the code immediately after saving the file without typing any single command.  Add the below code to your vimrc file and try to save the test cpp file. It will create a executable with the name of the file.

1
2
"Auto compile on save
autocmd BufWritePost,FileWritePost *.cpp !g++ -Wall %:p -o %:r 

In vim :p expands to full file name path and :r expands to root of the file name (file extension will be removed)

But this is bad ! every time you try to save your file it tries to compile. So there is alternative, vim has quit event. i.e VimLeave.

1
2
"auto compile on quit - silent: do not report errors
autocmd VimLeave *.cpp !silent g++ -Wall %:p -o %:r

VimLeave event indicates vim to execute the command specified after ! before exiting the vim after writing viminfo file.

3. Abbreviations for faster coding


Abbreviations can save typing when you want to type same text multiple times thorough the document. and abbreviations can be defined to auto correct the typos.
Add the below code to your vimrc file and try to edit the cpp file. when every you type iff this will be auto corrected to if ()  and moves the cursor in between the parenthesis.

augroup abbrcmds
autocmd!
"short hands for cpp
autocmd FileType cpp :iabbrev iff if ()<left>
autocmd FileType cpp :iabbrev elf else if ()<left>
autocmd FileType cpp :iabbrev elsee else ()<left>
"short hands for shell
autocmd FileType sh :iabbrev iff if []<left>
autocmd FileType sh :iabbrev elf elseif []<left>
augroup END
view raw autocorrect.vim hosted with ❤ by GitHub

abbrev(ab) is used to create abbreviations, this works on normal mode. iabbrev is used to work with abbreviations on insert mode. and <left> is used to move the cursor one character left.

4. Auto comments


Similarly to automate commenting add the below code to vimrc.

augroup commentcmds
autocmd!
autocmd FileType python nnoremap <buffer> ,c I#<esc>
autocmd FileType sh nnoremap <buffer> ,c I#<esc>
autocmd FileType cpp nnoremap <buffer> ,c I//<esc>
autocmd FileType c nnoremap <buffer> ,c I//<esc>
augroup END
view raw autocomment.vim hosted with ❤ by GitHub

Vim will detect the file type using FileType event. And we are mapping the movement ,c to comment lines.
But this will comment only current line.

For commenting a block of text, go to the first line you want to comment, press Ctrl + V, and select until the last line. Second, press I// + Esc (then give it a second), and it will insert a // character on all selected lines.
ctrl+v
drop down using arrow key(select only first character of the each line)
shif+i
shift+//
esc
Un-commenting a block of text is almost the same: Put your cursor on the first # character, press CtrlV and go down until the last commented line and press x, that will delete all the # characters vertically.

Learning from http://learnvimscriptthehardway.stevelosh.com/

0 comments:

Post a Comment