17 November, 2009

VIM editor tips for file/source code editing

VIM Tips for file or source code editing

Hi all i will be updating this post with the vim usage for file editing or C source code editing which i found useful

1. To open two or more files in the same window
vim file1 file2
or
after opening the first file with vim file1 , type(command mode) :sp file2. This will open file2. Like this you can open more than two files.

2. Scrolling two files together

This is very useful for comparing two source codes.

Open two files as mentioned in tip no.1 , then type :set scrollbind in the first file's command and then type the same command for the second file. After that if you scroll the lines both the files lines will be scrolled together.

3. vim changing background color
:highlight Normal ctermbg=white ctermfg=black

4. vim status line with information's about the file opened

Type :set laststatus=2  vim wil display a basic status line.

If you want more information in the satus line read the below points.
  • Name of the file that is being edit
  • Format of the file that I am editing (DOS, Unix)
  • File type as recognized by Vim for the current file
  • ASCII and hex value of the character under the cursor
  • Position in the document as row and column number
  • Length of the file (line count)
The following command will turn your status line into a true information bar with all the above information:
:set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]

To make these settings permanent every time you open vim

In the ~/.vimrc  file inserts the below two lines. So if you want it in very user,
you have to edit the vimrc file in the /home/username/.vimrc .  
If the vimrc file is not there you can create one 
 
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]
set laststatus=2 
 
 


5. Highlighting search matches in vim 
 set hlsearch , also add it in the ~/.vimrc file 
set incsearch . If you set the 'incsearch' option, Vim will show the first 
    match for the pattern, while you are still typing it. 
   
6. Indenting for C style
    Type the below comamnd or save it in the ~/.vimrc file

set autoindent
set cindent

To indent two spaces (instead of one tab of eight spaces, the vim default):
set shiftwidth=2









No comments:

Post a Comment