Skip to content

Instantly share code, notes, and snippets.

@Edderic
Last active August 29, 2015 14:01
Show Gist options
  • Save Edderic/5bda849ec949137f6162 to your computer and use it in GitHub Desktop.
Save Edderic/5bda849ec949137f6162 to your computer and use it in GitHub Desktop.
Tips & Snips

Terminal:

  • OPTION + LEFT / RIGHT skips words backwards/forwards.
  • CTRL + k deletes everything at the right of the cursor.
  • CTRL + u deletes everything at the left of the cursor.

Vim:

  • in Command Mode:
    • gg/G goes to start/end of file.

      • 39gg moves cursor to line 39.
      • 39G moves cursor to line 39.
    • H/M/L moves to top/middle/bottom of screen.

    • f/F character finds forwards/backwards a character.

      • 3ft looks for the 3rd 't' after the cursor.
      • dft delete up to 't'
    • t/T character goes to cursor posititon adjacent to the character.

    • ;/, repeats command regarding f and t forwards and backwards.

    • yanking:

      • ye copies until the end of word (doesn't copy extra whitespace)
      • yt) copy from cursor until parenthesis.
      • Y/yy copies the whole line.
    • text objects:

      • caw 'change around word'
      • cis change inner sentence
      • cas change around sentence (like cis but also deletes extra whitespace
      • diw delete inside word. Deletes word under cursor (excluding whitespace).
      • daw delete around word. Deletes word under cursor (including whitespace).
      • dG delete until the end of file.
      • dgg delete until the start of file.
      • ~ change case of char(s) under cursor.
      • I move to the first non-blank character in the line, then go to insert mode.
      • Visual mode:
        • 2as selects two sentences.
    • w/W moves to the beginning of the next word.

      • 3d2w will repeat d2w 3 times.
    • e/E moves to the end of next word.

    • g + e/E moves to the last end of word/WORD.

    • b/B moves backwards, with cursor at end of word.

    • x/dl deletes what's under the cursor.

    • X/dh backspace.

    • D/d$ deletes from cursor until the end of line.

    • C/c$ deletes from cursor until the end of line and then goes to insert mode.

    • s/cl deletes the character under cursor, and goes to insert mode.

    • S deletes the whole line and then goes to insert mode.

    • r some_character replaces char under cursor with some_character

      • r <Enter> will replace a character with a line break.
      • 4r <Enter> will replace 4 characters with a line break.
    • R replace mode.

    • A appends end of line.

    • a appends after cursor.

    • p/P pastes before/after cursor (line, if in visual block mode).

      • xp swaps two characters.
    • Vp pastes to current line.

    • . repeats the last change. Works for all changes except undo, redo, and : commands.

      • Use case example:
        • /some_search_string searches for the nearest occurence of the search string.
        • r<Enter> replace char under cursor with some line break.
        • n goes to the next instance of the search.
        • . repeats the last change (i.e. r<Enter>).
    • v enter visual mode.

      • o/O goes to start/end of selection (works for visual block mode, too).
    • <CTRL>v shifts to visual block mode.

      • Great for editing tabular data!
    • #/* searches backwards/forwards for word under cursor.

    • % jumps between matching pairs([], etc.)

      • 50% moves cursor halfway to the end of file.
    • CTRL + T goes back to last buffer.

    • :! + some_external_command executes the external command.

    • :r <Filename> inserts the file contents to buffer.

    • :r !ls reads the output of the ls command and puts it below the cursor.

    • :<some_partial_name_of_vim_command> <CTRL + D> will show the list of commands that start out with the some_partial_name_of_vim_command string

    • Telling where you are:

      • :set ruler displays the absolute line number and horizontal position of cursor.
      • :set number displays absolute line numbers.
      • CTRL + G displays current file path.
    • Scrolling:

      • CTRL + f/ b moves up/down a whole screen.
      • CTRL + u/ d moves up/down half a screen.
      • CTRL + e/ y moves up/down 1 line.
      • z + t/z/b positions line where cursor exists to top/mid/bottom.
    • Searching:

      • :set ic/noic turns on/off ignore case of the searched string.
      • :/blah\c will search for "blah" with ignored case.
        • /<Up> shows the previously searched item.
        • */# goes to the next/last occurence of the item under the cursor.
          • can be prepended by a count.
      • Searching for whole words
        • /the will include words such as 'the', 'there', etc.
        • /\<the\> will include 'the', but not 'there', nor 'atheist'.
      • Highlighting matches:
        • :set hls/nohls turns on/off highlighting of the searched string.
        • :nohls disables the highlighting (until the next search)
      • Tuning search:
        • :set incsearch/nois displays the matches as you type the string to be searched/disables incremental search.
        • :set nowrapscan stops going back to the top after the file reaches the end.
      • Search patterns:
        • /^the will match string 'the' that start out in the beginning of a line.
        • the$ will match string 'the' that end out in the beginning of a line.
        • /c.m will match strings such as 'camera', 'community', etc.
          • after\. will match strings 'after.'
      • Marks:
        • '' or `` moves the cursor backwards and forwards.
          • CTRL + o/i jumps cursor to even earlier/later positions (even across files), without setting marks!
          • :jumps gives you a list of lines to jump to.
        • Named Marks:
          • m<some_char> marks the spot of the cursor.
            • if some_char is uppercase, mark becomes global (accessible in all files.)
          • `<some_char> goes to the mark associated to some character.
          • :marks displays all the named marks.
            • :marks MCP shows lines associated to marks 'M', 'C', 'P'.
          • Special marks:
            • ' was the previous location before the last jump.
            • " was the cursor position before leaving the file.
            • [ was the start of the last change.
            • } was the end of the last change.
            • . position where you last made the change.
        • :marks shows list of marks.
    • Configuration:

      • :map shows all mappings.
        • might be a good idea to add mappings for common stuff.
          • Wrap word with parenthesis, curly braces, or '[]'
    • Editing more than one file.

      • :edit <filename> opens the file in the current buffer.
        • Vim will close the file and open the new one.
      • :write/:w writes the file.
      • :edit! <some_file> discards the changes and edits the new file.
      • :hide edit <some_file> edits some_file, but not write changes in the current file yet.
      • in the terminal, vim file-a file-b file-c
        • :next/:n or :previous/:p go to the next/previous file.
        • :next!/:n! discard changes of current file and go to the next.
        • :wnext/:wprevious to save current file and go to the next/previous file.
      • :args shows you where you are.
      • :args file1 file2 file3 redefines files to be opened.
        • <CTRL>-^ lets you move quickly from file to file.
          • Note: Doesn't change the idea of where you are. Only commands like :next, :previous do.
      • Using registers:
        • "fyas saves yanked-around-sentence to 'f' register.
        • "e3Y saves 3 lines to 'e' register.
        • "fp pastes whatever is saved onto the 'f' register.
        • CTRL-V_jjwb"ay go Visual mode, do stuff, save to register 'a'.
        • "wdaw delete a word and save to register 'w'.
    • Writing to a file

      • :write >> logfile appends to file called logfile.
      • SHIFT-Vjj:write appends to file called logfile, the two selected lines.
    • Viewing a file

      • vim -R file edits the file in read-only mode. Can save if :w!
      • vim -M file opens the file strictly in read-only mode. Unmodifiable.
      • While in viewer mode:
        • :set modifiable
        • :set write
    • Changing the file name

      • :edit file
      • :saveas file renames the open file to 'file' and saves it.
      • :file file renames the file to 'file' without saving it.
    • Splitting Windows

      • Split a window
        • :split/:sp
        • :vsplit/:vsp
      • Close a window
        • :close
      • Closing all other windows
        • :only
      • Split a window on another file
        • :split file
        • :new will open a horiz split on a new file.
      • Window Size
        • :3sp file opens the file with 3 lines as height.
        • CTRL-W10 +/- increases or decreases size by 10. {height}CTRL-W _ sets the window height to a specified num of lines.
      • Vertical splits
        • :vsplit
        • :vsplit file
        • :vertical new/:vnew
      • Moving between windows
        • CTRL + any one of hjkl to move around.
        • CTRL-Wt moves to the TOP window.
        • CTRL-Wb moves to the BOTTOM window.
        • CTRL-W w/W cycles to the next/previous window.
      • Moving windows
        • CTRL-W HJKL
      • Commands for all windows
        • :qall quits all
        • :qall! quits all (forcefully, if needed).
        • :wall writes all
      • Opening a window for all arguments.
        • vim -o a b c d will open files a, b, c. d onto horizontal splits.
        • vim -O a b c d will open files a, b, c. d onto vertical splits.
      • vimdiff
        • ...
      • various
        • :leftabove {cmd} left or above the current window
        • :aboveleft {cmd} idem
        • :rightbelow {cmd} right or below the current window
        • :belowright {cmd} idem
        • :topleft {cmd} at the top or left of the Vim window
        • :botright {cmd} at the bottom or right of the Vim window
      • Tab pages
        • :tabedit/:tabe file
        • g t/T go to next/previous tab.
        • tab split makes a new tab page with one window that is editing the same buffer as the window we were in.
        • :tab help gt opens a new help gt tab.
        • :tabonly closes all tab pages except the current one. Unless there are unsaved changes in other tab pages.
    • Making big changes

      • Record and playback commands
        • q{register} starts recording keystrokes. {register} must be in the range of 'a-z' * Type your commands.
        • @{register} to execute the commands.
          • 3@l, for example, executes the command(s) associated with the register 'l'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment