wow what the powerful editor is this VIM
Posted by pankaj4u4m on November 11, 2009
vim is a powerful editor in linux operation system and i found some of very usefull tips in vim from net.
1>if you want to add some of line while opening some specific file then you can do by adding following lines in you .vimrc file
first of all create a .vimrc file like:
[codegambler@localhost ~]$ vi ~/.vimrc
then add these lines in it (this is for c/cpp header)
autocmd BufNewFile *.c r ~/.vim/mainheader
autocmd BufNewFile *.cpp r ~/.vim/mainheader
another use of autocmd is set default setting for filetype like
autocmd FileType html setlocal shiftwidth=2 tabstop=2
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
also by using
autocmd FileType cpp source ~/.vim/cpp.vim
autocmd FileType py source ~/.vim/py.vim
one usefull command to indent lines in c/c++ file(one of the prefered setting )
add
autocmd FileType cpp setlocal expandtab shiftwidth=4 softtabstop=4 cindent number
to your .vimrc file
you can make a file
[codegambler@localhost ~]$ vi ~/.vim/mainheader
and add like this
#include
#include
int main()
{
}
code. when you will create any c/cpp file this default code will automatically insert
2> using calculator inside your vim
if you want to do some basic calculation like +,-,*,% then this will automatically do for you
just add these lines in your .vimrc file
inoremap <C-B> <C-O>yiW<End>=<C-R>=<C-R>0<CR>
next time when you will type
2+2
now press ctrl+b …. and it will replace it with ..
2+2=4
what a amazing feature is;
3> like most editor you want to undo your some of code to previous version. you can do it by using command
: earlier 10
this will take you back to before 10 modification
:earlier 10s
this will take you 10s back into your code
:later 5 will forward the undo code
what a nice command
be happy and free to code you program in vim
i found these tips in this site ajay somani’s blog. which i found useful to me.
some other tips also whic are not in that site are here
1> encrypt you file inside vi
just create your file like
[codegambler@localhost ~]$ vi -x myufile
now you enter your pass* . now whenever you will open your file it will decrypt the content and whenever you close it it will again encrypt and save it..
if you know any good tips which can help to learn vim better please add it here… you are always welcome
HAPPY CODING