Posts tagged with vi

The people who brought you man tmux decided to throw in, as a bonus, the perfect sparse intro to vi keyboard shortcuts versus emacs keyboard shortcuts.

   Function                     vi              emacs
   Back to indentation          ^               M-m
   Bottom of history            G               M-<
   Clear selection              Escape          C-g
   Copy selection               Enter           M-w
   Cursor down                  j               Down
   Cursor left                  h               Left
   Cursor right                 l               Right
   Cursor to bottom line        L
   Cursor to middle line        M               M-r
   Cursor to top line           H               M-R
   Cursor up                    k               Up
   Delete entire line           d               C-u
   Delete/Copy to end of line   D               C-k
   End of line                  $               C-e
   Go to line                   :               g
   Half page down               C-d             M-Down
   Half page up                 C-u             M-Up
   Jump forward                 f               f
   Jump to forward              t
   Jump backward                F               F
   Jump to backward             T
   Jump again                   ;               ;
   Jump again in reverse        ,               ,
   Next page                    C-f             Page down
   Next space                   W
   Next space, end of word      E
   Next word                    w
   Next word end                e               M-f
   Paste buffer                 p               C-y
   Previous page                C-b             Page up
   Previous word                b               M-b
   Previous space               B
   Quit mode                    q               Escape
   Rectangle toggle             v               R
   Scroll down                  C-Down or C-e   C-Down
   Scroll up                    C-Up or C-y     C-Up
   Search again                 n               n
   Search again in reverse      N               N
   Search backward              ?               C-r
   Search forward               /               C-s
   Start of line                0               C-a
   Start selection              Space           C-Space
   Top of history               g               M->
   Transpose chars              xp              C-t

Thanks, Nicholas Marriott ⟨nicm@users.sourceforge.net⟩!

For the very bare bones skip this section and the next one. Or just read this.

