"Turning off expandtab in VIM for editing makefile
let _curfile = expand("%:t")
if _curfile =~ "Makefile" || _curfile =~ "makefile" || _curfile =~ ".*\.mk"
set noexpandtab
else
set expandtab
set tabstop=4
set shiftwidth=4
endif
"showing all opened files in vim
map <F4> :ls<CR>:buffer<space>
"showing the status bar with extra info
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]
set background=dark
set autoindent
if you had set the .vimrc to change the tab to spaces for C programming, it will be difficult to edit or create makefiles,
i had trouble creating makefile , i was getting the below error
Makefile:18: *** missing separator. Stop.
after googled little and found the solution, edit the .vimrc file like below
let _curfile = expand("%:t") if _curfile =~ "Makefile" || _curfile =~ "makefile" || _curfile =~ ".*\.mk" set noexpandtab else set expandtab set tabstop=4 set shiftwidth=4 endif
while using vi in Oracle virtual box with Ubuntu 12.04 as the guest OS with Windows 7 as the host, when we press arrow keys in insert mode, it used to print B, A, D, C
to fix this add the below line to the .vimrc file, if it is no file create it
set nocompatible
after this setting if you open the vi , the problem should not come
For converting tabs to spaces while editing the source codes
1) To insert space characters whenever the tab key is pressed.
:set expandtab
2) To control the number of space characters that will be inserted
when the tab key is pressed, set the 'tabstop' option.
To
insert 4 spaces for a tab, you have to type the below command
In VI/VIM below commands can be used Ctrl-] - go to function definition Ctrl-T - Jump back from the definition. Ctrl-W C-] - Open the definition in a horizontal split
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