A little foreword on learning vim:
You have two modes, command mode and edit mode. In command mode, you issue these commands by simplying typing them. To get into command mode no matter where you are, you'd press ESC. There are several keys to start directly editing the text, those are the buffer editing modes. There are numerous ways to traverse the text in a file, like the arrow keys but more efficiently. Those are the cursor moving commands. There are many commands to actually modify the text in a buffer by means of cutting, and pasting. Those are buffer manipulation commands. There are also "long" commands that simply wouldn't fit because there aren't enough keys on the keyboard. These commands are also used typically with vim extentions (www.vim.org). Long commands are done by going into command mode (ESC) and then typing :. Once you've typed : you'll notice a mini-prompt appear in the bottom left of the screen, and this is your long command buffer.
- commands:
- cursor moving:
- w = next word
- b = beginning of word (also acts as back)
- e = end of word (acts as next, but at end of next word)
- h = left
- j = down
- k = up
- l = right
- t( = put cursor at the next ( at insert position
- T( = put cursor at previous ( at insert position
- t[character] = put cursor at insert position for character
- f( = put cursor at next ( at append position
- F( = put cursor at previous ( at append position
- f[character] = put cursor at append pos for previous char
- buffer manipulation:
- dd = cut line
- d2d = cut 2 lines
- d50d = cut 50 lines
- d[anynumber]d = cut anynum lines
- yy = copy line
- y2y = copy 2 lines.. etc
- p = paste copy buffer
- dw = cut word
- d2w = cut next two words
- db = cut previous word
- d2b = cut previous 2 words
- d$ = cut until end of line
- d^ = cut until beginning of line
- dt( = cut until next paren
- dT( = cut until previous paren at insert
- r( = replace current character with (
- r[character] = replaces current character with [character]
- >> = tab in line
- << = tab back line
- 2>> = tab in next 2 lines
- . = do last succession of vim commands (i.e. if you appended, typed the word bla, then hit esc. press the down arrow and . it will append bla to the end of the next line)
- buffer editing:
- a = append
- A = append at front of line
- i = insert
- I = insert at end of line
- o = insert at beginning of next line
- O = insert at beginning of previous line
- misc commands:
- gg = beginning of doc
- GG = end of doc
- ctrl-d = page down
- ctrl-u = page up
- ctrl-f = screen forward
- ctrl-b = screen back
- n = next search result
- N = previous search result
- u = undo
- ctrl-r = redo
- v = visual mode (this lets you select a block of text, for example using arrow keys)
- ESC = brings you into command mode if you weren't already
- long commands:
- search and replace:
- :s/search/replace/ = search/replace current line
- :%s/search/replace/ search/replace all lines
- :s/search/replace/g = g means global (i.e. if there is more than one instance on the same line, replace both instances on that line.
- :s/search/replace/i = case insensitive
- :s/search/replace/gi = global and case insensitive
- :%s/search/replace/gi = global and case insensitive search and replace that searches the whole file
- :/search = search for search text. press n to go to next result
- saving, opening, and quitting:
- :e [filename] = opens filename. tab completion works
- :b[number] = go to buffer[num]. if you have mulitple files editing (i.e. you type vim *.java or you :e a few files)
- :w [filename] = write filename. just :w writes the current buffer. filename is optional in that case
- :q = quit
- :wq = write and quit
- :q! = force quit
- :wq! = force write and quit