Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vim

高見龍
December 08, 2013

 Vim

Vim, maybe the final text editor you need to learn :)

高見龍

December 08, 2013
Tweet

More Decks by 高見龍

Other Decks in Technology

Transcript

  1. Vim
    the final text editor you need to learn
    !
    ⾼高⾒見⻯⿓龍

    View Slide

  2. View Slide

  3. Current Status
    80% iOS app, 20% Ruby/Rails

    View Slide

  4. Scenario

    View Slide

  5. I'm looking for a text editor..

    View Slide

  6. 1. launch quickly
    2. easy to navigate between files
    3. and pretty syntax highlight

    View Slide

  7. Vim

    View Slide

  8. Vim = Vi iMproved

    View Slide

  9. Free and Charity

    View Slide

  10. What's Vim?

    View Slide

  11. it may look a little old-fashioned

    View Slide

  12. but it can also be modern : )

    View Slide

  13. So, Why Vim?

    View Slide

  14. Keyboard-driven

    View Slide

  15. Keyboard is King!

    View Slide

  16. and, Why not Vim?

    View Slide

  17. seems very hard to learn..

    View Slide

  18. you won’t use other editors after learning
    Vim..

    View Slide

  19. Today, we have 2 Goals..

    View Slide

  20. no Mouse

    View Slide

  21. no Arrow keys

    View Slide

  22. muscle memory

    View Slide

  23. Tips

    View Slide

  24. in terminal:
    1. ctrl + z to stash vim, and type fg to bring it back.
    2. ctrl + w to delete a word, ctrl + u to delete a whole
    line, both work in terminal and vim edit mode.
    3. ctrl + r to find history commands fuzzily.

    View Slide

  25. How to Vim?

    View Slide

  26. Practice:
    Install Vim in your machine.
    http://www.vim.org/download.php

    View Slide

  27. :help

    View Slide

  28. Modes
    normal, visual, and edit mode

    View Slide

  29. Modes Switch:
    1. in normal mode, i or a or o to enter edit mode(i =
    insert, a = append, o = newline)
    2. in edit mode, ESC or Ctrl+[ to enter normal mode
    3. in normal mode, hit v or V to enter visual mode
    4. in visual mode, hit ESC or Ctrl+[ to normal mode

    View Slide

  30. ESC or Ctrl + [

    View Slide

  31. View Slide

  32. Practice:
    launch Vim and switch between normal,
    visual, and edit mode.

    View Slide

  33. Movement
    :h movement

    View Slide

  34. move cursor with h, j, k and l
    disable arrow keys if possible

    View Slide

  35. photo by Kent-Chen

    View Slide

  36. Practice:
    Use h, j, k, l instead of using arrow keys.

    View Slide

  37. http://vim-adventures.com/

    View Slide

  38. ✓w or W to move cursor forward by a word, and b
    or B is backward.
    ✓0 (number zero) to back to the begin of the line,
    and $ is jump to the end.
    ✓fx will stop the cursor at the next "x" character in
    current line, and F is search backward.

    View Slide

  39. ✓} will move cursor to next section, { move to last
    section.
    ✓gg will move the cursor to the top of the current
    file, and G will jump to bottom.

    View Slide

  40. Practice:
    Use word jumpers (b, w, fx..etc) to move
    cursor between words and lines.

    View Slide

  41. ✓zz, zb, zt
    ✓/ search, n can jump to next matched result, and N
    will jump to last one.
    ✓* search the word on cursor.

    View Slide

  42. Practice:
    search some keywords in your document.

    View Slide

  43. Visual Selection

    View Slide

  44. ✓vit will visually select content between the tag,
    while vat will even including the tag.
    ✓vi" will visually select content between current
    double quotes.
    ✓viw will select the current word.
    ✓ctrl+v to enter block selection mode.

    View Slide

  45. Basic

    View Slide

  46. ✓:w write to file.
    ✓:q exit Vim.
    ✓:tabe create a new tab.
    ✓gt to switch to next tab, gT to previous tab. (I
    map gt and gT to F7 and F8 in my vimrc)
    ✓:new to create a horizontal split window, :vnew
    or :vs to create a vertical split window.

    View Slide

  47. ✓vi -o a.rb b.rb to open those two files at the same
    time with horizontal split window.
    ✓vi -O a.rb b.rb same as above, but in vertical split
    window.
    ✓vi -p a.rb b.rb to open files with tabs.
    ✓vi http://www.eddie.com.tw will read the source
    content into vim directly.

    View Slide

  48. Practice:
    1. quit Vim, and launch Vim, quite Vim, and
    launch Vim .. x N
    2. open a file with Vim, exit, then open it
    again, then exit .. x N

    View Slide

  49. Buffers
    :ls, :bd, :b3, :b filename, :tab ba

    View Slide

  50. Practice:
    try to open several files, and switch between
    them with buffer commands.

    View Slide

  51. Vim Text Objects

    View Slide

  52. d 3 w = delete 3 words

    View Slide

  53. Word:
    1. aw - around word
    2. iw - inner word

    View Slide

  54. Practice:
    1. select a word, deselect, and select another word, and
    deselect.. x N
    !
    2. select a word, delete it, select another word, and
    delete.. X N

    View Slide

  55. Sentence:
    1. as - a sentence
    2. is - inner sentence

    View Slide

  56. Practice:
    1. select a sentence, deselect, and select another
    sentence, and deselect.. x N
    !
    2. select a sentence, delete it, select another sentence,
    and delete.. X N

    View Slide

  57. Paragraph:
    1. ap - a paragraph
    2. ip - inner paragraph

    View Slide

  58. Practice:
    1. select a paragraph, deselect, and select another
    paragraph, and deselect.. x N
    !
    2. select a paragraph, delete it, select another
    paragraph, and delete.. X N

    View Slide

  59. Folding
    :h folds

    View Slide

  60. Basic:
    - zf to fold selected lines
    - zd to un-fold selected lines

    View Slide

  61. Practice:
    fold several lines, and unfold them .. x N

    View Slide

  62. Editing

    View Slide

  63. ✓D to clear all content of current line after the
    cursor.
    ✓C like D, but enter insert mode.
    dG will clear all content after the cursor, dgg will
    clear all content before the cursor.
    ✓x remove a character.
    ✓. to repeat last action.
    ✓dd to delete whole line, 3dd to delete 3 lines.
    ✓u to undo, ctrl+r to redo.

    View Slide

  64. Practice!

    View Slide

  65. ✓~ to toggle upper case and lower case.
    ✓:m+ to move current line to next line.
    ✓:m-2 to move current line to previous line.
    ✓>> to add indentation.
    ✓<< to reduce indentation.
    ✓= re-format, gg=G re-format whole file.

    View Slide

  66. Practice!

    View Slide

  67. ✓yy yank the whole line of the cursor.
    ✓3yy yank 3 lines.
    ✓p paste content from register, 3p paster content
    from register for 3 times.

    View Slide

  68. Practice!

    View Slide

  69. ✓dw, diw to delete the word in the cursor.
    ✓cw, ciw same as above, but enter insert mode.
    ✓r to replace current character.
    ✓J to concatenate current line with next line, 3J
    will concatenate next 3 lines.
    ✓> to add indentation, < to remove indentation.

    View Slide

  70. .vimrc

    View Slide

  71. ✓ set history=1000 keep 1000 lines of command line
    history.
    ✓ set undolevels=100
    ✓ set ruler show the cursor position all the time
    ✓ set autoread auto read when file is changed from
    outside
    ✓ set cursorline
    ✓ set number
    ✓ set numberwidth=4

    View Slide

  72. ✓ set nobomb no BOM(Byte Order Mark)
    ✓ set clipboard+=unnamed
    ✓ set splitright always open vertical split window in the
    right side.
    ✓ set splitbelow always open horizontal split window
    below.
    ✓ set scrolloff=5 start scrolling when n lines away from
    margins
    ✓ set showtabline=2 always show tab

    View Slide

  73. ✓ set synmaxcol=128
    ✓ set viminfo= disable .viminfo file
    ✓ filetype on enable filetype detection
    ✓ filetype indent on enable filetype-specific indenting
    ✓ filetype plugin on enable filetype-specific plugins
    ✓ syntax on syntax highlight
    ✓ set hlsearch search highlighting
    ✓ set incsearch incremental search
    ✓ set ignorecase ignore case when searching

    View Slide

  74. ✓ set nobackup no *~ backup files
    ✓ set noswapfile
    ✓ set nowritebackup
    ✓ set expandtab replace with spaces
    ✓ set softtabstop=2
    ✓ set shiftwidth=2
    ✓ set tabstop=2

    View Slide

  75. to disable sound on errors…
    ✓ set visualbell
    ✓ set noerrorbells
    ✓ set t_vb=
    ✓ set tm=500

    View Slide

  76. file encoding…
    ✓ set encoding=utf-8
    ✓ scriptencoding utf-8

    View Slide

  77. ignore something…
    ✓ set wildignore+=*
    .o,*
    .obj,*
    .pyc
    ✓ set wildignore+=*
    .png,*
    .jpg,*
    .gif,*
    .ico
    ✓ set wildignore+=*
    .swf,*
    .fla
    ✓ set wildignore+=*
    .mp3,*
    .mp4,*
    .avi,*
    .mkv

    View Slide

  78. remove tailing whitespace…
    ✓ autocmd BufWritePre * :%s/\s\+$//e

    View Slide

  79. key re-map…
    ✓ map c comment
    ✓ noremap gT to previous tab
    ✓ noremap gt to next tab
    ✓ nmap v> tab to add indentation
    ✓ nmap v< shift-tab to reduce indentation
    ✓ vmap >gv
    ✓ vmap !

    View Slide

  80. key re-map…
    ✓ map 0 ^
    ✓ nmap v :tabe $MYVIMRC
    ✓ map 0 :topleft 100 :split README.md

    View Slide

  81. Plugins

    View Slide

  82. https://github.com/kaochenlong/eddie-vim

    View Slide

  83. if you want to use my .vimrc
    1. clone from my Github repo
    2. cd to cloned repo and execute install script
    3. make symbolic link for vim
    4. done!

    View Slide

  84. or if you trust me…
    1. via curl:
    sh <(curl -L https://github.com/kaochenlong/eddie-vim/raw/master/utils/install.sh)
    !
    2. via wget:
    sh <(wget --no-check-certificate https://github.com/kaochenlong/eddie-vim/raw/master/utils/install.sh -O -)

    View Slide

  85. Pathogen
    nice plugin manager
    https://github.com/tpope/vim-pathogen

    View Slide

  86. NERDTree
    A tree explorer
    https://github.com/scrooloose/nerdtree

    View Slide

  87. SnipMate
    code snippets plugin, inspired by TextMate
    https://github.com/msanders/snipmate.vim

    View Slide

  88. ctrlp
    Fuzzy file, buffer, mru, tag, etc finder
    https://github.com/kien/ctrlp.vim

    View Slide

  89. surround.vim
    quoting/parenthesizing made simple
    https://github.com/tpope/vim-surround

    View Slide

  90. vim-multiple-cursors
    Sublime Text style multiple selections for Vim
    https://github.com/terryma/vim-multiple-cursors

    View Slide

  91. powerline
    the ultimate vim statusline utility
    https://github.com/Lokaltog/vim-powerline

    View Slide

  92. rails.vim
    Ruby on Rails power tools
    https://github.com/tpope/vim-rails

    View Slide

  93. fugitive
    a git wrapper for vim
    https://github.com/tpope/vim-fugitive

    View Slide

  94. vimwiki
    personal wiki for vim
    https://github.com/vim-scripts/vimwiki

    View Slide

  95. ragtag
    https://github.com/tpope/vim-ragtag

    View Slide

  96. Practicing!

    View Slide

  97. References

    View Slide

  98. http://blog.eddie.com.tw/screencasts/

    View Slide

  99. and more practice.. : )

    View Slide