Professional Documents
Culture Documents
VIM VI Improved
VIM VI Improved
Configuring and using VIM editor By Tusharadri Sarkar July 13, 2009 IBM
Tusharadri Sarkar
Vim is an improvement over the popular Vi editor The Vi editor was developed by Bill Joy in 1976 ed was the original UNIX text editor Bill Joy modified ed and named it ex He developed Vi as a Visual interface to ex Bram Moolenaar developed Vim in November,1991 for the Amiga Computer, added many additional features In 1992 it was ported to UNIX platform GUI feature was added in 1996, gVim appeared
July 13, 2009 Tusharadri Sarkar 2
Features of Vim
Vim has almost full compatibility with vi + extra features and enhancements like: Completion, comparison and merging of files: vimdiff An integrated help system Support for extended regular expressions Support for scripting languages (native: vimscript + others: Perl, Ruby, Python, TCL etc.) Support for plug-ins and a GUI (gVim) version Limited IDE features and mouse interaction (for both GUI and CLI versions)
July 13, 2009 Tusharadri Sarkar 3
Features of Vim
Editing capability of compressed or archived files (.gzip, .bzip2, .zip and .tar files) Editing capability of files over network protocols like SSH, FTP and HTTP Session state prevention Spell checking Splitting of windows (both horizontal & vertical) Support for Unicode + other languages Trans-session command Syntax highlighting Visual mode
July 13, 2009 Tusharadri Sarkar 4
You never need not use the mouse or arrow keys!! Powerful navigation capability with minimal use of Ctrl/Meta keys Automatic Keyword Completion Character, line and block wise editing: The Visual mode Use multiple clipboards/buffers: No right clicks!! Use macros: Record and replay multiple macros Powerful command line editing Customize Vim as you like it: Too many vim options
July 13, 2009 Tusharadri Sarkar 5
Configuring Vim
How to configure Vim & Set Vim as the default editor
Tusharadri Sarkar
Configuring Vim
Make aliases of the vim path in the .bashrc file of your login directory as follows: alias vi=/usr/local/bin/vim X alias vim=/usr/local/bin/vim X
* Note: The actual path may vary from system to system. This is the most common path.
Create the .vimrc file in the login directory with your preferred Vim options Invoke the bash shell once and you will set vim as your default editor
Tusharadri Sarkar
Configuring Vim
Tusharadri Sarkar
Configuring Vim
:set visualbell t_vb= " turn off error beep/flash :set number " show line numbers :set ignorecase " ignore case when searching :set viminfo='20,<50,s10,h :set autoindent " always set auto indenting on :set smartindent " smart indent :set expandtab " tabs are converted to spaces :set sm " show matching braces :syntax on " syntax highlighting
Tusharadri Sarkar
Configuring Vim
Each option has a short form Using short form is convenient while enabling/disabling any option for current session :set number == se nu enables line number To disable any option, just prefix no before it :se nonu disables line number If you have GUI support (gVim), you can also set the color scheme, font style, window height/width and background and foreground colors
Tusharadri Sarkar
10
Using Vim
Tusharadri Sarkar
11
Modes of Vim
There are 3 modes with distinct sets of functionalities: 1. Command Mode (For Navigation) 2. Input Mode (Everything shows up on screen)
[Enter]
Tusharadri Sarkar
13
Vim: Navigation
Keystroke h l/spacebar k/Ctrl+p J/Ctrl+n Crtl+f Ctrl+b Ctrl+d Ctrl+u M (Shift+h) H L W/w Move cursor left * Move cursor right * Move cursor up * Move cursor down * Scroll forward one page Scroll back one page Scroll down one half of a page Scroll up one half of a page Move cursor to middle of page Move cursor to top of page Move cursor to bottom of page Move cursor forward a word at a time*
July 13, 2009 Tusharadri Sarkar 14
Function
Vim: Navigation
Keystroke B/b E/e 0 (Zero) / | $ ) ( G % . (Apostrophe dot) a (Apostrophe a) Function Move cursor to bottom of page * Move cursor to end of word * Move cursor to beginning of line * Move cursor to end of line Move cursor to beginning of next sentence Move cursor to beginning of current sentence Move cursor to end of file * Move cursor to the matching bracket; Place cursor on {}[]() Move cursor to previously modified line Move cursor to line mark "a" generated by marking "ma"
Tusharadri Sarkar
15
Vim: Navigation
Keystroke A (Apostrophe A) gg ] [ +/^ Function Move cursor to line mark "a" (global between buffers) generated by marking with keystroke "mA" Move to the beginning of the file Move cursor to next lower case mark Move cursor to previous lower case mark Move cursor down/up in first column Move to the first character of first word
Functions marked with * also support motions; e.g. # 10w will take the cursor 10 words forward. 40G will take you to the 40th line from beginning
Tusharadri Sarkar
16
Action
fc / Fc ; / , (semi/coma)
The same rule of motion applies here also with actions marked *
July 13, 2009 Tusharadri Sarkar 20
Pattern Substitutions
General format of substitution :[.|$|%]s/s1/s1[switches] or :n1,n2s/s1/s2/[switches]
Function
Tusharadri Sarkar
26
Tusharadri Sarkar
28
Tusharadri Sarkar
29
Vim sets them as local to the window when using diff mode. Otherwise they are set to global values while editing other files.
July 13, 2009 Tusharadri Sarkar 30
When already in editing mode, diff mode can be invoked in 3 ways :diffsplit <filename> :diffthis :diffpatch <patchfile> To ensure these commands use a vertical split, use option :vert :vert diffpatch /tmp/diff :vert diffsplit main.c
July 13, 2009 Tusharadri Sarkar 31
Force the difference to be updated :diffupdate To jump between the differences [c:Jump backward to the previous start of a change ]c: Jump forward to the previous start of a change
Tusharadri Sarkar
32
Highlight the differences with the following groups: hl-DiffAdd Added/inserted lines hl-DiffChange Changed text hl-DiffText Changed text inside a changed line hl-DiffDelete Deleted lines
Tusharadri Sarkar
33
Diff Copying: Two commands to copy test from one buffer to another: [range] diffg[et] [buffspace]: Modify current buffer to undo differences with another buffer within range [range] diffpu[t] [buffspace]: Modify another buffer to undo differences with the current buffer within range do: Same as diffg[et] without argument or range dp: Same as diffpu[t] without argument or range
Tusharadri Sarkar
34
Ctags helps to jump to the references under cursor Add tags to a particular sets of file or entire directory: ctags R <path>; It will create a tags file Alternatively you can use: ctags *.h *.c to create tags Most frequently used commands for ctags: Ctrl+] The vim editor will jump into the tag to follow it to a new position in the file or to a new file Ctrl+t/pop The vim editor will allow the user to jump back a level
July 13, 2009 Tusharadri Sarkar 35
:set tags=./tags1,./tag2
To generate the cscope database use the command: cscope b *.cpp/(or .cc/.c) *.h or cscope b R Invoke cscope from vim: cscope find search_type serach_string
Description Find all references to a symbol Find global definition Find calls of this function Find functions that the specified function calls Find specified text string Open file Find files that "#include" the specified file
July 13, 2009 Tusharadri Sarkar 38
search_type symbol (s) global (g) calls (c) called (d) text (t) file (f) include (i)
You can manually generate a cscope file with a shell script #!/bin/bash find ./ -name "*.[ch]pp" -print > cscope.files cscope -b -q -k
July 13, 2009 Tusharadri Sarkar 39
Sorting:
Mark a block of text at the top line and bottom line of the block of text. i.e. "mt" and "mb" on two separate lines. This text block is then referenced as "'t,'b. Now :'t,'b !sort
Tusharadri Sarkar
40
Text formatting:
Use bookmark as above i.e. mt and mb. Now apply the command # :t,b !nroff
Spell checking:
Same as above. Command # :t,b !spell You can undo the changes by pressing u
Tusharadri Sarkar
41
Tusharadri Sarkar
42
gVim on Cygwin
Location of gVim in cygwin: /usr/bin/gvim.exe Also you can find vim here: /usr/bin/vim.exe In the login directory, create suitable aliases in .bashrc
alias gv=/usr/bin/gvim.exe
Create a .gvimrc file in the same directory with your preferred options set Launch the Xserver by command: startx from cygwin window. This will open the XTreminal Launch the gVim from Xterm using your alias
Tusharadri Sarkar
43
Best way would be to create one or more aliases of this in .bashrc file
July 13, 2009 Tusharadri Sarkar 44
References:
Tusharadri Sarkar
45
THANK YOU !!
And Happy Vimming
Tusharadri Sarkar
46