45
Vim Hacks 林佑安 (Cornelius) [email protected] http://c9s.blogspot.com / c9s@(plurk|twitter).com OSSF 自由軟體鑄造廠

Vim Hacks (OSSF)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Vim Hacks (OSSF)

Vim Hacks林佑安 (Cornelius)

[email protected]://c9s.blogspot.com/c9s@(plurk|twitter).com

OSSF 自由軟體鑄造廠

Page 2: Vim Hacks (OSSF)

Demo

Page 3: Vim Hacks (OSSF)

To get Vim installed

• on Debian/Ubuntu: $ sudo apt-get install vim vim-gnome

• on MacOS: $ sudo port install vim vim-app

Page 4: Vim Hacks (OSSF)

To compile your Vim

• some ./configure options:

• --with-features=tiny,normal,big,huge

• --enable-perlinterp

• --enable-rubyinterp

• --enable-pythoninterp

• --enable-multibyte

• --enable-gui , --enable-cscope etc ...

Page 5: Vim Hacks (OSSF)

To compile your Vim

$ ./configure --prefix=/opt/local \ --enable-rubyinterp \ --enable-perlinterp \ --with-features=huge \ --enable-multibyte \ --enable-cscope \ --enable-mzschemeinterp

$ make $ make install

Page 6: Vim Hacks (OSSF)

Basic Editing

Command Mode (start with ‘:’)Insert Mode (i,o,a)

Visual Mode (v,V,c-v)

ESC

Normal Mode

Page 7: Vim Hacks (OSSF)

Basic Editing

1. $ vim file2. type h,j,k,l to move around.3. type i to insert some text4. <ESC> to go back normal mode5. :w to save file.6. repeat 3-5 step7. :wq

Page 8: Vim Hacks (OSSF)

Edit your .vimrc file

$ vim ~/.vimrc

“iset nu<ESC>”

“:wq”

$ vim ~/.vimrc

Page 9: Vim Hacks (OSSF)

Basic Options

set nocompatiblesyntax onfiletype onfiletype plugin onfiletype indent onset nuset wrapset shiftwidth=4set tabstop=4

Page 10: Vim Hacks (OSSF)

Basic GUI Options (.gvimrc)

set langmenu=en_US.utf-8language mes en_US.UTF-8

if has('gui_mac') && has(‘gui_running’) set gfn=Monaco:h12else "set gfn=Dejavu\ Sans\ Mono\ 9 "set gfn=Andale\ Mono:h14 set gfn=Bitstream\ Vera\ Sans\ Mono\ 10 endifset guioptions+=c “use console dialog insteadset guioptions-=e “no gui tabset guioptions-=T “no Toolbarset guioptions-=m “no menuset guioptions-=r “no right-hand scrollbar

" downlaod a colorscheme you like to ~/.vim/colors/" colors billw

Page 11: Vim Hacks (OSSF)

Insert Mode

• i I

• a A

• o O

Page 12: Vim Hacks (OSSF)

Visual Mode

• v

• V

• Ctrl-v

Page 13: Vim Hacks (OSSF)

Basic MotionC-d (half-page down)C-u (half-page up)C-f (full-page down)C-b (full-page up)gg (goto the first line)G (goto the last line)30gg:10 10%

Page 14: Vim Hacks (OSSF)

Basic Motionb,w,eB,W,E

f[x] F[x] ; ,

( ) sentence{ } paragraph[ ] section

Page 15: Vim Hacks (OSSF)

Buffer

Page 16: Vim Hacks (OSSF)

$ vim file1 file2 file3

Page 17: Vim Hacks (OSSF)

:bufdo tab split

Page 18: Vim Hacks (OSSF)

:bufdo %s!pattern!string!g

Page 19: Vim Hacks (OSSF)

:bufdo mkview

Page 20: Vim Hacks (OSSF)

:[n]buffer:bn:bp:sb

:buffers:buffers!

Page 21: Vim Hacks (OSSF)

Window

Page 22: Vim Hacks (OSSF)

