You are on page 1of 39

Quick Tour of gvim

Milton Yee 9/30/2010

© 2010 Altera Corporation—Confidential


Agenda
 Effective text editing
 Give an introduction to gvim.
 Frequently used commands in gvim.
 Customizations using gvimrc

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
2
Good editor saves time
 Every day we are interacting with text files.
 Creating/editing/viewing text
 good editor boosts productivity

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
3
7 Habits of EffectiveText Editing
 From gvim creator, Bram Moolenaar
 http://www.moolenaar.net/habits.html
 1.Move around quickly
 Reading, error checking, searching
 Learn the repeated action, learn new commands that perform
those action quicker, use new commands and become automatic.
 2.Don’t type it twice
 3.Fix it when it’s wrong
 Syntax coloring, () balancing, typos
 4.A file seldom comes alone
 Edit multiple buffers. Ability to jump between files.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
4
7 Habits of Effective Text Editing
 5. Let’s work together
 editor should work with other programs. It should be integrated in
other IDEs. Unix style. Work together to do bigger tasks.
 6. Text is structured
 Create macros / scripts to work this certain type of text. Example,
edit-compile-fix cycle.
 7. Make it a habit/ Sharpen the saw
 Invest time to learn a skill. Learn new commands and make them
a habit. No need to think about it.
 No need to learn every command, just the ones that helps w/
your repetitive task.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
5
What’s gvim
 a powerful text editor

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
6
Why gvim?
 Powerful engine w/ tons of features.
 I’m probably using < 1% of its features.

 Widely available – Windows, Linux, Mac OSX


(MacVim)
 Syntax highlighting for all languages (C++, Python,
Perl, Tcl, etc)
 Ferrari of editors. Fast loading and execution
 Efficiently handles large files
 Common operations easy and fast.
 Best of both worlds, commandline + GUI
© 2010 Altera Corporation—Confidential
ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
7
Screenshot

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
8
Getting Started
 ARC resource gvim (included in dev/acds) or
install your own from www.vim.org.
 gvim <file>
 use batch files/alias n / v to launch.
 In Linux my ~/.bashrc has these aliases:
 alias n=gvim
 alias v="gvim -R“
 In windows I have these batch files in d:\bin
 n.bat
 start d:\a\vim\vim73\gvim.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
 v.bat
 start d:\a\vim\vim73\gvim.exe -R %1 %2 %3 %4 %5 %6 %7 %8 %9

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
9
Two modes of gvim
 ‘i’ - Insert mode
 Press ‘a’ (append) appends from current cursor and switch to insert mode.
 Use this mode to enter text.

 ESC - Command mode


 Run commands, change/delete/substitute text, search/navigate text,
goto line.
 mouse not required to use gvim but…
 GUI Mode – works like any GUI app, menus,
toolbars, dialog boxes.
 mouse to keyboard switching has overhead. Over time, command
mode becomes 2nd nature, like driving stick shift and you avoid the GUI
tax.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
10
More ways to go into Insert Mode
 I Inserts at beginning of current line
 A inserts at end of current line
 O inserts new line after current line
 s delete current char and switch to insert mode
 S delete current line and switch to insert mode
 C delete rest of line and switch to insert mode
 cw delete word and switch to insert mode

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
11
Save edits to disk
 :w
 Write file
 :w!
 Write file ignoring read-only

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
12
Exit gvim
 :q
 :q!
 Force quit

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
13
Search
 /pattern/ search forward from current location.
 ?pattern? Search backwards
 Uses regular expressions
 n – Next match
 N – Previous match
 :set ignorecase
 :set noignorecase
 :set ignorecase smartcase
 Use case only if text to search has capitalized letters.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
14
Search Sample
 Search for /cell/. matching words are highlighted.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
15
Search
 *
 Search forward for the word under cursor.
 *?
 Search backwards for word under cursor

 No need to type the text to search.


 Avoids spelling mistakes.
 Handy if word to search is long.
 Same ‘n’, and ‘N’ to move through the matches.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
16
Language support
 %
 With cursor at { } ( ), [ or ], oscillates between begin/ end of the
pair.
 [{ goto to start of block
 ]} goto end of a block.
 gd
 Go to definition of function.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
