Useful vim commands - bloominstituteoftechnology/CS-Wiki GitHub Wiki
This is a collection of command examples that are commonly used by instructors here at Lambda School.
This is not a complete reference or a tutorial. It is just the commands that we commonly use in order to edit quickly. See the links for more in-depth information.
[ESC]means to hit the escape key.[RET]means to hit the return key.CTRL-xmeans hold down control andx.
See Also
- OpenVim Tutorial
- Vim Adventures
- Vim Reference Card (utexas)
- Vim Reference Card (rtorr)
- Vim Quick Reference Card (vimqrc)
Editing
I: insert at the beginning of the lineA: append starting at end of lineD: delete to end of lineC: change from here to end of linerX: replace the current character with character XR: start replacing characters from the cursor on~: change the case of a characterdd: delete (cut) the current linedw: delete the current wordd$: delete from cursor to end of linedtX: delete all characters up to the next occurrence of X in the linedTX: delete all characters down to the previous occurrence of X in the linectX: change the characters from the cursor to the next occurrence of X in the line
Moving
hjkl: move the cursor left, down, up, and right faster than reaching for the arrow keys$: move to end of line^: move to the first non-space character in the line0: move to the beginning of the linetX: move to the next X character in the lineTX: move to just after the previous X character in the linee: move to the end of the next (or current) wordb: move to the beginning of the previous (or current) wordw: move to the beginning of the next (or current) wordCTRL-f: move forward a page (page down)CTRL-b: move backward a page (page up)*: move to the next occurrence of the word under the cursor#: move to the previous occurrence of a word under the cursor%: move to the open/close brace/bracket/paren paired with the one under the cursor/foo[RET]: search for the next occurrence offooin the file?foo[RET]: search for the previous occurrence offooin the filen: repeat the last searchN: repeat the last search, but in the other direction
Markers
ma: set markeraat the cursor location (can use any letter a-z)`a: jump to markera(can use any letter a-z)
Visual Blocks
v: start a cursor-oriented visual blockV: start a line-oriented visual blockCTRL-v: start a rectangular visual block
Once a block is selected:
c: change the block to different text (with rectangular blocks, this makes one cursor per line)d: delete the blocky: yank (copy) the block:s/foo/bar/g[RET]: replace all occurrences offoowithbarin the block (this will show up as:'<,'>when you first start typing)
Search and Replace
The following use regexes:
:s/foo/bar[RET]: replace the first occurrence offoowithbaron the current line:s/foo/bar/g[RET]: replace all occurrences offoowithbaron the current line:%s/foo/bar/g[RET]: replace all occurrence offoowithbaron all lines in the file
Cut/Copy/Paste
dd: delete (cut) the current linedw: delete the current wordd$: delete from cursor to end of linedtX: delete all characters up to the next occurrence of X in the linedTX: delete all characters down to the previous occurrence of X in the linex: delete the character under the cursoryy: copy the current lineyw: copy the current wordy$: copy from cursor to end of linep: paste after cursor positionP: paste before cursor position
GUI-based vims often support COMMAND-c/CTRL-c and COMMAND-v/CTRL-v for copying and pasting from the system buffer. (Note that the system copy/paste buffer is different from that used by the y and p commands.) If you're using the console-based vim, you might have to select the text to copy with the mouse if you want to copy it to the system copy/paste buffer.
Counting
20ix[ESC]: insert 20xcharacters15~: change the case of the next 15 characters5k: move the cursor up 5 lines12yy: copy 12 lines
Saving and Quitting
:q[RET]: quit unless the file has been modified:q![RET]: quit even if the file has been modified, discarding changesZZ: save the file (only if it has been modified) and quit:qa[RET]: quit all open tabs:qa![RET]: quit all open tabs even if the files have been modified, discarding changes
Misc
.: repeat the last edit command, e.g.AHello[ESC]j.will addHelloto the end of the current and next linexp: transpose the character under the cursor with the next one; not really a special command--simplyxto cut the current character under the cursor followed bypto paste it after the cursor position!!ls[RET]: run thelscommand and insert its output into the document (can run any command):sh[RET]: spawn an interactive shell (hitCTRL-dor typeexit[RET]to get back to vim)
Tabs
- WIP
:q[RET]: close the current window or tab (if multiple windows or tabs are open)
Split Screen/Windows
- WIP
:q[RET]: close the current window or tab (if multiple windows or tabs are open)
Macros
- WIP