Skip to content

Instantly share code, notes, and snippets.

@henrywang
Last active February 25, 2019 10:10
Show Gist options
  • Save henrywang/fcc681f96301fdf2e29d98d513f5e881 to your computer and use it in GitHub Desktop.
Save henrywang/fcc681f96301fdf2e29d98d513f5e881 to your computer and use it in GitHub Desktop.

act, repeat, reverse

  • gj, gk move down and up by display lines
  • w, b commands target the start of a word
  • e, ge commands target the end of a word
  • f{char} Forward to the next occurrence of {char}
  • F{char} Backward to the previous occurrence of {char}
  • t{char} Forward to the character before the next occurrence of {char}
  • T{char} Backward to the character after the previous occurrence of {char}
  • I tend to use f{char} and F{char} in Normal mode when I want to move the cursor quickly within the current line
  • I tend to use the t{char} and T{char} character search commands in combination with d{motion} or c{motion}
  • d/abc : delete search abc
  • m{a-z} : marks are local to each individual buffer
  • m{A-Z} : marks are globally accessible
  • ’{mark} : moves to the line where a mark was set, positioning the cursor on the first non-whitespace character
  • `{mark} : moves the cursor to the **exact position where a mark was set
  • `` : Position before the last jump within current file
  • `. : Location of last change
  • `^ : Location of last insertion
  • `[ : Start of last change or yank
  • `] : End of last change or yank
  • `< : Start of last visual selection
  • % works with (), {}, and []
  • jump rule : The sentence-wise and paragraphwise motions are jumps, but the character-wise and word-wise motions are not
  • (/) : Jump to start of previous/next sentence
  • {/} : Jump to start of previous/next paragraph
  • H/M/L : Jump to top/middle/bottom of screen
  • gf : Jump to file name under the cursor
  • C-] : Jump to definition of keyword under the cursor
  • gf : go to the filename under the cursor
  • `> End of last visual selection

files

  • C-^ : quickly toggle between the current (%) and alternate files(#)
  • :w[rite] : Write the contents of the buffer to disk
  • :e[dit]! : Read the file from disk back into the buffer (that is, revert changes)
  • :qa[ll]! : Close all windows, discarding changes without warning
  • :wa[ll] : Write all modified buffers to disk

window

  • w : Cycle between open windows
  • c : Close the active window
  • o : Keep only the active window, closing all others

tab

  • The :lcd {path} command lets us set the working directory locally for the current window. If we create a new tab page and then use the :lcd command to switch to another directory, we can then comfortably scope each tab page to a different project. Note that :lcd applies locally to the current window, not to the current tab page. If we have a tab page containing two or more split windows, we could set the local working directory for all of them by running :windo lcd {path}.
  • if the current tab page contains more than one window, we can use the T command, which moves the current window into a new tab page
  • :tabc[lose] : Close the current tab page and all of its windows
  • :tabo[nly] : Keep the active tab page, closing all others

:*do

  • :windo, :argdo, :bufdo, :tabdo
  • :cdo[!] {cmd} - Execute {cmd} in each valid entry in the quickfix list. (:Rg foo, :cdo s/foo/bar/ge | update) (e means suppress any errors)
  • :cfdo[!] {cmd} - Execute {cmd} in each file in the quickfix list. (useful changing multiple matches in the same file)
  • :ld[o][!] {cmd} - Execute {cmd} in each valid entry in the location list for the current window.
  • :lfdo[!] {cmd} - Execute {cmd} in each file in the location list for the current window.

file

  • % : the filepath of the active buffer (/path/with/file)
  • :h : removes the filename while preserving the rest of the path
  • %:h : the path in which the current file located
  • :e. : Open file explorer for current working directory
  • :E : Explore Open file explorer for the directory of the active buffer
  • C-g : echoes the name and status of the current file
  • if we specify a filepath that doesn’t correspond to an existing file, then Vim will create a new empty buffer. Cannot save it because the filepath does not exist. Solution:
    • :!mkdir -p %:h
    • :write
  • save file as a super user : :w !sudo tee % > /dev/null

normal

  • If I’m in Insert mode with my cursor at the end of a line, the quickest way to open a new line is to press . And yet I sometimes prefer to press o just because I anticipate that I might want that extra granularity from the undo command
  • The C-a and C-x commands perform addition and subtraction on numbers. When run without a count they increment by one, but if we prefix a number, then we can add or subtract by any whole number. For example, if we positioned our cursor on a 5 character, running 10C-a would modify it to read 15.
  • when an operator command is invoked in duplicate, it acts upon the current line. So dd deletes the current line, while >> indents it.
  • R : enter into replace mode

insert

  • C-w : Delete back one word
  • C-u : Delete back to start of line
  • C-[ : Switch to Normal mode
  • C-o : Switch to Insert Normal mode
  • C-r{register} : pasting a few words from Insert mode
  • C-r= : This opens a prompt at the bottom of the screen where we can type the expression that we want to evaluate

visual

  • v : Enable character-wise Visual mode
  • V : Enable line-wise Visual mode
  • C-v : Enable block-wise Visual mode
  • gv : Reselect the last visual selection
  • o : Go to other end of highlighted text
  • we should prefer operator commands over their Visual mode equivalents when working through a repetitive set of changes
  • the deletion should affect all marked lines simultaneously, but the insertion affects only the topmost line (at least for the duration of Insert mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment