Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vim for newbies

Vim for newbies

Internal talk for Mollie Development

Mattijs Bliek

July 06, 2017
Tweet

More Decks by Mattijs Bliek

Other Decks in Technology

Transcript

  1. You will spend more time editing code than
    writing code, so it makes sense to optimise
    editors for this purpose.
    Vim philosophy

    View full-size slide

  2. Opening files
    Open the directory you want in vim by going
    to the command line and typing vim {path}
    If {path} is a directory, you can open a file by
    typing :e {relative-path}.

    View full-size slide

  3. • :q = exit, error on unsaved changes
    • :q! = exit without saving
    • :w = save
    • :wq = save and exit
    How to save lives
    and exit Vim!

    View full-size slide

  4. Moving the cursor
    • Use h, j, k, l instead of arrow keys
    • Use e to go to the end of a word
    • Use b to go to the beginning of a word
    • Type the line number + gg to go to that line

    View full-size slide

  5. Modes in Vim
    • Normal mode
    • Insert mode
    • Visual mode

    View full-size slide

  6. By default you’re in normal mode, this mode
    is meant for fast navigation and commands
    that require no typing.
    Modes in Vim

    View full-size slide

  7. Pressing i takes you into insert mode. This is
    the mode you’re used to from your regular
    editor.
    Modes in Vim

    View full-size slide

  8. Pressing v takes you into visual mode. You can
    now move through the document and
    everything you come across will be selected.
    You will use this mode the least.
    Modes in Vim

    View full-size slide

  9. Pressing esc will take you back into normal
    mode from whichever mode you’re in.
    Modes in Vim

    View full-size slide

  10. While in normal mode you can execute
    operators on the text in the document. These
    have the following structure:
    (action)(target)
    Operators

    View full-size slide

  11. The action is what you want to do, such as
    delete or change a piece of text.
    The target is on what you want to
    performance the actions, such as the word
    underneath the cursor.
    Operators

    View full-size slide

  12. Actions and targets are pretty intuitive in
    Vim, here are some examples:
    c = change
    d = delete
    w = word
    p = paragraph
    Operators

    View full-size slide

  13. Let’s try to delete a word by moving to the
    beginning of the word and typing dw.
    Operators

    View full-size slide

  14. Once you learn an operator, it’s easy to know
    the related operators as well.
    So if you know you can delete a word with
    dw, and you know c is change, you can
    figure out that you can change a word with
    cw.
    Operators

    View full-size slide

  15. Sometimes you need to tell Vim more about
    what you want to delete, such as whether
    you want to delete the text in an html tag, or
    also the tag itself.
    Operators

    View full-size slide

  16. You do this by putting i (in) or a (around) in
    between the action and the target.
    Deleting the text in an html tag is done by
    typing dit.
    Operators

    View full-size slide

  17. Note this also works for other things, so I can
    empty an array by typing di[ or di].
    Operators

    View full-size slide

  18. If you know you want to repeat a command
    multiple times, you can type the amount of
    repetitions before the command.
    Moving up 10 lines is 10j
    Deleting 5 words if 5dw
    Repeating commands

    View full-size slide

  19. You can also repeat the last executed
    command by typing a .
    Say you just deleted a word with dw and
    want to delete another one, just type .
    Repeating commands

    View full-size slide

  20. You can undo stuff by pressing u in normal
    mode.
    Again notice how the names of Vim
    commands make a lot of sense.
    Undo

    View full-size slide

  21. You search a lot in Vim, since it is much faster
    to move through a document this way.
    Searching the current buffer is done by typing
    / followed by your query. Search in the reverse
    direction by using ?
    Searching

    View full-size slide

  22. You can cycle through your search results by
    using n (next). Or use N to go in the reverse
    direction.
    Searching

    View full-size slide

  23. You can also quickly find a certain character
    on the current line by typing f followed by the
    character. Search in the reverse direction by
    using F.
    Searching

    View full-size slide

  24. Vim allows you to customise all its key
    bindings. Some default bindings aren’t very
    convenient on a modern day keyboard layout,
    so we’ll remap them.
    Mappings

    View full-size slide

  25. Create and/or open ~/.vimrc
    Paste in this Gist: https://git.io/vQwOU
    Mappings

    View full-size slide

  26. There are an enormous amount of plugins for
    Vim. You can get really close to IDE level
    functionality. The best thing is you can
    customise pretty much everything, so you’re
    never stuck with the defaults.
    Plugins

    View full-size slide