Skip to content

Instantly share code, notes, and snippets.

@danman01
Created January 25, 2019 19:15
Show Gist options
  • Save danman01/41bb288d68e49aa2d8ad7aa1d186c07c to your computer and use it in GitHub Desktop.
Save danman01/41bb288d68e49aa2d8ad7aa1d186c07c to your computer and use it in GitHub Desktop.
vim notes
vim
Vim is a text editor available on most unix systems, and can easily be installed via package managers if not installed.
Great for working in the terminal.
Learning curve, but improves speed overtime.
Terminal based text editor is the tool of choice for most professional coders (vim or emacs).
My reasoning: why work with Atom, when you can work with Vim? Vim will always be there, always improving. Available on all systems, to all devs around the world (ssh into a server, use vim)
Basic commands
- Goal: don’t use the mouse!
- keystrokes only improves speed
let’s edit .vimrc (called a dotfile - configuration file in home dir)
Moving around:
Down - h
Up - j
Left - h
Right - l
or use arrows
Command mode:
shift + ;
type commands in bottom left.
:mycommand
get to previous command by pressing up arrow
Insert Mode:
i
Visual mode:
v
Exit modes by pressing Escape, denoted as <Esc>
Useful Movement commands:
move to end of line: $
move to beginning of line: ^
look familiar? Regex commands…learning vim helps with other tools
move to end of file: <S-g> (read as Shift + g)
move to beginning of file: gg
hover over a file name, gf goes to file
Split windows:
<Ctrl-w> v - splits vertical
:split also works for horizontal
:vsplit works for horizontal
:split another_file opens split from another file
<Ctrl-w> w - jumps between splits
<Ctrl-w> arrow - jumps to certain direction
increase size of window: <Ctrl-w> +
:close or :q will close a window
:new opens a new window
more:
:help, then select the section, below the getting started
Editing text:
from normal mode:
w - go to next word
b - go to previous word
x - delete char under cursor and copy to clipboard
y - copies to clipboard
* use with modifier:
* yw copies word;
* y5w copies 5 words;
* yy copies entire line
p - paste from clipboard from default registry
- change registry with “somekey
- “* is the system registry
- ex: “ayy
- puts the current line in the registry “a”
- “ap pastes from registry “a"
- “byy puts current line in registry “b"
d - delete modifier
- dd deletes entire line
- dw deletes word going forward
- db delete word going back
numbers, like 5, are modifiers too:
5dw deletes the next 5 words
5b moves back 5 words
5 + down arrow moves down 5 lines
u - undo
<Ctrl r> - redo
from insert mode:
enter insert mode by pressing ‘i’. Exit insert mode by pressing <Esc>
Edit like normal
Search:
/ (forward search)
? (backward search)
n - next result
Find and replace
within command mode, :, type:
%s/term/newterm/gc
here, gc means global and confirm. leave off ‘gc' to just find and replace the first term. leave of ‘c’ to not confirm your replacements.
recording:
http://vim.wikia.com/wiki/Recording_keys_for_repeated_jobs
To start recording, press q in normal mode followed by a letter (a to z). That starts recording keystrokes to the specified register. Vim displays recording in the status line. Type any normal mode commands, or enter insert mode and type text. To stop recording, again press q while in normal mode.
To playback your keystrokes, press @ followed by the letter previously chosen. Typing @@ repeats the last playback.
mapping keys to commands:
http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1)
plugins:
https://github.com/VundleVim/Vundle.vim
Bundle for Vim
Reference:
Cursor movement
* h - move left
* j - move down
* k - move up
* l - move right
* w - jump by start of words (punctuation considered words)
* W - jump by words (spaces separate words)
* e - jump to end of words (punctuation considered words)
* E - jump to end of words (no punctuation)
* b - jump backward by words (punctuation considered words)
* B - jump backward by words (no punctuation)
* 0 - (zero) start of line
* ^ - first non-blank character of line
* $ - end of line
* G - Go To command (prefix with number - 5G goes to line 5)
Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
Insert Mode - Inserting/Appending text
* i - start insert mode at cursor
* I - insert at the beginning of the line
* a - append after the cursor
* A - append at the end of the line
* o - open (append) blank line below current line (no need to press return)
* O - open blank line above current line
* ea - append at end of word
* Esc - exit insert mode
Editing
* r - replace a single character (does not use insert mode)
* J - join line below to the current one
* cc - change (replace) an entire line
* cw - change (replace) to the end of word
* c$ - change (replace) to the end of line
* s - delete character at cursor and subsitute text
* S - delete line at cursor and substitute text (same as cc)
* xp - transpose two letters (delete and paste, technically)
* u - undo
* . - repeat last command
Marking text (visual mode)
* v - start visual mode, mark lines, then do command (such as y-yank)
* V - start Linewise visual mode
* o - move to other end of marked area
* Ctrl+v - start visual block mode
* O - move to Other corner of block
* aw - mark a word
* ab - a () block (with braces)
* aB - a {} block (with brackets)
* ib - inner () block
* iB - inner {} block
* Esc - exit visual mode
Visual commands
* > - shift right
* < - shift left
* y - yank (copy) marked text
* d - delete marked text
* ~ - switch case
Cut and Paste
* yy - yank (copy) a line
* 2yy - yank 2 lines
* yw - yank word
* y$ - yank to end of line
* p - put (paste) the clipboard after cursor
* P - put (paste) before cursor
* dd - delete (cut) a line
* dw - delete (cut) the current word
* x - delete (cut) current character
Exiting
* :w - write (save) the file, but don't exit
* :wq - write (save) and quit
* :q - quit (fails if anything has changed)
* :q! - quit and throw away changes
Search/Replace
* /pattern - search for pattern
* ?pattern - search backward for pattern
* n - repeat search in same direction
* N - repeat search in opposite direction
* :%s/old/new/g - replace all old with new throughout file
* :%s/old/new/gc - replace all old with new throughout file with confirmations
Working with multiple windows and files
https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html
If you want, you can probably do everything from one vim session! :) Here are some commands to turn one vim session (inside one xterm) into multiple windows.
:e filename - edit another file
:split filename - split window and load another file
:ctrl-w s - splits window horizontally (same as :split)
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w - move cursor to another window (cycle)
ctrl-w_ - maximize current window (horizontal)
ctrl-w= - make all equal size
10 ctrl-w+ - increase window size by 10 lines
ctrl-w v - split vertially (same as :vsplit)
ctrl-w < or > - increase size left or right (vertical split)
ctrl-w 5< or 10> - increase size by number modifier left or right (vertical split)
:vsplit file - vertical split
:sview file - same as split, but readonly
:hide - close current window
:only - keep only this window open
Buffers:
* :b someOpenBuffer - switch to open buffer
* :bnext (or :bn) - go to next buffer
* :bprev (of :bp) - go to previous buffer
* :bd - delete a buffer (close a file)
* :sp filename - Open a file in a new buffer and split window
* :ls - show current buffers
:b 2 - open buffer #2 in this window

* Another good vim commands cheatsheet and a vi introduction using the "cheat sheet" method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment