Skip to content

Instantly share code, notes, and snippets.

@suzmas
Last active April 26, 2017 01:36
Show Gist options
  • Save suzmas/1f9f0ca4beb9d7dcebd859b8b8bbbda1 to your computer and use it in GitHub Desktop.
Save suzmas/1f9f0ca4beb9d7dcebd859b8b8bbbda1 to your computer and use it in GitHub Desktop.
Vim commands I use / find helpful while learning

Vim for Me

  • Set cwd to current directory:

    :cd %:p:h

  • Set args to all the files in cwd

    :args **/*

  • Delete all pattern matches in file

    :g/MATCH BEGIN/ .,/MATCH END/d

  • Delete pattern match in many files and save all changed Make sure your :args are set to the correct files (check this with just :args )

    :argdo g/MATCH BEGIN/ .,/MATCH END/d | update

  • Vsplit multiple files at once

    vim -O <file1> <file2> <file3> <etc...>

  • Find & replace all instances of whole word in file

    :% s/\<WORD TO FIND\>/NEW WORD/g

@sslampa
Copy link

sslampa commented Apr 25, 2017

Delete, Cut, Copy, and Paste

Delete / Cut

  • Delete n characters forward / Delete n characters backward
    nx / nX

  • Delete (can be chained with movement keys) / Delete from cursor to end of line
    d / D

    • Example: dw -> deletes next word
  • Delete then enter insert mode (can be chained with movement keys) / Delete from cursor to end of line then enter insert mode
    c / C

  • Delete whole line / Delete whole line and enter insert mode
    dd / cc

Copy

  • Copy with movement key
    y

    • Example: yw -> copy next word
  • Copy current line
    yy

  • Copy selected text
    v (enters visual mode) + (movement keys), then y to copy selected text; OR
    ny -> where n is number of characters

  • Copy multiple lines
    V (enters visual mode) + (movement keys), then y to copy selected lines; OR
    nyy -> where n is number of lines

Paste

  • Paste forwards / Paste backwards
    p / P

@sslampa
Copy link

sslampa commented Apr 26, 2017

Other Stuff

  • Insert after next character / Insert at end of line:
    a / A

  • Insert at cursor / Insert at beginning of line:
    i / I

  • Insert at end of word:
    ea

  • Change / Delete based on character:
    ct<char> or dt<char> -> change/delete up to given next character (exclusive of character)
    cf<char> or df<char> -> change/delete from cursor to given next character (inclusive of character)

    • Sample text: " I'm text - " => dt- Deletes all text up to -, whereas df- deletes all text including -
  • Change / Delete inner match within current line:
    ci<char> or di<char> -> change/delete inside matched pair (exclusive of character)
    ca<char> or da<char> -> change/delete around matched pair (inclusive of character)

    • Sample text: " [This is some text] " => ci[ Changes text inside '[ ]', whereas ca[ changes all text including '[ ]'
    • These only work for paired characters like ' ', " ", { }, [ ], ( ), , < >
    • There's also a really handy one cit or dit. This says change/delete inner tags.
      • Sample: <h1>Hello Suz!</h1>, will remove "Hello Suz!"
  • Repeat last command:
    . (just a period)

  • Insert line below / Insert line above:
    o / O (Use this these a lot)

@sslampa
Copy link

sslampa commented Apr 26, 2017

Different Ways To Save and Quit

ZZ
:wq
:x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment