0
Perl Hacks
  on Vim
            Lin You-An
          c9s / Cornelius
       pause id: CORNELIUS
Lin You-An
VIM & Perl
how can vim improve
perl coding productivity
 vim       coding     ?
The worst way to edit
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

     Oops!
      lost “a”
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                            xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                          xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                         xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                        xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                      xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                     xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                    xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                  xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
                 xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
               xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
            xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
        xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
      xN
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
#!/usr/bin/env perl
my $hppiness = PerlChina->join();
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

               if this costs 4 sec
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

                      if this costs 4 sec

        if x 50 times th...
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

                      if this costs 4 sec

        if x 50 times th...
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

                        if this costs 4 sec

          if x 50 time...
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

                        if this costs 4 sec

          x 50 times t...
#!/usr/bin/env perl
my $hppiness = PerlChina->join();

                        if this costs 4 sec


              Awful
 ...
What can you do in
   16.6 hours
or more hacking
sleep...
时间
The VIM way...
VIM:
#!/usr/bin/env perl
my $hppiness = perlchina->join();
                        F   h
VIM:
#!/usr/bin/env perl
my $hppiness = perlchina->join();
       a ppend a
VIM:
#!/usr/bin/env perl
my $hppiness = perlchina->join();
    f   P
VIM:
#!/usr/bin/env perl
my $hppiness = Perlchina->join();
                ~
VIM:
#!/usr/bin/env perl
my $hppiness = PErlchina->join();
                 ~
VIM:
#!/usr/bin/env perl
my $hppiness = PERlchina->join();
                  ~
VIM:
#!/usr/bin/env perl
my $hppiness = PERLchina->join();
                   ~
So What is VIM ?
VIM   IDE
VIM is an
 Editor
VI Improved
Move More Efficiently.
               动
Features
1. Mode
More Than
4 Edit Mode
INSERT
NORMAL
 VISUAL
 SELECT
  ... et cetera
ESC
      [Ii]NSERT
      NORMAL
      [Vv]ISUAL
1.1 Normal Mode
Motion
HJKL
←↓↑→
h ,j ,k ,l
H ,M ,L
 w ,e ,b
f[x] , t[x]
[{ , ]} , %
 ( ,) ,{ ,}
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

