perl.hacks.on.vim perlchina

225
Perl Hacks on Vim 林佑安 Lin You-An c9s / Cornelius pause id: CORNELIUS

Upload: yo-an-lin

Post on 17-May-2015

4.028 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Perl.Hacks.On.Vim Perlchina

Perl Hacks on Vim

林佑安Lin You-An

c9s / Cornelius

pause id: CORNELIUS

Page 2: Perl.Hacks.On.Vim Perlchina

林佑安Lin You-An

Page 3: Perl.Hacks.On.Vim Perlchina

VIM & Perl

Page 4: Perl.Hacks.On.Vim Perlchina

how can vim improve perl coding productivity

vim 如何改善 coding 生产力 ?

Page 5: Perl.Hacks.On.Vim Perlchina

The worst way to edit最糟糕的编辑方式

Page 6: Perl.Hacks.On.Vim Perlchina

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 7: Perl.Hacks.On.Vim Perlchina

Oops!

lost “a”

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 8: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 9: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 10: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 11: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 12: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 13: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 14: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 15: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 16: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 17: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 18: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 19: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 20: Perl.Hacks.On.Vim Perlchina

x N

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 21: Perl.Hacks.On.Vim Perlchina

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 22: Perl.Hacks.On.Vim Perlchina

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 23: Perl.Hacks.On.Vim Perlchina

if this costs 4 sec

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 24: Perl.Hacks.On.Vim Perlchina

if x 50 times this kind of situation per day

if this costs 4 sec

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 25: Perl.Hacks.On.Vim Perlchina

if this costs 4 sec

if x 50 times this kind of situation per day

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 26: Perl.Hacks.On.Vim Perlchina

if this costs 4 sec

and you work for more than 300 day per year

= 16.6 hours

if x 50 times this kind of situation per day

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 27: Perl.Hacks.On.Vim Perlchina

x 50 times this kind of situation per day ?

if this costs 4 sec

and you work for more than 300 day per year

= 16.6 hours

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 28: Perl.Hacks.On.Vim Perlchina

x 50 times this kind of situation per day ?

if this costs 4 sec

and you work for more than 300 day per year

= 16.6 hours

Awful

#!/usr/bin/env perlmy $hppiness = PerlChina->join();

Page 29: Perl.Hacks.On.Vim Perlchina

What can you do in16.6 hours

Page 30: Perl.Hacks.On.Vim Perlchina

讀幾本書

Page 31: Perl.Hacks.On.Vim Perlchina

陪家人

Page 32: Perl.Hacks.On.Vim Perlchina

陪女朋友

Page 33: Perl.Hacks.On.Vim Perlchina

or more hacking

Page 34: Perl.Hacks.On.Vim Perlchina

sleep...

Page 35: Perl.Hacks.On.Vim Perlchina

时间就是金钱

Page 36: Perl.Hacks.On.Vim Perlchina

The VIM way...

Page 37: Perl.Hacks.On.Vim Perlchina

VIM:

F h

#!/usr/bin/env perlmy $hppiness = perlchina->join();

Page 38: Perl.Hacks.On.Vim Perlchina

VIM:

a append

#!/usr/bin/env perlmy $hppiness = perlchina->join();

Page 39: Perl.Hacks.On.Vim Perlchina

VIM:

f P

#!/usr/bin/env perlmy $hppiness = perlchina->join();

Page 40: Perl.Hacks.On.Vim Perlchina

VIM:

~

#!/usr/bin/env perlmy $hppiness = Perlchina->join();

Page 41: Perl.Hacks.On.Vim Perlchina

VIM:

~

#!/usr/bin/env perlmy $hppiness = PErlchina->join();

Page 42: Perl.Hacks.On.Vim Perlchina

VIM:

~

#!/usr/bin/env perlmy $hppiness = PERlchina->join();

Page 43: Perl.Hacks.On.Vim Perlchina

VIM:

~

#!/usr/bin/env perlmy $hppiness = PERLchina->join();

Page 44: Perl.Hacks.On.Vim Perlchina

So What is VIM ?

Page 45: Perl.Hacks.On.Vim Perlchina

VIM 不是 IDE

Page 46: Perl.Hacks.On.Vim Perlchina

VIM is an Editor

Page 47: Perl.Hacks.On.Vim Perlchina