Ctrl-W vCtrl-W sCtrl-W hCtrl-W jCtrl-W kCtrl-W lCtrl-W xCtrl-W w

Ctrl-W _Ctrl-W |Ctrl-W q

:wincmd w:wincmd v:wincmd s

Page 23: Vim Hacks (OSSF)

nmap <silent> <D--> :resize -5<CR> nmap <silent> <D-=> :resize +5<CR> nmap <silent> <D-]> :vertical resize +5<CR> nmap <silent> <D-[> :vertical resize -5<CR>

For MacOS

For Unix-like set winaltkeys=yes

nmap <silent> <M--> :resize -5<CR> nmap <silent> <M-=> :resize +5<CR> nmap <silent> <M-]> :vertical resize +5<CR> nmap <silent> <M-[> :vertical resize -5<CR>

Page 24: Vim Hacks (OSSF)

Tabpage

Page 25: Vim Hacks (OSSF)

:[n]tabdo:tabn:tabp:sb:tabs:tabs!

:tabnew:tabclose:tabfirst:tabfind:tablast

Page 26: Vim Hacks (OSSF)

nmap tn :tabnew<CR>nmap tl gtnmap th gTnmap te :tabedit

“ split current buffer to tabpagennoremap <silent> ty :tab split<CR>nnoremap <silent> td :exec 'tabedit '.expand('%')<CR>

“ close tab ( equal to <C-w>q )nnoremap <silent> tq :tabclose<CR>

“ open new tabnnoremap <silent> tn :tabnew<CR>

Page 27: Vim Hacks (OSSF)

“ open help in new pagennoremap <silent> th :tab help<CR>

“ move tab nnoremap <silent> tmh \ :exec ':tabmove ' . ( tabpagenr()-2 )<CR>nnoremap <silent> tml \ :exec ':tabmove ' . tabpagenr()<CR>

Page 28: Vim Hacks (OSSF)

Syntaxsyntax.txt

Page 29: Vim Hacks (OSSF)

Macro*macro*

Page 30: Vim Hacks (OSSF)

Mappingmap.txt

Page 31: Vim Hacks (OSSF)

Autocommandautocmd.txt

Page 32: Vim Hacks (OSSF)

Cases

Page 33: Vim Hacks (OSSF)

Case 1: Copy & Paste

• copy text between terminal vim and gui

• Solution: fakeclip plugin

Page 34: Vim Hacks (OSSF)

Case 2: Browsing File

• Solution: nerd tree plugin

• \e to open nerd tree window

Page 35: Vim Hacks (OSSF)

Case 3: Switching Buffer

• Solution: bufexplorer.zip

• \be to open buffer explorer

Page 36: Vim Hacks (OSSF)

Case 4: reformating code

• Problem: Coding Style Problem

• Solution: ‘equalprg’ option

• setlocal equalprg=/path/to/your/format_prg

Page 37: Vim Hacks (OSSF)

Case 5: code snippets

• Problem: 重複的程式碼片段

• Solution: snipmate.vim

Page 38: Vim Hacks (OSSF)

Case 6: completion

• Problem: 懶得呼叫 C-x C-n 做 completion

• Solution: autocomplpop.vim

Page 39: Vim Hacks (OSSF)

VimL

Page 40: Vim Hacks (OSSF)

• variable

• function

• condition

• looping

• vim built-in functions

• command

Page 41: Vim Hacks (OSSF)

• vim runtime path

• $VIM/

• $VIMRUNTIME/

Page 42: Vim Hacks (OSSF)

See Also

• Vim.org

• Vim Tips wiki

• :tab help

Page 43: Vim Hacks (OSSF)

Utilities

• Vimana

• VIM::Packager

Page 44: Vim Hacks (OSSF)

Selected Help Section

• ‘C-editing’

• ‘completion’

• ‘tips.txt’

• ‘autocmd’

• ‘eval.txt’

Page 45: Vim Hacks (OSSF)

vim script authorson github.com

• http://github.com/c9s

• http://github.com/kana

• http://github.com/mattn

• http://github.com/tpope

• etc...