VI keyboard shortcuts

  • This is timely - I wrote an ed guide today and this post is a good opportunity to share it. I've been working on a remote server via ILO-in-a-browser with broken console definitions and a screen that's too small for the interface. It's a gift - I've wanted to be fluent in ed for a long time and spent the time to get there today.

    If anyone has something similar for console-mode sam usage I'd love to know that too.

    Anyway - the guide:

        Ed doesn't push any of the fancy cruft that modern
        editors do - full screen views of the text in question
        or fiddly mouse interfaces.
        
        It is line based, scriptable, it works *everywhere*
        that unix runs, and the default configuration is
        predictable. A true power tool from the days of yore
        when men were men and each tool did one simple thing
        extremely well.
        
        How to get into ed and create some text:
        <pre>
        $ touch my_filename
        $ ed my_filename
        0
        a
        Here is some text
        next line
        more
        yet more
        The dot on the next line indicates I'm done inserting text. Then save and exit.
        .
        w
        q
        $ 
        </pre>
        
        Basic commands:
        * a: append to the first line
        * p: print the current line
        * n: print the current line with a line number (handy)
        * %n: list all lines
        * 1n: list line one
        * 1,5n: line lines one to five
        * 5,$n: list lines five until the end
        * 2d: delete line two
        * 3,4j: merge lines 3 and 4
        * 2i: insert text to a new line before the current line 2
        * P: enable prompt
        * %l: - that's a lower-case letter after 'k' not the
          number one. Print all lines, including end-of-line
          markers (useful in dos2unix settings among others).
        
        Regular expressions (this is where it gets exciting):
        * 2s/next/another/: change the word 'next' on line
          two do 'another' (don't forget that trailing slash)
        * g/e/: find all the lines containing the letter 'e'
        * g/e/n: find all the lines containing the letter 'e'
          and then print them out with their line number next
          to them.
        * %s/e/q/: change the first instance of the letter 'e'
          to 'q' on all lines
        * %s/e/q/g: change all instances of the letter 'e' to
          'q' on all lines
        * 1,5s/e/q/3: change the third instance of the letter
          'e' to 'q' on all lines
        * %v/for/n: print out the lines that do NOT contain
          the word 'for'
        
        Other magic:
        * 1,4y: cut ('yank') lines 1 to 4
        * 5x: paste to after line 5
        * 1,3t6: Transfers lines one to three to a position
          after line 6.

  • Is there something special about this? I imagine you'd find equivalent content on dozens of pages by searching for "vi commands".

  • Poor.

    No real mention of modality or ranges, only a selection of commands. To call VI commands "keyboard shortcuts" is a bit of a misnomer - they're not shortcuts, they are its interface.

  • I prefer the PDF format "Reference Cards" you can print and pin up next to your desk.

    * http://www.digilife.be/quickreferences/QRC/Vi%20Reference%20...

    * http://www.digilife.be/quickreferences/QRC/vi%20Quick%20Refe...

    * http://www.digilife.be/quickreferences/QRC/VIM%20Quick%20Ref...

    I am of course from the Emacs camp, so here are some emacs ref cards to complement.

    * http://dept-info.labri.fr/~idurand/enseignement/Rebondir/ema...

  •   d	Delete text. (see explanation above)
    
    And where is the explanation?

  • Most of that I am pretty automatic on. But one thing that I notice when I'm coding is that I don't really know many commands to make it easier to handle everything in between two brackets or parenthesis or what have you. Sure, there is the '%' key. But are there other useful commands as far as that goes?