VI Improved

Page 48: Perl.Hacks.On.Vim Perlchina

Move More Efficiently.更有效率的移动

Page 49: Perl.Hacks.On.Vim Perlchina

學習曲線

Page 50: Perl.Hacks.On.Vim Perlchina
Page 51: Perl.Hacks.On.Vim Perlchina

Features

Page 52: Perl.Hacks.On.Vim Perlchina

1. Mode

Page 53: Perl.Hacks.On.Vim Perlchina

More Than 4 Edit Mode

Page 54: Perl.Hacks.On.Vim Perlchina

INSERTNORMALVISUALSELECT

... et cetera

Page 55: Perl.Hacks.On.Vim Perlchina

[Ii]NSERTNORMAL[Vv]ISUAL

ESC

Page 56: Perl.Hacks.On.Vim Perlchina

1.1 Normal Mode

Page 57: Perl.Hacks.On.Vim Perlchina

Motion

Page 58: Perl.Hacks.On.Vim Perlchina
Page 59: Perl.Hacks.On.Vim Perlchina
Page 60: Perl.Hacks.On.Vim Perlchina

HJKL←↓↑→

Page 61: Perl.Hacks.On.Vim Perlchina

h , j , k , lH , M , Lw , e , bf[x] , t[x][{ , ]} , %( , ) , { , }

Page 62: Perl.Hacks.On.Vim Perlchina

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 63: Perl.Hacks.On.Vim Perlchina

Cursor

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 64: Perl.Hacks.On.Vim Perlchina

[{

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 65: Perl.Hacks.On.Vim Perlchina

f@

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 66: Perl.Hacks.On.Vim Perlchina

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

$

Page 67: Perl.Hacks.On.Vim Perlchina

^

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 68: Perl.Hacks.On.Vim Perlchina

H 畫面最上方# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 69: Perl.Hacks.On.Vim Perlchina

M畫面中間行

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 70: Perl.Hacks.On.Vim Perlchina

L 畫面最下方

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 71: Perl.Hacks.On.Vim Perlchina

修改至行尾

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 72: Perl.Hacks.On.Vim Perlchina

C並進入 Insert mode

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep}

sub func1 {

}

Page 73: Perl.Hacks.On.Vim Perlchina

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 74: Perl.Hacks.On.Vim Perlchina

%

# comments ...

foreach my $foo ( @bar ) {

# do something

my @outs = grep /pattern/ , @list; }

sub func1 {

}

Page 75: Perl.Hacks.On.Vim Perlchina

:h motion.txt

Page 76: Perl.Hacks.On.Vim Perlchina

1.2 Insert Mode

Page 77: Perl.Hacks.On.Vim Perlchina

Editing text

Page 78: Perl.Hacks.On.Vim Perlchina

Insert Mode

• i : Insert text before the cursor

Page 79: Perl.Hacks.On.Vim Perlchina

• i : Insert text before the cursor

• I : Insert text before the first non-blank in the line

Insert Mode

Page 80: Perl.Hacks.On.Vim Perlchina

• i : Insert text before the cursor

• I : Insert text before the first non-blank in the line

• a : Append text after the cursor

Insert Mode

Page 81: Perl.Hacks.On.Vim Perlchina

• i : Insert text before the cursor

• I : Insert text before the first non-blank in the line

• a : Append text after the cursor

• A : Append text at the end of the line

Insert Mode

Page 82: Perl.Hacks.On.Vim Perlchina

1.3 Visual Mode

Page 83: Perl.Hacks.On.Vim Perlchina

Select region

Page 84: Perl.Hacks.On.Vim Perlchina

Visual Mode

• v : start Visual mode per character.

Page 85: Perl.Hacks.On.Vim Perlchina

Visual Mode

• v : start Visual mode per character.

• V : start Visual mode linewise.

Page 86: Perl.Hacks.On.Vim Perlchina

Visual Mode

• v : start Visual mode per character.

• V : start Visual mode linewise.

• Ctrl-v : start Visual mode blockwise.

Page 87: Perl.Hacks.On.Vim Perlchina

Visual Mode

• v : start Visual mode per character.

• V : start Visual mode linewise.

• Ctrl-v : start Visual mode blockwise.

operator 如 y (yank) , d (delete) , c (change) ... etc

Page 88: Perl.Hacks.On.Vim Perlchina

2. Syntax Highlight Support

Page 89: Perl.Hacks.On.Vim Perlchina

/opt/local/share/vim/vim72 $ ls -1 syntax/ | wc -l 520

more than 500 syntax files

Page 90: Perl.Hacks.On.Vim Perlchina

可定制Syntax

Page 91: Perl.Hacks.On.Vim Perlchina

Perl code needs syntax

Page 92: Perl.Hacks.On.Vim Perlchina

but we design more syntax in Perl.

但是我們也用 Perl 創建更多的語法

Page 93: Perl.Hacks.On.Vim Perlchina

Modules need syntax too所以模組也需要自己的

語法標記

Page 94: Perl.Hacks.On.Vim Perlchina

Template::Declare template simple => sub { html { head {} body { p {'Hello, world wide web!'} } } };

# ... templates

Page 95: Perl.Hacks.On.Vim Perlchina
Page 96: Perl.Hacks.On.Vim Perlchina
Page 97: Perl.Hacks.On.Vim Perlchina

package MyView; use Markapl;

tempalte '/a/page.html' => sub { h1("#title") { "Hi" }; p(".first") { "In the begining, lorem ipsum...." }; p(style => "color: red;") { "But...." }; }

Page 98: Perl.Hacks.On.Vim Perlchina

Jifty::DBI::Schema

package TestApp::Model::Phone; use Jifty::DBI::Schema; use Jifty::DBI::Record schema { column user => references TestApp::Model::User by 'id', is mandatory; column type => ...; column value => ...; };

Page 99: Perl.Hacks.On.Vim Perlchina

~/.vim/syntax/[filetype].vim

Page 100: Perl.Hacks.On.Vim Perlchina

~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]

