Thursday, April 15, 2010

VIM



VIM 
 
:set -- To see current configuration of VIM
How to recover from swap:
1 Open the swap file .file.swp in vim
2 :recover will recover your file 

wrap:
set wrap -- to wrap lines to fit into window

Set highlight search color
set hlsearch
hi Search ctermbg=LightYellow
hi Search ctermfg=Red

Registry:
Registers in Vim let you run actions or commands on text stored within them. To access a register, you type "a before a command, where a is the name of a register. If you want to copy the current line into register k, you can type
"kyy
Or you can append to a register by using a capital letter
"Kyy
You can then move through the document and paste it elsewhere using
"kp
To paste from system clipboard on Linux
"+p
To paste from system clipboard on Windows (or from "mouse highlight" clipboard on Linux)
"*p
To access all currently defined registers type
:reg

Changing Color scheme
colorscheme default/morning/evening etc..

show scripts running under vim:
scriptnames

Block commenting:
1. Move to the first column in the first line you want to comment-out.
2. Press Ctrl-V to start block-mode selection.
3. Move down to select the first column of a block of lines.
4. Press I and then the desired comment starter (i.e. // or #)
5. Press ESC and the insertion will be applied to the whole block.
To uncomment with this techniques follow the directions but instead of I use x to delete the first char.

VIM diff:
To get the diff between current open file file1 and another file file2:
:vert diffsplit file2
To get the diff between two files files1 and file2:
:tabe file1
:vert diffsplit file2
To traverse between the diffs:
[c :Jump backward to the previous start of a change
]c: Jump forward to the previous start of a change

 vimdiff /path/to/file scp://remotehost//path/to/file

[[     --   Jump to funtion start.
[{    --   Jump to block start.
%    --   Jump to matching parenthesis.
=%  --  Indent code between parenthesis. 
*      --  Search the word under the cursor.

Multiple searches at same time:
/ \|

Multiple search & replace at same time:
:%s/aaa/bbb/e | %s/111/222/e

To white spaces(by default endof line is shown with '$' and tab as ^I):
:set list
:set list! --- no form of above command

Ex editor commands:
:e#     --  opens last opened files.
:ls       --  List the list opened files through vim.
:e#n   --  Will re-open the  nth opened file, listed in ":ls" command.

:b#     --  opens last opened opened file("Ctrl-6" does the same).
:bn      --  Will re-open the  nth opened file, listed in ":ls" command.
:b[TAB] -- Will list the buffers.
:b [any partof file name][TAB] -- auto completes the complete file name in the buffer.

Basic configuration of .vimrc
set nocompatible
set bs=2
set shiftwidth //No of spaces to be used for each step of (auto)indent.

Search ∧ Replace:
:%s/abc.*//gc -- remove trailing abc including abc from a line
:%s/abc\zs.*//gc -- remove trailing abc excluding abc
:%s/.*\ze\abc//gc -- remove leading abc excluding abc 
%s/.*abc/abc/ -- remove leading abc excluding abc
:%s/.\{-}abc/abc -- remove leading abc excluding abc fist occurrence
%s/.*\ze\<undefined\>//g  --- remove leading abc excluding undefined.
 
%s/<[^>]*>//gc  -- replace angular bracket contents to different string
g!/<pattern>/d -- delete lines which doesn't match pattern
 
:put =range(1,100)  -- set number on each line


%s/\s\+$//  -- remove  all spaces and tabs at the end of the line

 :.,$s/pattern1/pattern2/gc  -- to find and replace from the current location to end of the file.
:1,.s/pattern1/pattern2/gc -- to find and replace from start of the file to current location.

Add a word at particular coloumn at all lines of the file
 
set ve=all
Go to the column number
Ctrl-v
select number of rows we need to copy.
press r
type the character
escape
Done :-) 

-----------------------------
word completion: 
ctrl-x ctrl-n -- Word completion – forward
CTRL-x CTRL-p -- Word completion – backward

 
Ctrl+x Ctrl+l -- line completion
 
Ctrl+x Ctrl+f -- file completion
  
set dictionary+=/usr/share/dict/words  -- dictionary words

Ctrl+x Ctrl+k -- dictionary word completion
 
 

set nocompatible: This option stops vim from behaving in a strongly vi-compatible way. It should be at the start of any vimrc 
file as it can affect lots of other options which you may want to override.
*****************************************
Syntax highlighting not working in vim:
Just check your vim runtimepath settings by giving following command:
:set rtp
Check whether your runtimepath path info has the same vim version location 
as your vim version if not change the path location to your vim version.
For example:
if the ":set rtp" is 
set runtimepath=~/.vim,/usr/share/vim/vim63/macros/vimfiles,/usr/share/vim/vim63,/usr/share/vim/vim63/macros/vimfiles/after,~/.vim/plugin
but your vim version is vim7.0 then change the runtimepath to following:
set runtimepath=~/.vim,/usr/share/vim/vim70/macros/vimfiles,/usr/share/vim/vim70,/usr/share/vim/vim70/macros/vimfiles/after,~/.vim/plugin
*****************************************
TO grep in VIM:

in ex editor

: grep -rin "label" *
This will give the results in buffer. If you type "ENTER" there, you will be taken to first item in the search list.
To traverse through the all the items of the search. type ":cscope" on ex editor which will open the buffer.
You can use the arrow keys to traverse the buffer & to the item press "ENTER".
Other option to move between the search items is use ":cn" and ":cp"

To get Help from VIM, try
:h

To traverse between the tags use "ctrl-t" and "ctrl-]"

Ctrl-[ can be used as alias to ESC key to enter command mode
:set wildmode=longest,list -- This will find the longest match with the first 'wildchar', then list all matching files with the next.
:set ai -- To set auto indentation.
:history -- to see the history of commands in ex-mode
:history / -- to get search history of vim.
q: -- in the normal mode to open history of commands window. You can search/edit here using regular vim commands. You start in Normal mode. Press 'Enter' to execute a command.
:u -- undo changes.
Ctrl -R -- redo changes.
ga -- show ascii value of character under cursor in decimal, hex, and octal.

Follow the follow commands to indent total file: gg=G
gg -- to go to the top of file
= -- To fix the indentation
G -- Perform the operation till the end of the file.

Ctrl-w c -- Make buffer hidden and close window.
Ctrl-w q -- Quit editing and close window
Ctrl-w o -- Make current window only one on the screen.
Ctrl-w s -- Splits the window Horizontally.
Ctrl-w v -- Splits the window Vertically.
Ctrl-w w -- To navigate between the windows.
Ctrl-w ] -- Split window and jump to tag under cursor
Ctrl-w n -- Create new empty window
Ctrl-w r -- Rotate windows downwards.
Ctrl-w R -- Rotate windows upwards.
CTRL-W x -- Exchange current window with next one
Ctrl-w = -- Make all windows equal height
CTRL-W - -- Decrease current window height
CTRL-W + -- Increase current window height
CTRL-W _ -- Set current window height (default: very high)


 :h quickfix -- Command help for Vim's special mode to speedup the edit-compile-edit cycle. :h csqf -- Help commands for Vim's cscopequickfix commands
VIM on windows:

Saving font & color setting on gvim -
First, specify your font by going to Edit > Select Font (btw, I'm on a Windows machine, so maybe the linux menus are different).

Second, after you've selected your font, enter the command :mkvimrc!. This will write your font choice (and key mappings, etc) to your _vimrc file. This will NOT save your color settings if you're using a theme, however. To tell gVIM to use a color theme at startup, go to the next step.

Third, to specify your color theme, enter the command :e _vimrc. This will open your VIM settings file for editing. Scroll to the bottom of the file and add the line "colors blue". You can specify whatever color theme you want in place of "blue", provided you use the name of the theme itself. No file references are necessary. And it's case-insensitive too, so you could enter "colors Elflord" or "colors elflord" and be fine either way. Just make sure you write the file to disk before you quit gVIM.

Windows GVIM:
.vimrc equivalent file can be found at: Edit > Startup Settings
:vimgrep -- equivalent grep command used
:cs -- to list the grep'd results 
Config:
colorscheme koehler
set wildmode=longest,list

References:
http://www.scribd.com/doc/33619079/VIM-%E2%80%93-VI-Improved
http://www.fprintf.net/vimCheatSheet.html
http://www.linuxquestions.org/questions/programming-9/how-to-save-gvim-settings-307945/
http://www.catswhocode.com/blog/100-vim-commands-every-programmer-should-know 
http://www.lilax.net/textfiles/2001-06-09/vim_multifile.html 
http://www.kevinberridge.com/2008/10/vim-file-editing.html 
http://www.openlogic.com/wazi/bid/188018/Vim-Undo-Tips-and-Trickshttp://www.openlogic.com/wazi/bid/188018/Vim-Undo-Tips-and-Tricks
http://vimdoc.sourceforge.net/htmldoc/cmdline.html#history 
http://vim.wikia.com/wiki/VimTip1572 --- highlight
http://stackoverflow.com/questions/6225291/vim-delete-all-text-in-line-before-after-highlighted-search-pattern 
https://stackoverflow.com/questions/1497958/how-do-i-use-vim-registers?rq=1
https://stackoverflow.com/questions/7103173/vim-how-to-change-the-highlight-color-for-search-hits-and-quickfix-selection

No comments:

Post a Comment