17
Search multiple files
 :vimgrep <text to search> *.py
 What if I need to search whole directory tree
 :vimgrep <text to search> **/*.py
 :copen to open window showing the matches.
 Ctrl-W-c to close the results window.
 :cn
 Next match

 :cp
 Previous match

 Double click opens match.


© 2010 Altera Corporation—Confidential
ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
18
Vimgrep Sample

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
19
Search multiple files
 Previous vimgrep query results are in still
accessible.
 :cold
 Previous vimgrep session
 :cnew
 Next vimgrep session

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
20
Moving around
 <line number>G, G
 Goto line number
 Goto Last line.

 gg
 Goto top of file

 h, l, j,k
 left,right,up,down

 Ctrl-B, Ctrl-U, Ctrl-F, trl-D


 Page Up, Half Page Up, Page Down, Half Page Down

 Arrow keys, Regular Page Up/Page Down/Ctrl-


Home/Ctrl-end work as well
© 2010 Altera Corporation—Confidential
ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
21
Delete Text: Precision Karate chop
 x, <count>x
 Delete the character under cursor
 D
 From cursor to end of line
 dw, <count>dw
 Delete word

 df<char>
 Delete from current cursor to first occurrence of character.
 Backspace/delete key works too.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
22
Delete Text
 dd, <count>dd
 Delete line
 g/pattern/d
 Delete lines that have text matching pattern

 dd followed by ‘p’ switches order of two lines

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
23
undo
 u
 Undo the last command.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
24
Repeat operation
 ‘.’

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
25
Replace Text
 r<char>
 Replace character at cursor w/ <char>
 cw
 Chang Word. Delete word at cursor and enter insert mode to
enter new word.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
26
Editing Example
 I get an error on pintable:
 Secondary function CLKIN3N_BC direction and Differential
function LVDSIN4C_95P direction do not agree.
 Open file n *pintable.txt
 /CLKIN3N_BC/
… P59A0B IO CLKIN3N_BC LVDSIN4C_95P …
….
….P59A1B IO CLKIN3P_BC LVDSIN4C_95N …
 Position cursor to N
 rP
… P59A0B IO CLKIN3P_BC LVDSIN4C_95P

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
27
Editing Example
 Cursor is on CLKIN3P_BC.
 * Finds all occurrences of current word.
… P59A0B IO CLKIN3P_BC LVDSIN4C_95P …
….
….P59A1B IO CLKIN3P_BC LVDSIN4C_95N …
 Cursor moves to 2nd occurrence. Move cursor to
letter P
 rN
… P59A0B IO CLKIN3P_BC LVDSIN4C_95P …
….
….P59A1B IO CLKIN3N_BC LVDSIN4C_95N …

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
28
Search and Replace
 :s /pattern/replacement/<options>
 Within current line only
 Similar syntax as sed unix utility.
 Uses regular expressions.
 Use /g to do more than occurrence.
 Options can be ‘g’ to replace more than one occurrence, ‘c’ to confirm
each replacement.
 :%s /pattern/replacement/<options>
 Operate over whole file.
 Use trailing /c option to confirm.

 <range> :s /pattern/replacement/<options>
 <range> can be x,y to operate between lines x and y. If y is not
specified, it means to current positions.. $ for end of file.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
29
Examples
 :%s/^M//gc
 Remove extra carriage return. ^M is entered with CTRL-Q M in
Windows/ CTRL-V M in Linux.
 :%s /\(\w\+\)/"\1\"/g
 Quote every word. Every special character needed a \. The
parenthesis capture the word and \1 is reference to the word.
 :%s /$/,/g
 Append character to end of line
 :%s /,\s\(.\+\).atom.xml/, \1, \1, create/g
 Repeat the filename w/o the atom.xml extension.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
30
Make
 :make
 Runs make
 :copen
 View results, use same navigation keys as :vimgrep.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
31
Custom make command
 :set makeprg=<your command>
 Example:
 :set makeprg=d:/depot/icd_tools/pinbal/main/t1.bat\
f5_125_z1441.csv

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
32
Edit additional files
 :e <file>, :tabnew <file>
 :cd <directory>, :pwd
 :buffers
 List the files opened
 :bf, :bl, :b<number>, :bn, :bp
 Switch to first, last, specific buffer
 Next buffer, Previous buffer

 :bd, :bd<number>
 Close buffer
 Close specific buffer

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
33
Copy and Paste
 mc
 Start marking text and store in register c. Can be any letter.
 Move end of block to copy
 y’c
 Yanked
 p
 Traditional Windows style Shift arrow, Shift-Paste
works too.

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
34
Copy and Paste
 yy’
 Copy line under cursor
 Move cursor to new location
 p
 Paste line

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
35
Customizations
 Customization
 Windows : _gvimrc goes into C:\Documents and Settings\<your
login>
 Unix : ~/.gvimrc
 Color schems:
 Windows : C:\Documents and Settings\<your login> \vimfiles\
 Unix : ~/.vim/color/

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
36
Sample .gvimrc /_gvimrc
Complete file in ~myee/.gvimrc
" Set Gvim in a more user friendly way instead of vi mode.
set nocompatible

" Set keymovement to be Windows like.


source $VIMRUNTIME/mswin.vim

" Set Mouse selection to be like Windows.


behave mswin

set mousehide " Hide the mouse when typing text

" Map F4 to search word at cursor for all files in cur directory.
" do not jump to first match and open results window.
map <F4> :execute "vimgrep /" . expand("<cword>") . "/j *.*" <Bar> cw<CR>
" Run make but don't jump to 1st error and then call copen to show output.
map <F2> :execute "make!" <Bar> copen <CR>

" Only do this for Vim version 5.0 and later.


if version >= 500

" Switch on search pattern highlighting.


set hlsearch
" Switch on default colorscheme.
colorscheme myeekoehler
endif

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
37
Additional Python support through plugin
 Python uses indentation for block definition.
 Install python_match.vim plugin
http://www.vim.org/scripts/script.php?script_id=386
 To use [% and ]% to go to begin/end block, extends % to cycle
through if/elif/else, for/continue/break
 Copy python_match.vim to
 Window’s %HOMEDIR%/vimfiles\ftplugin
 Linux’s ~/.vim/ftplugin
 Add line to Windows’s %HOMEDIR%/_gvimrc
 Add line to Linux~/.gvimrc
 :filetype plugin on

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
38
Resources
 http://www.vim.org/
 http://sw-wiki.altera.com/twiki/bin/view/Main/Vim
 http://vimdoc.sourceforge.net/
 Free online books:
 http://www.truth.sk/vim/vimbook-OPL.pdf
 http://www.swaroopch.com/notes/Vim
 7 Habits of effective text editing:
 http://www.moolenaar.net/habits.html

© 2010 Altera Corporation—Confidential


ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS & STRATIX are Reg. U.S. Pat. & Tm. Off.
and Altera marks in and outside the U.S.
39

You might also like