global vimrc in linux

the location is /etc/vimrc, to set tab width to 4 spaces, add the following lines

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
set softtabstop=4
set nowrap

how to comment block of codes in Vim & gVim

Put your cursor on the first # character, press Ctrl V (or Ctrl Q for gVim), and go down until the last commented line and press x, that will delete all the # characters vertically.

For commenting a block of text is almost the same: First, go to the first line you want to comment, press Ctrl V, and select until the last line. Second, press Shift Esc (then give it a second), and it will insert a # character on all selected lines. For the stripped-down version of vim shipped with debian/ubuntu by default, type : s/^/# in the second step instead.

Remove unwanted whitespace in Vim

From: http://vim.wikia.com/wiki/Remove_unwanted_spaces

In a search, \s finds whitespace (a space or a tab), and \+ finds one or more occurrences.

Delete all trailing whitespace (at the end of each line) with: Edit

:%s/\s\+$//

Like with ed(1), the substitution text can be omitted if blank:

:%s/\s\+$

More rarely, a user might want to delete (leading) whitespace at the beginning of each line:

:%s/^\s\+
" Same thing (:le = :left = left-align given range):
:%le

With the following mapping a user can press F5 to delete all trailing whitespace. The variable _s is used to save and restore the last search pattern register (so next time the user presses n they will continue their last search), and :nohl is used to switch off search highlighting (so trailing spaces will not be highlighted while the user types). The e flag is used in the substitute command so no error is shown if trailing whitespace is not found. Unlike before, the substitution text must be specified in order to use the required flag.

:nnoremap <silent> <F5> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar>:nohl<CR>

See Highlighting whitespaces at end of line to display, rather than delete, unwanted whitespace.

elimite ^M in gvim

(from: http://stackoverflow.com/questions/5843495/what-does-m-character-mean-in-vim)

This happens when you have a mixture of Windows line endings and Unix ones. If you have 100 lines, 99 are \r\n and one is \n, you’ll see 99 ^M characters. The fix is to find that one line and replace it. Or run dos2unix on the file. You can replace the Windows line endings with:

:%s/\r\(\n\)/\1/g