Page 101: Perl.Hacks.On.Vim Perlchina

~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]syn region [id] start=[re] end=[re]

Page 102: Perl.Hacks.On.Vim Perlchina

~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]syn region [id] start=[re] end=[re]syn keyword [id] [keyword] ...

Page 103: Perl.Hacks.On.Vim Perlchina

~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]syn region [id] start=[re] end=[re]syn keyword [id] [keyword] ...

hi [id] guibg=[color] ctermfg=[color]

Page 104: Perl.Hacks.On.Vim Perlchina

~/.vim/syntax/perl/*.vim~/.vim/syntax/perl/jifty.vim~/.vim/syntax/perl/template-declare.vim~/.vim/syntax/perl/markapl.vim

Page 105: Perl.Hacks.On.Vim Perlchina

" Fold Jifty Templatessyn region jifty_td_fold start=+^\(private\s\+\)\=template\s+ end=+^};+ transparent fold keepend

" Fold Jifty Template Delcare Tagssyn region jifty_tag_fold start="^\z(\s*\)\<\(div\|table\|row\|form\|script\|cell\|row\|th\|tfoot\)\>\s*{" end="^\z1};\=" fold keepend

Page 106: Perl.Hacks.On.Vim Perlchina

syn region jfscolumn start=+^\s*\(param\|column\)\>+ end=+;+ contained contains=jfspropertybe,jfsproperty,jfscol,jfscolname, \ perlComment, perlString, perlFunction, perlFloat, perlNumber, perlSpecialString, perlStringUnexpanded \ fold

Fold Jifty::DBI schema columns and action params

Page 107: Perl.Hacks.On.Vim Perlchina

:help syntax.txt

Page 108: Perl.Hacks.On.Vim Perlchina

4. key

mapping

Page 109: Perl.Hacks.On.Vim Perlchina

:map:nmap:vmap:imap:smap :xmap... more

Page 110: Perl.Hacks.On.Vim Perlchina

:map (all)

:nmap (normal mode)

:vmap (visual mode)

:imap (insert mode)

:smap (select mode)

:xmap (visual , select mode)... more

Page 111: Perl.Hacks.On.Vim Perlchina

:nmap <C-c><C-c> :!perl -Wc %

Ctrl C Ctrl C

let perl check current perl script syntax

Page 112: Perl.Hacks.On.Vim Perlchina

nmap <tab> v>nmap <s-tab> v<

vmap <tab> >gvvmap <s-tab> <gv

use Tab and Shift-Tab to indent region ,instead of “>” and “<“

Page 113: Perl.Hacks.On.Vim Perlchina

imap <F2> <C-R>=strftime("%c")<CR>imap <F2> <C-R>=system(‘perl -MDateTime -e DateTime->now’)<CR>

to insert timestamp

Page 114: Perl.Hacks.On.Vim Perlchina

cmap <c-a> <home>cmap <c-e> <end>cnoremap <c-b> <left>cnoremap <c-d> <del>cnoremap <c-f> <right>cnoremap <c-n> <down>cnoremap <c-p> <up>

cnoremap <esc><c-b> <s-left>cnoremap <esc><c-f> <s-right>

support bash-like key mapping

Page 115: Perl.Hacks.On.Vim Perlchina

:h map.txt

Page 116: Perl.Hacks.On.Vim Perlchina

5.Text Object

Page 117: Perl.Hacks.On.Vim Perlchina

Text Object

• word

• string

• paragraph

• block

⇛ action(yank,delete,change ...etc)

Page 118: Perl.Hacks.On.Vim Perlchina

v | c | d i | a { | [ | ( | “ | ‘

visualchangedelete

Inner ObjectAn Object

Region{ }[ ]( )“ “‘ ‘

Operator Mapping

Operator

Page 119: Perl.Hacks.On.Vim Perlchina

va{

Page 120: Perl.Hacks.On.Vim Perlchina
Page 121: Perl.Hacks.On.Vim Perlchina

sub( blah , blah )ci(

sub( )

sub( new_args )

Page 122: Perl.Hacks.On.Vim Perlchina

“Hello World”di”

“”

“Hello New World”

Page 123: Perl.Hacks.On.Vim Perlchina

Vim7.1 Comes withtag block region

Page 124: Perl.Hacks.On.Vim Perlchina

<aaa> Hola </aaa>

vit

<aaa> Hola </aaa>

Visual Select

Page 125: Perl.Hacks.On.Vim Perlchina

"dl" delete character (alias: "x") |dl| "diw" delete inner word *diw* "daw" delete a word *daw* "diW" delete inner WORD (see |WORD|) *diW* "daW" delete a WORD (see |WORD|) *daW* "dd" delete one line |dd| "dis" delete inner sentence *dis* "das" delete a sentence *das* "dib" delete inner '(' ')' block *dib* "dab" delete a '(' ')' block *dab* "dip" delete inner paragraph *dip* "dap" delete a paragraph *dap* "diB" delete inner '{' '}' block *diB* "daB" delete a '{' '}' block

Alias

Page 126: Perl.Hacks.On.Vim Perlchina

8.FOLDS

Page 127: Perl.Hacks.On.Vim Perlchina

FOLD IS

Page 128: Perl.Hacks.On.Vim Perlchina
Page 129: Perl.Hacks.On.Vim Perlchina

Fold Methods

Page 130: Perl.Hacks.On.Vim Perlchina

Syntax Fold

Fold Methods

Page 131: Perl.Hacks.On.Vim Perlchina

Syntax Fold

Fold Methods

:set foldmethod=syntax

set fold method as syntax , check out more options in:$VIMRUNTIME/syntax/*.vim

Page 132: Perl.Hacks.On.Vim Perlchina

Perl built-in syntax/opt/local/share/vim/vim72/syntax/perl.vim

if exists("perl_want_scope_in_variables")“ .....if exists("perl_extended_vars")“ ..... if exists("perl_fold")“ ..... you can enable those features in

your .vimrc

let perl_fold = 1let perl_extended_vars = 1“ .... etc

Page 133: Perl.Hacks.On.Vim Perlchina

let perl_include_pod = 1let perl_extended_vars = 1 let perl_want_scope_in_variables = 1let perl_fold = 1let perl_fold_blocks = 1

for complex things like @{${"foo"}}.

for something like $pack::var1

Page 134: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker Fold

Fold Methods

Page 135: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker Fold

Fold Methods

:set foldmethod=marker

fold region by markers , the default marker is “{{{“ , “}}}”

Page 136: Perl.Hacks.On.Vim Perlchina

# fold this {{{sub do_something {

# a lot of work ...

}# }}}

Page 137: Perl.Hacks.On.Vim Perlchina

# fold this {{{sub do_something {

# a lot of work ... # foldlevel 2 {{{2 # foldlevel 4 {{{4

# }}}4 # foldlevel here is 3 # }}}2

}# }}}

Page 138: Perl.Hacks.On.Vim Perlchina
Page 139: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker FoldIndent Fold

Fold Methods

Page 140: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker FoldIndent Fold

Fold Methods

:set foldmethod=indent

use indent to fold

Page 141: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker FoldIndent FoldManual Fold

Fold Methods

Page 142: Perl.Hacks.On.Vim Perlchina

:set foldmethod=manual

create folds manually

Page 143: Perl.Hacks.On.Vim Perlchina

利用 autocmd 加上 mkview , loadview 來儲存手動建立的折疊區塊,儲存的折疊會被放在 ~/.vim/view/ 裡頭。

autocmd BufWinLeave *.* silent mkviewautocmd BufWinEnter *.* silent loadview

Page 144: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker FoldIndent FoldManual FoldExpr Fold (Custom Fold Function)

Fold Methods

Page 145: Perl.Hacks.On.Vim Perlchina

:set foldexpr=MyFoldLevel(v:lnum)

customized fold function

Page 146: Perl.Hacks.On.Vim Perlchina

Syntax FoldMarker FoldIndent FoldManual FoldExpr Fold (Custom Fold Function)

Diff Fold

Fold Methods

Page 147: Perl.Hacks.On.Vim Perlchina

zazA

zmzM

zrzR

zj , zk

[z , ]z

*za*za When on a closed fold: open it. When folds are nested, you may have to use "za" several times. When a count is given, that many closed folds are opened. When on an open fold: close it and set 'foldenable'. This will only close one level, since using "za" again will open the fold. When a count is given that many folds will be closed (that's not the same as repeating "za" that many times).

*zA* zA When on a closed fold: open it recursively. When on an open fold: close it recursively and set

zm Fold more: Subtract one from 'foldlevel'. If 'foldlevel' was already zero nothing happens. 'foldenable' will be set. *zM*zM Close all folds: set 'foldlevel' to 0. 'foldenable' will be set.

Page 148: Perl.Hacks.On.Vim Perlchina

:h folding

Page 149: Perl.Hacks.On.Vim Perlchina

10. QuickFix

Page 150: Perl.Hacks.On.Vim Perlchina

:grep

Page 151: Perl.Hacks.On.Vim Perlchina

:grep [pattern] [filepath]

Page 152: Perl.Hacks.On.Vim Perlchina

Result ⇛ QuickFix Window

Page 153: Perl.Hacks.On.Vim Perlchina

QuickFix Window

Page 154: Perl.Hacks.On.Vim Perlchina

:set grepprg=/path/to/grep

Page 155: Perl.Hacks.On.Vim Perlchina

:copen

Page 156: Perl.Hacks.On.Vim Perlchina

:cclose

Page 157: Perl.Hacks.On.Vim Perlchina

:cnext

Page 158: Perl.Hacks.On.Vim Perlchina

:cprevious

Page 159: Perl.Hacks.On.Vim Perlchina

com! -bang -nargs=? QFix cal QFixToggle(<bang>0)fu! QFixToggle(forced) if exists("g:qfix_win") && a:forced == 0 cclose unlet g:qfix_win else copen 10 let g:qfix_win = bufnr("$") enendf nn <leader>q :QFix<cr>

QuickFix Window Toggle

nmap to “\q” , the default <leader> key is “\”

Page 160: Perl.Hacks.On.Vim Perlchina

9.Helpful Settings

Page 161: Perl.Hacks.On.Vim Perlchina

Should we re-indent codemanually ?

Page 162: Perl.Hacks.On.Vim Perlchina

PerlTidy++

Page 163: Perl.Hacks.On.Vim Perlchina

:set equalprg=perltidy

Page 164: Perl.Hacks.On.Vim Perlchina

:set equalprg=perltidy

then press “=” , it will pass to perltidy

to re-format a region , set perltidy as your reformater

Page 165: Perl.Hacks.On.Vim Perlchina

autocmd Filetype perl :set equalprg=perltidy

when filetype is perl use autocmd to set equalprg

Page 166: Perl.Hacks.On.Vim Perlchina

to reformat SQL , we have SQL::Beautify module

Page 167: Perl.Hacks.On.Vim Perlchina

SQL::Beautify$ cat bin/sql-beautify.pl #!/usr/bin/env perluse warnings;use strict;use SQL::Beautify;local $/;my $sql = SQL::Beautify->new( query => <STDIN> , spaces => 4 , break => "\n" );print $sql->beautify;

autocmd Filetype sql :set equalprg=sql-beautify.pl

command! -range SQLF :'<,'>!/Users/c9s/bin/sql-beautify.pl

Page 168: Perl.Hacks.On.Vim Perlchina

Javascript::Beautifier

autocmd Filetype javascript :set equalprg=js-beautify.pl

command! -range JSF :'<,'>!/Users/c9s/bin/js_beautify.pl

[localhost]$ cat bin/js_beautifier #!/usr/bin/env perluse JavaScript::Beautifier qw/js_beautify/;local $/;$js_source_code = <STDIN>;my $pretty_js = js_beautify( $js_source_code, { indent_size => 2, indent_character => ' ',} );print $pretty_js;

Page 169: Perl.Hacks.On.Vim Perlchina

HTML Entities

command! -range Entities :'<,'>!/Users/c9s/bin/html-entities.pl

$ cat bin/html-entities.pl use HTML::Entities;for ( <STDIN> ) { print encode_entities( $_ ) . "\n";}

" <a href="">YAPC ASIA</a>

&quot; &lt;a href=&quot;&quot;&gt;YAPC ASIA&lt;/a&gt;

Page 170: Perl.Hacks.On.Vim Perlchina

Morse Encoder

use Convert::Morse qw(as_ascii as_morse is_morsable); print as_morse( $_ ) . “\n” for ( <STDIN> );

command! -range Morse :'<,'>!/Users/c9s/bin/morse-encode.pl

sub handler_start { my ( $kernel, $heap, $session ) = @_[ KERNEL, HEAP, SESSION ]; print "Session ", $session->ID, " has started.\n"; $heap->{count} = 0; $kernel->yield('increment');}

... ..- -... .... .- -. -.. .-.. . .-. ..--.- ... - .- .-. - { -- -.-- -.--. $-.- . .-. -. . .-.. --..-- $.... . .- .--. --..-- $... . ... ... .. --- -. -.--.- -...- .--.-. ..--.- [ -.- .--. .-. .. -. - .-..-. ... . ... ... .. --- -. .-..-. --..-- $... . ... ... .. --- -. -....- >.. -.. --..-- .-..-. .... .- . $.... . .- .--. -....- >{-.-. --- ..- -. - } -...- ----- -.-.- $-.- . .-. -. . .-.. -....- >-.-- .. . .-.. -.. -.--. .----. .. -. -.-. .-. . -- . -. - .----. -.--.- -.-.-}

Page 171: Perl.Hacks.On.Vim Perlchina

source codetraverse

Page 172: Perl.Hacks.On.Vim Perlchina

use Data::Dumper;Your::App->do_something();

we need to find Data/Dumper.pm in @INCthen type something like

$ vim /opt/local/..../site_perl/Data/Dumper.pm

Case:

Page 173: Perl.Hacks.On.Vim Perlchina

fu! GetCursorModuleName() let cw = substitute( expand("<cWORD>") , '.\{-}\(\(\w\+\)\(::\w\+\)*\).*$' , '\1' , '' ) return cwendfunction

fu! TranslateModuleName(n) return substitute( a:n , '::' , '/' , 'g' ) . '.pm'endf

fu! GetPerlLibPaths() let out = system('perl -e ''print join "\n",@INC''') let paths = split( out , "\n" ) return pathsendf

Page 174: Perl.Hacks.On.Vim Perlchina

fu! FindModuleFileInPaths() let paths = GetPerlLibPaths() let fname = TranslateModuleName( GetCursorModuleName() ) let methodname = GetMethodName()

if TabGotoFile( 'lib/' . fname , methodname ) return endif

for p in paths let fullpath = p . '/' . fname if TabGotoFile( fullpath , methodname ) break endif endforendfunction

nmap <leader>fm :call FindModuleFileInPaths()<CR>

type  “\fm”  rather  than  type  $  perldoc  -­‐m  Module::Name

Page 175: Perl.Hacks.On.Vim Perlchina

nmap <C-x><C-i> :call InstallCPANModule()<CR>function! InstallCPANModule() let l = getline('.') let cw = substitute( expand('<cWORD>') , ";$" , "" , "g" ) let cw = substitute( cw , "['\"]" , "" , "g" ) echo "Installing CPAN Module: " . cw . "\n" silent exec "!cpanp i " . cw . " >& /dev/null" echo "Done\n"endfunction

Install CPAN Module from <cWORD>

Page 176: Perl.Hacks.On.Vim Perlchina

au bufnewfile *.pl 0r ~/.vim/skeleton/template.plau bufnewfile *.pod 0r ~/.vim/skeleton/template.podau bufnewfile *.pm 0r ~/.vim/skeleton/template.pm

Skeletons

Page 177: Perl.Hacks.On.Vim Perlchina

fu! ReadTemplate() let sname = input( "template type:" ) exec '0r ~/.vim/skeleton/perl/' . sname . '.pl'endfcommand! NewFromSkeleton :call ReadTemplate()

more than one skeleton for perl code

Page 178: Perl.Hacks.On.Vim Perlchina

nm cr= ^f=cf;nm cl= ^f=c^

Page 179: Perl.Hacks.On.Vim Perlchina

fu! PodHelperFunctionHeader() let subname = substitute( getline('.') , 'sub\s\+\(\w\+\)\s\+.*$' , '\1' , "" ) let lines = [ \ '=head2 ' . subname , \ '' , \ '' , \ '' , \ '=cut' , \ '', \] for text in lines :call append( line('.') - 1 , text ) endfor :call cursor( line('.') - len( lines ) + 2 , 1 )endfnmap <leader>pf :call PodHelperFunctionHeader()<CR>

Pod Helper

Page 180: Perl.Hacks.On.Vim Perlchina

abbr _perlbin #!/usr/bin/env perlabbr _s $self

Page 181: Perl.Hacks.On.Vim Perlchina

detect features

if has('perl')

endif

if has(‘netbeans_intg’)

endif

Page 182: Perl.Hacks.On.Vim Perlchina

crossplatform settings

if has('mac')

elseif has(‘win32’)

elseif has(‘unix’)

endif

Page 183: Perl.Hacks.On.Vim Perlchina

if you compiled vim with perl

Page 184: Perl.Hacks.On.Vim Perlchina

function! WhitePearl() perl << EOF VIM::Msg("pearls are nice for necklaces"); VIM::Msg("rubys for rings"); VIM::Msg("pythons for bags"); VIM::Msg("tcls????"); EOF endfunction

Page 185: Perl.Hacks.On.Vim Perlchina

:perl $a=1 :perldo $_ = reverse($_);1 :perl VIM::Msg("hello") :perl $line = $curbuf->Get(42)

Page 186: Perl.Hacks.On.Vim Perlchina

:perl VIM::Msg("Text") # displays a message :perl VIM::Msg("Error", "ErrorMsg") # displays an error message :perl VIM::Msg("remark", "Comment") # displays a highlighted message :perl VIM::SetOption("ai") # sets a vim option :perl $nbuf = VIM::Buffers() # returns the number of buffers :perl @buflist = VIM::Buffers() # returns array of all buffers :perl $mybuf = (VIM::Buffers('qq.c'))[0] # returns buffer object for 'qq.c' :perl @winlist = VIM::Windows() # returns array of all windows :perl $nwin = VIM::Windows() # returns the number of windows :perl ($success, $v) = VIM::Eval('&path') # $v: option 'path', $success: 1 :perl ($success, $v) = VIM::Eval('&xyz') # $v: '' and $success: 0 :perl $v = VIM::Eval('expand("<cfile>")') # expands <cfile> :perl $curwin->SetHeight(10) # sets the window height :perl @pos = $curwin->Cursor() # returns (row, col) array :perl @pos = (10, 10) :perl $curwin->Cursor(@pos) # sets cursor to @pos :perl $curwin->Cursor(10,10) # sets cursor to row 10 col 10

Page 187: Perl.Hacks.On.Vim Perlchina

11.

Plugins

Page 188: Perl.Hacks.On.Vim Perlchina

perlprove.vim

parse test output to quickfix window

':make *t', or ':make t'

Page 189: Perl.Hacks.On.Vim Perlchina

SnipMate

Page 190: Perl.Hacks.On.Vim Perlchina

snippet cla class .. initialize .. end class ${1:`substitute(Filename(), '^.', '\u&', '')`} def initialize(${2:args}) ${3} end

end code snippet

Page 191: Perl.Hacks.On.Vim Perlchina

DBExt.vimDatabase extension,support

Oracle, Sybase, MSSQL , MySQL, DBI etc..

Page 192: Perl.Hacks.On.Vim Perlchina

xml.vim

xml related

Page 193: Perl.Hacks.On.Vim Perlchina

FuzzyFinder.vimFuzzy/Partial pattern explorer

Page 194: Perl.Hacks.On.Vim Perlchina

The_NERD_TREE.vimDirectory Tree Explorer

Page 195: Perl.Hacks.On.Vim Perlchina

The NERD Commenter

Comment Helper

Page 196: Perl.Hacks.On.Vim Perlchina

taglist.vim透過 ctags 工具產生程式碼標籤

並可將 macro , function , variable 等資料整理出來

Page 197: Perl.Hacks.On.Vim Perlchina

autocomplpop.vim

Page 198: Perl.Hacks.On.Vim Perlchina

MRUmost recently used

Page 199: Perl.Hacks.On.Vim Perlchina

bufexplorer.vimBuffer Explorer

Page 200: Perl.Hacks.On.Vim Perlchina

git-vim

Page 201: Perl.Hacks.On.Vim Perlchina

cpan-list.vimhttp://github.com/c9s/cpan-list.vim

Page 202: Perl.Hacks.On.Vim Perlchina

template-init.vimhttp://github.com/c9s/template-init.vim

Page 203: Perl.Hacks.On.Vim Perlchina

vimrchttp://oulixe.us/_vimrchttp://oulixe.us/_gvimrc

Page 204: Perl.Hacks.On.Vim Perlchina

The old way toinstall vim scripts

Page 205: Perl.Hacks.On.Vim Perlchina
Page 206: Perl.Hacks.On.Vim Perlchina
Page 207: Perl.Hacks.On.Vim Perlchina
Page 208: Perl.Hacks.On.Vim Perlchina
Page 209: Perl.Hacks.On.Vim Perlchina

Download

Page 210: Perl.Hacks.On.Vim Perlchina

Read Install details

Page 211: Perl.Hacks.On.Vim Perlchina

Read Install details

Awful!

Page 212: Perl.Hacks.On.Vim Perlchina

閱讀安裝步驟

Awful!

Page 213: Perl.Hacks.On.Vim Perlchina
Page 214: Perl.Hacks.On.Vim Perlchina

VimanaVim script Manager

Page 215: Perl.Hacks.On.Vim Perlchina

• Vimball

• Archive File ( zip , rar )

• .vim extension file

Page 216: Perl.Hacks.On.Vim Perlchina

• Cache::File

• App::CLI

• Archive::Any

• ...etc

Page 217: Perl.Hacks.On.Vim Perlchina

$ cpan Vimana

it’s on CPAN

Page 218: Perl.Hacks.On.Vim Perlchina

$ vimana search xml

Page 219: Perl.Hacks.On.Vim Perlchina

$ vimana search xmlrrd.vim - Edit RRD data with Vim.qt.vim - tiny tool for the uic used in Qt from Trolltechsyntax-for-xul - Highlighting for XML User interface Language.maven2.vim - Compiler plugin for maven2 .... skip

Page 220: Perl.Hacks.On.Vim Perlchina

$ vimana info xml.vim

Page 221: Perl.Hacks.On.Vim Perlchina

$ vimana install xml.vim

Page 222: Perl.Hacks.On.Vim Perlchina

$ vimana install xml.vim$ vimana install rails.vim$ vimana install the-nerd-tree.vim$ vimana install taglist.vim$ vimana install snipmate$ vimana install fuzzyfinder.vimetc ...

ALL Works

Page 223: Perl.Hacks.On.Vim Perlchina

Future Plan

•upgrade

•remove

•config

•support makefile

Page 224: Perl.Hacks.On.Vim Perlchina

http://github.com/c9s/Vimana/tree/master

Git Repository

Page 225: Perl.Hacks.On.Vim Perlchina

Thank You