Vim - wwangdev/wwangdev.github.io GitHub Wiki

Vim

Need to Know

  • Two modes:
    • Insert mode
    • Command mode
  • Use h,j,k,l to move in Command mode
    • h: left
    • j: up
    • k: down
    • l: right
  • o: insert a line under; O: insert a line above
  • w XXX: save as XXX
  • q: quit
  • /XXX: find XXX forward, n: next
  • ?XXX: find XXX backward, n: next
  • :set ignorecase/noignorecase: set no/case-sensitive.
  • Replace:
    • :%s/xxx/XXX/[c,e,g,i]: replace xxx by XXX for all
    • :1,4s/xxx/XXX/[c,e,g,i]: replace xxx by XXX for line 1 - 4
    • c,e,g,i
      • c: ask before replace
      • e: no error message
      • g: replace all in lines (usually enable)
      • i: not case-sensitive
    • Programming:
      • :syntax on
      • :set autoindent
      • :set tabstop=4
      • in ~ directory, vim .vimrc and write the config content
      • use gcc to compile
      • use gcc -o XXX to compile to XXX.out
      • ./a.out to see result

Vim Common Commands

Mode Switch

  • a -- insert after cursor
  • i -- insert at cursor
  • o -- insert a line under the cursor
  • Esc -- enter command mode
  • : -- enter command line mode

Move the Cursor

  • h -- left
  • l -- right
  • j -- down
  • k -- up
  • ^ -- line begin
  • $ -- line end
  • G -- file end
  • Gg --file begin
  • W -- next word
  • B -- previous word
  • Ctrl+f -- next page
  • Ctrl+b -- previous page
  • 5k -- up 5 lines

Copy, Paste and Delete

  • x -- delete the character at cursor
  • dd -- delete the line
  • D -- delete the rest of line from cursor
  • d -- delete (use with Move the Cursor)
  • yy -- copy the line
  • y -- copy (use with Move the Cursor)
  • P -- paste

Undo and Redo

  • u -- undo
  • Ctrl+R: redo

Search and Replace

  • :/string -- search string forward
  • ?/string -- search string backward
  • s/pattern/string -- replace pattern by string

Save and Exit

  • :w -- save
  • :w filename -- save as filename
  • :q -- exit
  • :q! -- exit without save

Compile Relative

  • g++: cpp files
  • gcc -c: preprocessor, compile and assembly. Generate .o file
  • gcc -S: preprocessor, compile. Generate .s assembly file
  • gcc -E: preprocessor. Generate result to std output
  • gcc -g: generate debug info.

Compile Optimization

  • -Onum: -O2 is good enough
  • -march: compile for specific CPU. EX: gcc -O2 -march=pentium4 summary.c

Debug with gcc

  • Start to Debug
    1. use gcc -g XXX.cpp to generate debug file
    2. gdb a.out
  • get help: help
  • list (l): list source code
    • l num: nearby 10 lines from num
    • search/reverse-search XXX: display the line of XXX
  • break point
    • break 10: set a break point at line 10
    • info break: break info
    • clear: clear the current line break point
  • run and step
    • run (r): run to the next break point
    • next (n): run to the next line
    • n num: run to the next num lines
    • continue (c): continue to the next break point
    • step (s): run to the next line (step into)
  • watch
    • print (p) XXX: print the value of XXX
    • watch XXX: break when XXX value is changed
  • set: set var i=1: set i=1
  • stack: bt
  • exit: quit (q)