When I was first learning to program there were a number of layers of complexity besides just the material in the tutorial I was looking at. For example,

  • becoming familiar with linux (what’s /usr/share/lib/? And you say something about sudo apt-get install programname but also gksudo synaptic &?)
  • And there are different “flavours” of Linux? Oh, gawd. (stick with ubuntu for starters. Plenty of fun to be had in the years to come exploring arch, slackware, teenpup)
  • shorthands people use to explain what to do at the command line (what’s the difference between a # and a $?)
  • git
  • github
  • apache, node.js
  • /etc/httpd.conf
  • how to compile C programs
  • linking versus compiling versus lexing
  • regexes
  • learn a few quick perl one-liners
  • python vs ruby vs C vs J vs Haskell vs PHP vs C++ vs R vs Java vs Javascript vs….
  • Rails vs Django vs Zend vs CakePHP vs Mojolicious vs ….
  • What’s the difference between Javascript and Java? (Answer: They’re as similar as “car” and “carpet”.)
  • ruby gems, ruby version manager
  • libraries, API’s, includes, headers, callbacks
  • pointers, dereferencing pointers
  • hash is not at all like a hashing function but it is a dictionary
  • pure virtual functions, encapsulation, scope
  • functional programming, object-oriented programming, design patterns, monads
  • OAuth, PGP, SHA1, MD5sum, SSH-keygen
  • states, events, data structures
  • and this mysterious editor, vi (should I be using emacs or something?).

Programmers spent a lot of time talking about ancillary stuff like their “toolchain” or HAML or SCSS or tmux or oh-my-zsh. If you spend 8 hours a day interfacing with text: then one or more of these tools, learnt gradually, can eventually save a lot of time — but as a newbie learning the difference between -> and => (they’re NOTHING alike), it’s very easy to get distracted by the stuff that experienced hackers are getting hot flashes over, and the core fundamentals that you need to make the computer spit out a useful result rather than another error to google on.

Add to that, the programmer who’s just fallen in love with tmux and is therefore blogging about it is with near certainty going to say: “It takes time to learn, but do it! It will save you in the long run.” Here’s my advice: don’t do it. Zed Shaw agrees; he says gedit and nano are the right tools for beginners. You don’t need a $500 bluegrass guitar pick that was the only kind Earl Scruggs would touch the first time you pick up a guitar. You can do just fine with your frikkin thumb.

Nevertheless, someday it does become appropriate to start easing into vi or another “advanced” editor and these are, I think, good gradual first steps. If you can immediately type always-as-easily and sometimes-quicker in vi than gedit, then you’ll be able to stick with vi and gradually pick up tricks and tips. Which is the right way to do it; not buying 10 pragprog books on toolchain elements before you’ve successfully written a 'Hello World'.

I always used nano or gedit because vi was scary and I wanted to just get to work. What follows is the minimum instructions I would have needed to know to start writing in vi. The minimal instructions are preceded by a backgrounder.


What the heck is all of this vim, vi, ex, ed, sed, emacs, nano, gedit crap?!

  • vi is a text editor from back in the days of 800 baud modems. Unlike the ajaxy fast response times of the tumblr browser, back then sending and receiving a message like “" "ok, you said 'down key'. I have moved the cursor down one line” would take considerable time. So the author of vi invented a way to say “1-3-1-↓”. “OK, it’s as if you said 'down key' 131 times.” Without having to press 131 keystrokes. vim = vi improved, is a later update to vi. I’m just going to talk about vi.
  • sed, ed, and ex are line editors — like Word’s “find and replace” function but with more options.
  • ex and ed are really old and I haven’t used them.
  • there are even older tools still sitting around in the default unix toolbox — if you play around with Arch Linux (recommended after you’ve run Ubuntu for a while), you will see these programs hanging around the innards.
  • emacs and vi are favourite text editors for programmers. These are old tools that are considered to have aged well. Plus you can program in front of a black screen with green type that looks like the Matrix and feel like a bad#ss.

Similarity. Both emacs and vim are plain-text editors—like Notepad, not like Word—that are meant to make plain-text editing very fast. Unlike an IDE (jedit, vis basic, kompozer, texniccenter, gnutexmacs, kile, …) they do not suggest program-language keywords as you type.

So whereas I now use vi to write client reports and business plans (to be compiled in LaTeX, something else that took me a while to learn but is now paying dividends), I would not use an IDE for that because an IDE is just for programming.

Difference. Briefly, the difference between emacs and vi / vim is:

  • vi is default-installed on every machine
  • emacs is something you customise to infinity on your own machine, to the point that when you’re typing a blog post in a tumblr window you wish you had the emacs interface

If you skipped ahead, start reading here.

Open a text file by typing vi textfilename.txt or vi programname.perl at the command line.

The first question you probably have when you open vi is: wtf! I am typing words and nothing is appearing! is this a text editor or not? aaack! some weird function just happened for no reason!

Here is what’s going on. vi has two modes: a command mode and a typing mode. The command mode is so you can do commands like find-and-replace, save, jump back to where I just was in the file, or delete 5 lines. The insert mode is a normal typing mode.

  • To enter typing mode, type i whilst in command mode.
  • To exit typing mode, type <esc> whilst in typing mode.

One more thing and you are ready to start using vi. To save a file and close vi, type :wq.

That is enough to get you going. But I will also leave you a few more key commands that give a hint as to why vi might be preferable to nano. (Typically both are installed on any server you ssh into, so you have your pick.) All of these are to be typed from “command mode” i.e. not typing mode i.e. hit <esc> before trying to execute any of these.

  • :%s/[firm name]/Jed Underbaugh Properties LLC/g will replace all instances of “[firm name]” with “Jed Underbaugh Properties LLC” Yay, form letters!
  • w and b will take you forward/back a word, like ctrl+← or ctrl+→ in Word.
  • db will delete the previous word like <ctrl+backspace> in word.
  • <ctrl+o> and <ctrl+i> will take you back to “where was I in this long document? it was several pages ago…”
  • /flower will search for the string “flower”. little n takes you to the next instance of “flower”, big N takes you to the previous instance of “flower”. This is like ctrl+F in Word.
  • j and k move up and down lines. It’s surprising how the deciseconds you save by not moving your hand back and forth from the arrow keys add up into seconds and eventually into minutes over the course of the day.
  • You should type :ino jk <enter> :ino kj <enter> at the beginning of your vi session on a remote terminal, or edit the file called ~/.vimrc on your own machine to include the lines :imap jk <Esc> and :imap kj <Esc>. This way you don’t have to reach over to hit the <esc> key all the time, which negates the time savings mentioned above. Just type jk or kj.
  • d8d deletes 8 lines.
  • d5b deletes the previous 5 words (like hitting <ctrl+backspace> 5 times in Word).
  • u is the “undo” button. (Ctrl+Z in Word)
  • ctrl+R (capital R) is the “redo” button.

You can learn a lot more over the course of a few sessions by typing vimtutor at the command line. That brings up a well-written tutorial. I worked through it very gradually whilst getting “real stuff” done.