vim text editor

what we'll do

background

  • old-school text editor (vi released in 1976, vim in 1991)
  • available in every version of Linux/UNIX machine

background

  • these days you can still find vim practically everywhere
  • available as a plug-in in almost every IDE and text editor
    (atom, sublime, eclipse, intellij, visual studio code)

#editorwar

why use vim?

  • it's very powerful (regex, macro)
  • it's very fast (because you hardly ever use the mouse)
  • it's available almost everywhere
  • it's super hard to learn (no, it's not)

modal editor

  • two modes: edit mode and insert mode
  • live demonstration:
    • opening file
    • insert mode (i)
    • edit mode (esc), move around with h, j, k, l
    • save (:w), quit (:q)
  • that's it, you can start using vim

demonstration

  • insert ( i ) and append ( a )
  • insert new lines above ( O ) and below ( o )
  • go to line ( gg )

demonstration

  • moving a line of code ( dd, p )
  • moving lines of code ( <n>dd, p )
  • copying lines of code ( <n>yy, p )
  • moving around ( $, ^, % )  
  • combining y and d with $, ^, %
  • visual mode ( v )
  • repeat last action ( . )

demonstration

  • change a word ( cw )
  • change a character ( r )
  • delete a character ( x )
  • change indentation ( >> and << )
  • search ( /string, n and N )
  • replace ( :%s/old/new/g )
  • jump points ( m<key>, ~<key>)

and that's it!

  • you can find the list of commands that I often use in the next few slides 
  • don't expect to be good in weeks, or even months, but if you persist, I think the payoff is massive
  • learn to type fast > learning vim 

resources

  • run vimtutor from command line
  • online vimtutor: http://www.openvim.com/
  • cheat sheet: https://vim.rtorr.com/
  • use google
  • this presentation ... ?

starting commands

  • add a number before a command to repeat it
  • u and ctrl+r (undo and redo)
  • i (insert), a (append), o and O (insert line below/above)
  • gg (go to), yy (yank/copy), dd (delete)
  • ^, $, % (move to start, to end, to bracket), combine with y, d
  • / to search, n to go to next match, N to go to previous match
  • m<key> to mark, ~<key> to jump to mark
  • ctrl+n to complete string

the three kings

  • %               - go to corresponding bracket
  • <n>gg       - go to line n
  • ^                - go to first (non-blank) character in line
  • $                - go to the end of line
  • /<string>  - search for string and jump to it
  • there are other keys like g_, 0, G, {, }, w, b, but I rarely use them

moving around

  • u, ctrl+r     - undo and redo
  • ctrl + n      - string completion
  • .                 - repeat last command
  • x          - delete a single character
  • r          - replace a single character
  • cw       - replace word
  • dd       - delete a line (<n>dd deletes n lines)
  • d$, d^ - delete until end of line, delete until start of line
  • yy       - yank (copy) a line (<n>yy copies n lines)
  • y$, y^ - yank until end of line, yank until start of line
  • p         - paste whatever is in the buffer (after you delete/copy)
  • >>       - move line to the right (<< to move left), 
  • v         - visual mode (you can highlight bunch of text, then press
                  y, x (or d), r,

editing

insertion

  • i       - insert before cursor
  • a       - insert after cursor
  • o       - insert line below cursor
  • O      - insert line above cursor

register

  • :reg      - show register
  • "<n>p  - paste contents of register n
  • "<n>y  - (after visual), yank contents to register n

marking

  • m<key>   - place a marker, e.g. ma
  • ~<key>    - jump to marker, e.g. ~a

search and replace

  • /<pattern>
  • n  - search forward
  • N - search backward
  • :%s/<old pattern>/<replacement>/g

saving and quitting

  • :w     - save the file
  • :w!    - force save (e.g. read only file)
  • :q      - quit ... and you can guess what :q! is
  • :wq  - save and quit
  • :e      - open a file (don't use :o)

others

  • :set autoindent (automatically indent)
  • :set tabstop=4 (set tabs to 4 spaces)

Introduction to VIM

By Daniel Sutantyo

Introduction to VIM

#acm

  • 270