sub func1 {

...
# comments ...

foreach my $foo ( @bar ) {

    # do something

   my @outs = grep /pattern/ , @list;
	 	             Curs...
# comments ...

foreach my $foo ( @bar ) {

    # do something            [{

   my @outs = grep /pattern/ , @list;
	 	
}
...
# comments ...

foreach my $foo ( @bar ) {

    # do something

   my @outs = grep /pattern/ , @list;
	 	
}               ...
# comments ...

foreach my $foo ( @bar ) {

    # do something

   my @outs = grep /pattern/ , @list;
	 	
}               ...
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

sub fu...
# comments ...
    H
foreach my $foo ( @bar ) {

    # do something

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

sub func...
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

sub func1 {...
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

sub func1 {

...
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

sub func1 {

...
# comments ...

foreach my $foo ( @bar ) {

    # do something

    my @outs = grep
	
}                     C
            ...
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

sub func1 {

...
# comments ...

foreach my $foo ( @bar ) {

    # do something

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

     %
sub fu...
:h motion.txt
1.2 Insert Mode
Editing text
Insert Mode

• i : Insert text before the cursor
Insert Mode

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

• i : Insert text before the cursor
• I : Insert text before the first non-blank in the
  line
• a : Append te...
Insert Mode

• i : Insert text before the cursor
• I : Insert text before the first non-blank in the
  line
• a : Append te...
1.3 Visual Mode
Select region
Visual Mode

• v : start Visual mode per character.
Visual Mode

• v : start Visual mode per character.
• V : start Visual mode linewise.
Visual Mode

• v : start Visual mode per character.
• V : start Visual mode linewise.
• Ctrl-v : start Visual mode blockwi...
Visual Mode

• v : start Visual mode per character.
• V : start Visual mode linewise.
• Ctrl-v : start Visual mode blockwi...
2. Syntax Highlight Support
/opt/local/share/vim/vim72
$ ls -1 syntax/ | wc -l
   520


              more than 500 syntax files
Syntax
Perl code needs syntax
but we design more
   syntax in Perl.
              Perl
Modules need syntax too
Template::Declare
  template simple => sub {
     html {
       head {}
       body {
          p {'Hello, world wide web!...
package MyView;
use Markapl;

tempalte '/a/page.html' => sub {
  h1("#title") { "Hi" };
  p(".first") { "In the begining, l...
Jifty::DBI::Schema
  package TestApp::Model::Phone;
  use Jifty::DBI::Schema;
  use Jifty::DBI::Record schema {
     colum...
~/.vim/syntax/[filetype].vim
~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]
~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]
syn region [id] start=[re] end=[re]
~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]
syn region [id] start=[re] end=[re]
syn keyword [id] [keyword]...
~/.vim/syntax/[filetype].vim

syn match [id] [re] [options]
syn region [id] start=[re] end=[re]
syn keyword [id] [keyword]...
~/.vim/syntax/perl/*.vim
~/.vim/syntax/perl/jifty.vim
~/.vim/syntax/perl/template-declare.vim
~/.vim/syntax/perl/markapl.v...
" Fold Jifty Templates
syn region jifty_td_fold
    start=+^(privates+)=templates+
    end=+^};+ transparent fold keepend
...
Fold Jifty::DBI schema columns and action params

 syn region jfscolumn
      start=+^s*(param|column)>+
      end=+;+ con...
:help syntax.txt
4.
  key
mapping
:map
:nmap
:vmap
:imap
:smap
:xmap
... more
:map       (all)

:nmap      (normal mode)

:vmap      (visual mode)

:imap      (insert mode)

:smap      (select mode)

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



Ctrl C      Ctrl C

     let perl check current perl script syntax
use Tab and Shift-Tab to indent region ,
        instead of “>” and “<“

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

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



               to ins...
cmap   <c-a>   <home>
cmap   <c-e>   <end>
cnoremap <c-b>        <left>
cnoremap <c-d>        <del>
cnoremap <c-f>        ...
:h map.txt
5.
Text Object
Text Object

• word
• string      ⇛             action
• paragraph       (yank,delete,change ...etc)

• block
Operator Mapping

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

   visual                         Region
  change    Inner O...
va{
sub( blah , blah )
           ci(


     sub( )

   sub( new_args )
“Hello World”
          di”


     “”

“Hello New World”
Vim7.1 Comes with
  tag block region
<aaa> Hola </aaa>
           vit


<aaa> Hola </aaa>

            Visual Select
Alias
   "dl" delete character (alias: "x")   |dl|
   "diw" delete inner word               *diw*
   "daw" delete a word  ...
8.
FOLDS
FOLD IS
Fold Methods
Fold Methods
 Syntax Fold
Fold Methods
   Syntax Fold
                 :set foldmethod=syntax


set fold method as syntax , check out more options i...
Perl built-in syntax
/opt/local/share/vim/vim72/syntax/perl.vim


if exists("perl_want_scope_in_variables")
“ .....
if exi...
for complex things like
                                        @{${"foo"}}.



let   perl_include_pod = 1
let   perl_exte...
Fold Methods
 Syntax Fold
 Marker Fold
Fold Methods
 Syntax Fold
 Marker Fold

          :set foldmethod=marker

               fold region by markers , the
    ...
# fold this {{{
sub do_something {

    # a lot of work ...

}
#   }}}
# fold this {{{
sub do_something {

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

         #...
Fold Methods
 Syntax Fold
 Marker Fold
 Indent Fold
Fold Methods
 Syntax Fold
 Marker Fold
 Indent Fold
          :set foldmethod=indent

                use indent to fold
Fold Methods
 Syntax Fold
 Marker Fold
 Indent Fold
 Manual Fold
:set foldmethod=manual

          create folds manually
autocmd   BufWinLeave *.*          silent mkview
autocmd   BufWinEnter *.*          silent loadview

                autoc...
Fold Methods
 Syntax Fold
 Marker Fold
 Indent Fold
 Manual Fold
 Expr Fold (Custom Fold Function)
:set foldexpr=MyFoldLevel(v:lnum)


                customized fold function
Fold Methods
 Syntax Fold
 Marker Fold
 Indent Fold
 Manual Fold
 Expr Fold (Custom Fold Function)
 Diff Fold
*za*
          za    When on a closed fold: open it. When folds are nested, you
               may have to use "za" severa...
:h folding
10. QuickFix
:grep
:grep [pattern] [filepath]
Result ⇛ QuickFix Window
QuickFix Window
:set grepprg=/path/to/grep
:copen
:cclose
:cnext
:cprevious
QuickFix Window Toggle
com! -bang -nargs=? QFix cal QFixToggle(<bang>0)
fu! QFixToggle(forced)
   if exists("g:qfix_win") ...
9.
Helpful Settings
Should we re-indent code
       manually ?
PerlTidy++
:set equalprg=perltidy
:set equalprg=perltidy

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

              then press “=” , it will p...
autocmd Filetype perl :set equalprg=perltidy


                 when filetype is perl
             use autocmd to set equal...
to reformat SQL ,
we have SQL::Beautify module
SQL::Beautify
 $ cat bin/sql-beautify.pl
 #!/usr/bin/env perl
 use warnings;
 use strict;
 use SQL::Beautify;
 local $/;
 ...
Javascript::Beautifier
  [localhost]$ cat bin/js_beautifier
  #!/usr/bin/env perl
  use JavaScript::Beautifier qw/js_beauti...
HTML Entities      $ cat bin/html-entities.pl
                   use HTML::Entities;
                   for ( <STDIN> ) {
...
Morse Encoder
    use Convert::Morse qw(as_ascii as_morse is_morsable);
    print as_morse( $_ ) . “n” for ( <STDIN> );

c...
source code
  traverse
Case:

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


         we need to find Data/Dumper.pm in @INC
                 ...
fu! GetCursorModuleName()
  let cw = substitute( expand("<cWORD>") , '.{-}((w+)(::w+)
*).*$' , '1' , '' )
  return cw
endf...
fu! FindModuleFileInPaths()
  let paths = GetPerlLibPaths()
  let fname = TranslateModuleName(     GetCursorModuleName()  ...
Install CPAN Module from <cWORD>
 nmap <C-x><C-i> :call InstallCPANModule()<CR>
 function! InstallCPANModule()
   let l = ...
Skeletons

au bufnewfile *.pl 0r ~/.vim/skeleton/template.pl
au bufnewfile *.pod 0r ~/.vim/skeleton/template.pod
au bufnewfil...
more than one skeleton for perl code

fu! ReadTemplate()
  let sname = input( "template type:" )
  exec '0r ~/.vim/skeleto...
nm cr= ^f=cf;
nm cl= ^f=c^
Pod Helper
fu! PodHelperFunctionHeader()
  let subname = substitute( getline('.') , 'subs+(w+)s
+.*$' , '1' , "" )
  let l...
abbr _perlbin #!/usr/bin/env perl
         abbr _s $self
detect features
if has('perl')

endif

if has(‘netbeans_intg’)

endif
crossplatform settings
 if has('mac')

 elseif has(‘win32’)

 elseif has(‘unix’)

 endif
if you compiled vim
      with perl
function! WhitePearl()
perl << EOF
     VIM::Msg("pearls are nice for necklaces");
     VIM::Msg("rubys for rings");
     ...
:perl $a=1
:perldo $_ = reverse($_);1
:perl VIM::Msg("hello")
:perl $line = $curbuf->Get(42)
:perl   VIM::Msg("Text")                # displays a message
:perl   VIM::Msg("Error", "ErrorMsg")   # displays an error m...
11.
Plugins
perlprove.vim
 parse test output to quickfix window

        ':make *t', or ':make t'
SnipMate
snippet cla class .. initialize .. end
    class ${1:`substitute(Filename(), '^.', 'u&', '')`}
        def initialize(${2:...
DBExt.vim
Database extension support
  Oracle, Sybase, MSSQL ,
     MySQL, DBI etc..
xml.vim
 xml related
FuzzyFinder.vim
 Fuzzy/Partial pattern explorer
The_NERD_TREE.vim
    Directory Tree Explorer
The NERD Commenter
     Comment Helper
taglist.vim
     ctags
macro , function , variable
autocomplpop.vim
MRU
most recently used
bufexplorer.vim
    Buffer Explorer
git-vim
cpan-list.vim
http://github.com/c9s/cpan-list.vim
template-init.vim
http://github.com/c9s/template-init.vim
vimrc
http://oulixe.us/_vimrc
http://oulixe.us/_gvimrc
The old way to
install vim scripts
Download
Read Install details
Read Install details
             Awful!
Awful!
Vimana
  Vim script Manager
• Vimball
• Archive File ( zip , rar )
• .vim extension file
• Cache::File
• App::CLI
• Archive::Any
• ...etc
it’s on CPAN


 $ cpan Vimana
$ vimana search xml
$ vimana search xml
rrd.vim             - Edit RRD data with Vim.
qt.vim              - tiny tool for the uic used in Qt f...
$ vimana info xml.vim
$ vimana install xml.vim
$ vimana   install   xml.vim
$ vimana   install   rails.vim
$ vimana   install   the-nerd-tree.vim
$ vimana   install   ta...
Future Plan

•upgrade
• remove
• config
• support makefile
Git Repository
http://github.com/c9s/Vimana/tree/master
Thank You
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Upcoming SlideShare
Loading in...5
×

Perl.Hacks.On.Vim Perlchina

4,092

Published on

2 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,092
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
143
Comments
2
Likes
8
Embeds 0
No embeds

No notes for slide

Transcript of "Perl.Hacks.On.Vim Perlchina"

  1. 1. Perl Hacks on Vim Lin You-An c9s / Cornelius pause id: CORNELIUS
  2. 2. Lin You-An
  3. 3. VIM & Perl
  4. 4. how can vim improve perl coding productivity vim coding ?
  5. 5. The worst way to edit
  6. 6. #!/usr/bin/env perl my $hppiness = PerlChina->join();
  7. 7. #!/usr/bin/env perl my $hppiness = PerlChina->join(); Oops! lost “a”
  8. 8. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  9. 9. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  10. 10. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  11. 11. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  12. 12. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  13. 13. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  14. 14. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  15. 15. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  16. 16. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  17. 17. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  18. 18. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  19. 19. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  20. 20. #!/usr/bin/env perl my $hppiness = PerlChina->join(); xN
  21. 21. #!/usr/bin/env perl my $hppiness = PerlChina->join();
  22. 22. #!/usr/bin/env perl my $hppiness = PerlChina->join();
  23. 23. #!/usr/bin/env perl my $hppiness = PerlChina->join(); if this costs 4 sec
  24. 24. #!/usr/bin/env perl my $hppiness = PerlChina->join(); if this costs 4 sec if x 50 times this kind of situation per day
  25. 25. #!/usr/bin/env perl my $hppiness = PerlChina->join(); if this costs 4 sec if x 50 times this kind of situation per day
  26. 26. #!/usr/bin/env perl my $hppiness = PerlChina->join(); if this costs 4 sec if x 50 times this kind of situation per day and you work for more than 300 day per year = 16.6 hours
  27. 27. #!/usr/bin/env perl my $hppiness = PerlChina->join(); if this costs 4 sec x 50 times this kind of situation per day ? and you work for more than 300 day per year = 16.6 hours
  28. 28. #!/usr/bin/env perl my $hppiness = PerlChina->join(); if this costs 4 sec Awful x 50 times this kind of situation per day ? and you work for more than 300 day per year = 16.6 hours
  29. 29. What can you do in 16.6 hours
  30. 30. or more hacking
  31. 31. sleep...
  32. 32. 时间
  33. 33. The VIM way...
  34. 34. VIM: #!/usr/bin/env perl my $hppiness = perlchina->join(); F h
  35. 35. VIM: #!/usr/bin/env perl my $hppiness = perlchina->join(); a ppend a
  36. 36. VIM: #!/usr/bin/env perl my $hppiness = perlchina->join(); f P
  37. 37. VIM: #!/usr/bin/env perl my $hppiness = Perlchina->join(); ~
  38. 38. VIM: #!/usr/bin/env perl my $hppiness = PErlchina->join(); ~
  39. 39. VIM: #!/usr/bin/env perl my $hppiness = PERlchina->join(); ~
  40. 40. VIM: #!/usr/bin/env perl my $hppiness = PERLchina->join(); ~
  41. 41. So What is VIM ?
  42. 42. VIM IDE
  43. 43. VIM is an Editor
  44. 44. VI Improved
  45. 45. Move More Efficiently. 动
  46. 46. Features
  47. 47. 1. Mode
  48. 48. More Than 4 Edit Mode
  49. 49. INSERT NORMAL VISUAL SELECT ... et cetera
  50. 50. ESC [Ii]NSERT NORMAL [Vv]ISUAL
  51. 51. 1.1 Normal Mode
  52. 52. Motion
  53. 53. HJKL ←↓↑→
  54. 54. h ,j ,k ,l H ,M ,L w ,e ,b f[x] , t[x] [{ , ]} , % ( ,) ,{ ,}
  55. 55. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } sub func1 { }
  56. 56. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; Cursor } sub func1 { }
  57. 57. # comments ... foreach my $foo ( @bar ) { # do something [{ my @outs = grep /pattern/ , @list; } sub func1 { }
  58. 58. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } f@ sub func1 { }
  59. 59. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } $ sub func1 { }
  60. 60. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } ^ sub func1 { }
  61. 61. # comments ... H foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } sub func1 { }
  62. 62. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; M } sub func1 { }
  63. 63. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } sub func1 { } L
  64. 64. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } sub func1 { }
  65. 65. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep } C Insert mode sub func1 { }
  66. 66. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } sub func1 { }
  67. 67. # comments ... foreach my $foo ( @bar ) { # do something my @outs = grep /pattern/ , @list; } % sub func1 { }
  68. 68. :h motion.txt
  69. 69. 1.2 Insert Mode
  70. 70. Editing text
  71. 71. Insert Mode • i : Insert text before the cursor
  72. 72. Insert Mode • i : Insert text before the cursor • I : Insert text before the first non-blank in the line
  73. 73. Insert Mode • i : Insert text before the cursor • I : Insert text before the first non-blank in the line • a : Append text after the cursor
  74. 74. Insert Mode • 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
  75. 75. 1.3 Visual Mode
  76. 76. Select region
  77. 77. Visual Mode • v : start Visual mode per character.
  78. 78. Visual Mode • v : start Visual mode per character. • V : start Visual mode linewise.
  79. 79. Visual Mode • v : start Visual mode per character. • V : start Visual mode linewise. • Ctrl-v : start Visual mode blockwise.
  80. 80. 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
  81. 81. 2. Syntax Highlight Support
  82. 82. /opt/local/share/vim/vim72 $ ls -1 syntax/ | wc -l 520 more than 500 syntax files
  83. 83. Syntax
  84. 84. Perl code needs syntax
  85. 85. but we design more syntax in Perl. Perl
  86. 86. Modules need syntax too
  87. 87. Template::Declare template simple => sub { html { head {} body { p {'Hello, world wide web!'} } } }; # ... templates
  88. 88. package MyView; use Markapl; tempalte '/a/page.html' => sub { h1("#title") { "Hi" }; p(".first") { "In the begining, lorem ipsum...." }; p(style => "color: red;") { "But...." }; }
  89. 89. 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 => ...; };
  90. 90. ~/.vim/syntax/[filetype].vim
  91. 91. ~/.vim/syntax/[filetype].vim syn match [id] [re] [options]
  92. 92. ~/.vim/syntax/[filetype].vim syn match [id] [re] [options] syn region [id] start=[re] end=[re]
  93. 93. ~/.vim/syntax/[filetype].vim syn match [id] [re] [options] syn region [id] start=[re] end=[re] syn keyword [id] [keyword] ...
  94. 94. ~/.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]
  95. 95. ~/.vim/syntax/perl/*.vim ~/.vim/syntax/perl/jifty.vim ~/.vim/syntax/perl/template-declare.vim ~/.vim/syntax/perl/markapl.vim
  96. 96. " Fold Jifty Templates syn region jifty_td_fold start=+^(privates+)=templates+ end=+^};+ transparent fold keepend " Fold Jifty Template Delcare Tags syn region jifty_tag_fold start="^z(s*)<(div|table|row|form|script| cell|row|th|tfoot)>s*{" end="^z1};=" fold keepend
  97. 97. Fold Jifty::DBI schema columns and action params syn region jfscolumn start=+^s*(param|column)>+ end=+;+ contained contains=jfspropertybe,jfsproperty,jfscol,jfscolname, perlComment, perlString, perlFunction, perlFloat, perlNumber, perlSpecialString, perlStringUnexpanded fold
  98. 98. :help syntax.txt
  99. 99. 4. key mapping
  100. 100. :map :nmap :vmap :imap :smap :xmap ... more
  101. 101. :map (all) :nmap (normal mode) :vmap (visual mode) :imap (insert mode) :smap (select mode) :xmap (visual , select mode) ... more
  102. 102. :nmap <C-c><C-c> :!perl -Wc % Ctrl C Ctrl C let perl check current perl script syntax
  103. 103. use Tab and Shift-Tab to indent region , instead of “>” and “<“ nmap <tab> v> nmap <s-tab> v< vmap <tab> >gv vmap <s-tab> <gv
  104. 104. imap <F2> <C-R>=strftime("%c")<CR> imap <F2> <C-R>=system(‘perl -MDateTime -e DateTime->now’)<CR> to insert timestamp
  105. 105. 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
  106. 106. :h map.txt
  107. 107. 5. Text Object
  108. 108. Text Object • word • string ⇛ action • paragraph (yank,delete,change ...etc) • block
  109. 109. Operator Mapping v|c|d i|a { | [ | ( |“ |‘ visual Region change Inner Object {} delete An Object [] () ““ Operator ‘‘
  110. 110. va{
  111. 111. sub( blah , blah ) ci( sub( ) sub( new_args )
  112. 112. “Hello World” di” “” “Hello New World”
  113. 113. Vim7.1 Comes with tag block region
  114. 114. <aaa> Hola </aaa> vit <aaa> Hola </aaa> Visual Select
  115. 115. Alias "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
  116. 116. 8. FOLDS
  117. 117. FOLD IS
  118. 118. Fold Methods
  119. 119. Fold Methods Syntax Fold
  120. 120. Fold Methods Syntax Fold :set foldmethod=syntax set fold method as syntax , check out more options in: $VIMRUNTIME/syntax/*.vim
  121. 121. 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 enablethose features in your .vimrc let perl_fold = 1 let perl_extended_vars = 1 “ .... etc
  122. 122. for complex things like @{${"foo"}}. let perl_include_pod = 1 let perl_extended_vars = 1 let perl_want_scope_in_variables = 1 let perl_fold = 1 let perl_fold_blocks = 1 for something like $pack::var1
  123. 123. Fold Methods Syntax Fold Marker Fold
  124. 124. Fold Methods Syntax Fold Marker Fold :set foldmethod=marker fold region by markers , the default marker is “{{{“ , “}}}”
  125. 125. # fold this {{{ sub do_something { # a lot of work ... } # }}}
  126. 126. # fold this {{{ sub do_something { # a lot of work ... # foldlevel 2 {{{2 # foldlevel 4 {{{4 # }}}4 # foldlevel here is 3 # }}}2 } # }}}
  127. 127. Fold Methods Syntax Fold Marker Fold Indent Fold
  128. 128. Fold Methods Syntax Fold Marker Fold Indent Fold :set foldmethod=indent use indent to fold
  129. 129. Fold Methods Syntax Fold Marker Fold Indent Fold Manual Fold
  130. 130. :set foldmethod=manual create folds manually
  131. 131. autocmd BufWinLeave *.* silent mkview autocmd BufWinEnter *.* silent loadview autocmd mkview , loadview ~/.vim/view/
  132. 132. Fold Methods Syntax Fold Marker Fold Indent Fold Manual Fold Expr Fold (Custom Fold Function)
  133. 133. :set foldexpr=MyFoldLevel(v:lnum) customized fold function
  134. 134. Fold Methods Syntax Fold Marker Fold Indent Fold Manual Fold Expr Fold (Custom Fold Function) Diff Fold
  135. 135. *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. za When on an open fold: close it and set 'foldenable'. This will only close one level, since using "za" again will open zA 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. zm When on an open fold: close it recursively and set zM zm Fold more: Subtract one from 'foldlevel'. If 'foldlevel' was already zero nothing happens. zr 'foldenable' will be set. *zM* zR zM Close all folds: set 'foldlevel' to 0. 'foldenable' will be set. zj , zk [z , ]z
  136. 136. :h folding
  137. 137. 10. QuickFix
  138. 138. :grep
  139. 139. :grep [pattern] [filepath]
  140. 140. Result ⇛ QuickFix Window
  141. 141. QuickFix Window
  142. 142. :set grepprg=/path/to/grep
  143. 143. :copen
  144. 144. :cclose
  145. 145. :cnext
  146. 146. :cprevious
  147. 147. QuickFix Window Toggle 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("$") en endf nn <leader>q :QFix<cr> nmap to “q” , the default <leader> key is “”
  148. 148. 9. Helpful Settings
  149. 149. Should we re-indent code manually ?
  150. 150. PerlTidy++
  151. 151. :set equalprg=perltidy
  152. 152. :set equalprg=perltidy to re-format a region , set perltidy as your reformater then press “=” , it will pass to perltidy
  153. 153. autocmd Filetype perl :set equalprg=perltidy when filetype is perl use autocmd to set equalprg
  154. 154. to reformat SQL , we have SQL::Beautify module
  155. 155. SQL::Beautify $ cat bin/sql-beautify.pl #!/usr/bin/env perl use 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
  156. 156. Javascript::Beautifier [localhost]$ cat bin/js_beautifier #!/usr/bin/env perl use 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; autocmd Filetype javascript :set equalprg=js-beautify.pl command! -range JSF :'<,'>!/Users/c9s/bin/js_beautify.pl
  157. 157. HTML Entities $ cat bin/html-entities.pl use HTML::Entities; for ( <STDIN> ) { print encode_entities( $_ ) . "n"; } command! -range Entities :'<,'>!/Users/c9s/bin/html-entities.pl " <a href="">YAPC ASIA</a> &quot; &lt;a href=&quot;&quot;&gt;YAPC ASIA&lt;/a&gt;
  158. 158. 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'); } ... ..- -... .... .- -. -.. .-.. . .-. ..--.- ... - .- .-. - { -- -.-- -.--. $-.- . .-. -. . .-.. --..-- $.... . .- .--. --..-- $... . ... ... .. --- -. -.--.- -...- .--.-. ..--.- [ -.- .--. .-. .. -. - .-..-. ... . ... ... .. --- -. .-..-. --..-- $... . ... ... .. --- -. -....- >.. -.. --..-- .-..-. .... .- . $.... . .- .--. -....- >{-.-. --- ..- -. - } -...- ----- -.-.- $-.- . .-. -. . .-.. -....- >-.-- .. . .-.. -.. -.--. .----. .. -. -.-. .-. . -- . -. - .----. -.--.- -.-.- }
  159. 159. source code traverse
  160. 160. Case: use Data::Dumper; Your::App->do_something(); we need to find Data/Dumper.pm in @INC then type something like $ vim /opt/local/..../site_perl/Data/Dumper.pm
  161. 161. fu! GetCursorModuleName() let cw = substitute( expand("<cWORD>") , '.{-}((w+)(::w+) *).*$' , '1' , '' ) return cw endfunction 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 paths endf
  162. 162. 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 endfor endfunction nmap <leader>fm :call FindModuleFileInPaths()<CR> type  “fm”  rather  than  type  $  perldoc  -­‐m  Module::Name
  163. 163. Install CPAN Module from <cWORD> 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 "Donen" endfunction
  164. 164. Skeletons au bufnewfile *.pl 0r ~/.vim/skeleton/template.pl au bufnewfile *.pod 0r ~/.vim/skeleton/template.pod au bufnewfile *.pm 0r ~/.vim/skeleton/template.pm
  165. 165. more than one skeleton for perl code fu! ReadTemplate() let sname = input( "template type:" ) exec '0r ~/.vim/skeleton/perl/' . sname . '.pl' endf command! NewFromSkeleton :call ReadTemplate()
  166. 166. nm cr= ^f=cf; nm cl= ^f=c^
  167. 167. Pod Helper fu! PodHelperFunctionHeader() let subname = substitute( getline('.') , 'subs+(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 ) endf nmap <leader>pf :call PodHelperFunctionHeader()<CR>
  168. 168. abbr _perlbin #!/usr/bin/env perl abbr _s $self
  169. 169. detect features if has('perl') endif if has(‘netbeans_intg’) endif
  170. 170. crossplatform settings if has('mac') elseif has(‘win32’) elseif has(‘unix’) endif
  171. 171. if you compiled vim with perl
  172. 172. 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
  173. 173. :perl $a=1 :perldo $_ = reverse($_);1 :perl VIM::Msg("hello") :perl $line = $curbuf->Get(42)
  174. 174. :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 'q :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 :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
  175. 175. 11. Plugins
  176. 176. perlprove.vim parse test output to quickfix window ':make *t', or ':make t'
  177. 177. SnipMate
  178. 178. snippet cla class .. initialize .. end class ${1:`substitute(Filename(), '^.', 'u&', '')`} def initialize(${2:args}) ${3} end end code snippet
  179. 179. DBExt.vim Database extension support Oracle, Sybase, MSSQL , MySQL, DBI etc..
  180. 180. xml.vim xml related
  181. 181. FuzzyFinder.vim Fuzzy/Partial pattern explorer
  182. 182. The_NERD_TREE.vim Directory Tree Explorer
  183. 183. The NERD Commenter Comment Helper
  184. 184. taglist.vim ctags macro , function , variable
  185. 185. autocomplpop.vim
  186. 186. MRU most recently used
  187. 187. bufexplorer.vim Buffer Explorer
  188. 188. git-vim
  189. 189. cpan-list.vim http://github.com/c9s/cpan-list.vim
  190. 190. template-init.vim http://github.com/c9s/template-init.vim
  191. 191. vimrc http://oulixe.us/_vimrc http://oulixe.us/_gvimrc
  192. 192. The old way to install vim scripts
  193. 193. Download
  194. 194. Read Install details
  195. 195. Read Install details Awful!
  196. 196. Awful!
  197. 197. Vimana Vim script Manager
  198. 198. • Vimball • Archive File ( zip , rar ) • .vim extension file
  199. 199. • Cache::File • App::CLI • Archive::Any • ...etc
  200. 200. it’s on CPAN $ cpan Vimana
  201. 201. $ vimana search xml
  202. 202. $ vimana search xml rrd.vim - Edit RRD data with Vim. qt.vim - tiny tool for the uic used in Qt from Trolltech syntax-for-xul - Highlighting for XML User interface Language. maven2.vim - Compiler plugin for maven2 .... skip
  203. 203. $ vimana info xml.vim
  204. 204. $ vimana install xml.vim
  205. 205. $ vimana install xml.vim $ vimana install rails.vim $ vimana install the-nerd-tree.vim $ vimana install taglist.vim $ vimana install snipmate $ vimana install fuzzyfinder.vim etc ... ALL Works
  206. 206. Future Plan •upgrade • remove • config • support makefile
  207. 207. Git Repository http://github.com/c9s/Vimana/tree/master
  208. 208. Thank You
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×