Demystifying vim
Mike

Mike @michaelgv

About: Full-time freelancer; Former Lead Engineer / Senior Management; speaker; 14 years in development; open for consulting and freelance opportunities.

Location:
Canada
Joined:
Oct 11, 2017

Demystifying vim

Publish Date: Aug 10 '18
46 11

Before I start, just remember, when in doubt just don't close vim and you'll live.

Exiting Vim

It's easier said than done for some, there's countless articles asking for help, here's a shortcut:

Intent: Write and Quit

If you intend to write and quit (save changes, exit), you should follow this key bindings:

  1. ESCAPE
  2. :
  3. wq
  4. ENTER

Intent: Write, but don't quit

If you intend to write and don't quit, simply run:

  1. ESCAPE
  2. :
  3. w
  4. ENTER

Editing content

You can either use the i key, or hit insert to write content in vim. If you hit r or double hit insert, it will open the replace mode.

Undo, anyone?

In order to undo, make sure you're not in write mode (hit escape), and hit the U key until satisfied.

Can I redo it?

Yep, r until satisfied.

How the heck can I fix the indentation?

A little opinionated, but if you run this command then hit F7, it'll format your code in an opinionated way:

  1. ESCAPE
  2. :map mzgg=G`z
  3. ENTER
  4. [KEY:F7]

That's all for now!

This should get you by using vim in it's most basic format!

Comments 11 total

  • Pierre-Adrien Buisson
    Pierre-Adrien BuissonAug 10, 2018

    Hum, r does not redo last undone change. I believe you meant Ctrl-R or :redo (r in normal mode would allow you to replace the char under the cursor with the char you'll type).

    • Mike
      MikeAug 10, 2018

      My mistake, I’ve got my keybindings setup for r as redo!

  • Thomas Junkツ
    Thomas JunkツAug 10, 2018

    And then, there is :x

  • Vlastimil Pospichal
    Vlastimil PospichalAug 10, 2018

    Fix the indentation:

    • gg=G for whole file
    • =G from actual row to end of file
    • == actual line
  • Rattanak Chea
    Rattanak CheaAug 11, 2018

    This needs to go to my vim cheatsheet. Is there a part 2? How can I copy and paste a section of text?

    • jerpint
      jerpintAug 11, 2018

      v Will get you in visual mode. You can highlight the section you want, then yank (copy) with y. To paste, use p. You don't need to visually select to copy/paste. You could yank the word yw, or yank to the end of the line y$ , or yank the entire line yy

      • Vlastimil Pospichal
        Vlastimil PospichalAug 11, 2018

        V will get you in select mode. It's little diferent from visual mode, but commands are similar.

        • tbodt
          tbodtAug 12, 2018

          V is visual line mode, not select mode. It's the same as visual mode but it's line oriented, so you're cutting or pasting entire lines and not characters.

  • Paul
    PaulAug 11, 2018

    If you're in a linux environment, try running the command:

    vimtutor

    In your terminal. If you're like me you'll become a vim convert

  • Ben Sinclair
    Ben SinclairAug 12, 2018

    In your example of fixing indentation you're missing the <F7> bit. Use backticks to escape code.

    Actually I hate to say it, but this tutorial is wrong in the undo section too. Use u to undo in the general case, not U. Capital U undoes a single line, and basically works in a different way (and it'll not be obvious to new users how it's working).

Add comment