Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Vim Hacks

96,368 views

Published on

Vim Hacks @ COSCUP

Published in: Technology

Vim Hacks

  1. VIM Hacks Why Vim Vim c9s / Cornelius AIINK.com
  2. It doesn’t matter
  3. It does matter
  4. ...
  5. Microsoft Visual Studio
  6. Eclipse
  7. Code Block
  8. Notepad++
  9. ....
  10. ....
  11. • • IDE , Editor
  12. • • IDE , Editor • XXX Editor ..
  13. • • IDE , Editor • XXX Editor .. • ....
  14. Microsoft Windows ...
  15. GEdit
  16. Emacs
  17. TextMate
  18. VIM
  19. #!/usr/bin/env perl my $hppiness = COS::CUP();
  20. #!/usr/bin/env perl my $hppiness = COS::CUP(); Oops! “a”
  21. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  22. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  23. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  24. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  25. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  26. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  27. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  28. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  29. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  30. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  31. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  32. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  33. #!/usr/bin/env perl my $hppiness = COS::CUP(); xN
  34. #!/usr/bin/env perl my $hppiness = COS::CUP();
  35. #!/usr/bin/env perl my $happiness = COS::CUP();
  36. #!/usr/bin/env perl my $happiness = COS::CUP(); ?
  37. #!/usr/bin/env perl my $happiness = COS::CUP(); ?
  38. The VIM way...
  39. VIM: #!/usr/bin/env perl my $hppiness = COS::CUP(); F h
  40. VIM: #!/usr/bin/env perl my $happiness = COS::CUP(); a ppend a
  41. VIM
  42. VIM
  43. IDE
  44. ...
  45. ...
  46. !!!
  47. !!! !!!
  48. VIM
  49. VI Improved
  50. Learning Curve
  51. Vim
  52. Vim • Vim
  53. Vim • Vim • gVim
  54. Vim • Vim • gVim • gVim Easy
  55. Vim • Vim • gVim • gVim Easy • MacVim
  56. Vim • Vim • gVim • gVim Easy • MacVim • Vi in Emacs
  57. Vim • Vim • gVim • gVim Easy • MacVim • Vi in Emacs • Vi in Bash
  58. Vim • Vim • gVim • gVim Easy • MacVim • Vi in Emacs • Vi in Bash • etc ...
  59. Features
  60. 1. Mode
  61. INSERT NORMAL VISUAL SELECT ... et cetera
  62. ESC [Ii]NSERT NORMAL [Vv]ISUAL
  63. 1.1 Normal Mode
  64. Motion
  65. HJKL ←↓↑→
  66. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } sub func1 { }
  67. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; Cursor } sub func1 { }
  68. # comments ... foreach my $foo ( @bar ) { # [{ my @outs = grep /pattern/ , @list; } sub func1 { }
  69. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } f@ sub func1 { }
  70. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } $ sub func1 { }
  71. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } ^ sub func1 { }
  72. # comments ... H foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } sub func1 { }
  73. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; M } sub func1 { }
  74. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } sub func1 { } L
  75. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } sub func1 { }
  76. # comments ... foreach my $foo ( @bar ) { # my @outs = grep } C Insert mode sub func1 { }
  77. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } sub func1 { }
  78. # comments ... foreach my $foo ( @bar ) { # my @outs = grep /pattern/ , @list; } % sub func1 { }
  79. :h motion.txt
  80. 1.2 Insert Mode
  81. Editing text
  82. Insert Mode • i : Insert text before the cursor
  83. Insert Mode • i : Insert text before the cursor • I : Insert text before the first non-blank in the line
  84. 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
  85. 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
  86. 1.3 Visual Mode
  87. Select region
  88. Visual Mode • v : start Visual mode per character.
  89. Visual Mode • v : start Visual mode per character. • V : start Visual mode linewise.
  90. Visual Mode • v : start Visual mode per character. • V : start Visual mode linewise. • Ctrl-v : start Visual mode blockwise.
  91. 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
  92. 2. Syntax Highlight Support
  93. /opt/local/share/vim/vim72 $ ls -1 syntax/ | wc -l 520
  94. Syntax
  95. ~/.vim/syntax/[filetype].vim
  96. ~/.vim/syntax/[filetype].vim syn match [id] [re] [options]
  97. ~/.vim/syntax/[filetype].vim syn match [id] [re] [options] syn region [id] start=[re] end=[re]
  98. ~/.vim/syntax/[filetype].vim syn match [id] [re] [options] syn region [id] start=[re] end=[re] syn keyword [id] [keyword] ...
  99. ~/.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]
  100. :help syntax.txt
  101. 3. ENCODING
  102. :set fencs=utf-8,gbk,big5,euc-jp,utf-16le :set fenc=utf-8 enc=utf-8 tenc=utf-8
  103. :set fencs=utf-8,gbk,big5,euc-jp,utf-16le :set fenc=utf-8 enc=utf-8 tenc=utf-8
  104. Vim :set fencs=utf-8,gbk,big5,euc-jp,utf-16le :set fenc=utf-8 enc=utf-8 tenc=utf-8
  105. :set fencs=utf-8,gbk,big5,euc-jp,utf-16le :set fenc=utf-8 enc=utf-8 tenc=utf-8
  106. :set fencs=utf-8,gbk,big5,euc-jp,utf-16le :set fenc=utf-8 enc=utf-8 tenc=utf-8
  107. :set fencs=utf-8,gbk,big5,euc-jp,utf-16le :set fenc=utf-8 enc=utf-8 tenc=utf-8 Terminal
  108. :help encoding
  109. 4. Key Mapping
  110. :map :nmap :vmap :imap :smap :xmap ... more
  111. :map (all) :nmap (normal mode) :vmap (visual mode) :imap (insert mode) :smap (select mode) :xmap (visual , select mode) ... more
  112. :map (all) :nmap (normal mode) :vmap (visual mode) :imap (insert mode) :smap (select mode) :xmap (visual , select mode) ... more
  113. :nmap <C-c><C-c> :!gcc -Wall % -o %:r.out<CR> Ctrl C Ctrl C GCC .out
  114. Normal Mode Tab Shift-Tab nmap <tab> v> nmap <s-tab> v< vmap <tab> >gv vmap <s-tab> <gv
  115. nmap <tab> v> nmap <s-tab> v< vmap <tab> >gv vmap <s-tab> <gv Visual/Select Mode Tab Shift-Tab
  116. nmap <tab> v> nmap <s-tab> v< When mappings and menus are defined with the |:vmap| or |:vmenu| command they work vmap <tab> >gv both in Visual mode and in Select mode. When these are used in Select mode Vim vmap <s-tab> <gv automatically switches to Visual mode, so that the same behavior as in Visual mode is effective. Visual/Select Mode Tab Shift-Tab
  117. imap <F2> <C-R>=strftime("%c")<CR> Insert Mode F2
  118. 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> Command Mode Bash Key Binding
  119. :h map.txt
  120. 5. TEXT OBJECT
  121. Text Object • word • string ⇛ action • paragraph (yank,delete,change ...etc) • block
  122. Operator Mapping v|c|d i|a { | [ | ( |“ |‘ visual Region change Inner Object {} delete An Object [] () ““ Operator ‘‘
  123. va{
  124. function ( blah , blah ) ci( function ( ) function ( new_args )
  125. “Hello World” di” “” “Hello New World”
  126. 6. TAB PAPGES
  127. :tabnew
  128. :tabnew :tabedit path/to/file
  129. :tabnew :tabedit path/to/file :tabfind path/to/file
  130. :help tabpage.txt
  131. 7. FOLDS
  132. FOLD IS
  133. Fold Methods
  134. Fold Methods Syntax Fold
  135. Fold Methods Syntax Fold :set foldmethod=syntax $VIMRUNTIME/syntax/*.vim
  136. Perl : /opt/local/share/vim/vim72/syntax/perl.vim if exists("perl_want_scope_in_variables") “ ..... if exists("perl_extended_vars") “ ..... if exists("perl_fold") “ ..... .vimrc (Fold) let perl_fold = 1 let perl_extended_vars = 1 “ .... etc
  137. Fold Methods Syntax Fold Marker Fold
  138. Fold Methods Syntax Fold Marker Fold :set foldmethod=marker “{{{“ “}}}”
  139. # fold this {{{ function do_something { echo “COSCUP”; } # }}}
  140. Fold Methods Syntax Fold Marker Fold Indent Fold
  141. Fold Methods Syntax Fold Marker Fold Indent Fold :set foldmethod=indent
  142. Fold Methods Syntax Fold Marker Fold Indent Fold Manual Fold
  143. :set foldmethod=manual zf
  144. autocmd BufWinLeave *.* silent mkview autocmd BufWinEnter *.* silent loadview autocmd mkview , loadview ~/.vim/view/
  145. Fold Methods Syntax Fold Marker Fold Indent Fold Manual Fold Expr Fold (Custom Fold Function)
  146. :set foldexpr=MyFoldLevel(v:lnum)
  147. Fold Methods Syntax Fold Marker Fold Indent Fold Manual Fold Expr Fold (Custom Fold Function) Diff Fold
  148. 8.Modeline & FileType
  149. ModeLine
  150. " vim:fdm=marker:sw=2:ts=2:et:fdl=0: "============================ " Author: Cornelius < cornelius.howl{at}gmail{dot}com >
  151. " vim:fdm=marker:sw=2:ts=2:et:fdl=0: "============================ " Author: Cornelius < cornelius.howl{at}gmail{dot}com > Marker Tab Fold (Level 0 )
  152. FileType
  153. ~/.vim/ftplugin/ruby.vim vimrc
  154. ~/.vim/filetype.vim filetype.vim
  155. 9. FORMATTING
  156. :set equalprg=perltidy
  157. :set equalprg=perltidy “=” perltidy
  158. autocmd Filetype perl :set equalprg=perltidy autocmd equalprg
  159. autocmd Filetype c :set equalprg=indent C GNU Ident
  160. SQL $ 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 SQL Filter
  161. 10. QuickFix
  162. :grep & :make
  163. :grep [pattern] [filepath]
  164. :grep [pattern] [filepath] grepprg ( vimgrep) grep QuickFix
  165. QuickFix Window
  166. :set grepprg=/path/to/grep grepprg ( VIM grep)
  167. :make
  168. :make makeprg ( make) Makefile. compiler output parser
  169. :set makeprg=gmake makeprg ( make)
  170. Result ⇛ QuickFix Window
  171. :copen QuickFix Window
  172. :cclose QuickFix Window
  173. :cnext Result
  174. :cprevious Result
  175. 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>
  176. 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 “q” , <leader> “”
  177. 11. Plugin
  178. SnipMate TextMate
  179. snippet cla class .. initialize .. end class ${1:`substitute(Filename(), '^.', 'u&', '')`} def initialize(${2:args}) ${3} end end
  180. DBExt.vim Oracle, Sybase, MSSQL , MySQL, DBI
  181. xml.vim XML
  182. FuzzyFinder.vim Fuzzy/Partial pattern explorer
  183. The_NERD_TREE.vim
  184. The NERD Commenter
  185. taglist.vim ctags macro , function , variable
  186. cscope C
  187. autocomplpop.vim
  188. MRU most recently used
  189. bufexplorer.vim Buffer Explorer
  190. Rails.vim
  191. git-vim
  192. !
  193. !
  194. Vimana Vim script Manager
  195. • Vimball • Archive File ( zip , rar ) • .vim file
  196. POWERED BY Perl
  197. CPAN Vimana $ cpan Vimana
  198. $ vimana search xml
  199. $ 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
  200. script $ vimana info xml.vim
  201. xml.vim $ vimana install xml.vim
  202. $ 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
  203. Git Repository http://github.com/c9s/Vimana/tree/master
  204. ENJOY
  205. Thank You
  206. Demo ....
  207. Q &A

×