Web www.grok2.com
grok2.gif (391 bytes)

 

 

emacs for vi users

I created this page for those C programmers who, like me, are being forced to learn how to use the emacs editor after years of using the Unix standard vi editor. I will focus on how to do common things that you do when coding in C with the vi editor. Note that while there may be more than one way to do something, and sometimes an easier way to do something, the information I provide is more often a straight forward translation of how you would do things in vi in terms of how you would do the same thing using the emacs editor. Also what is here is the bare minimum to get you started. Unlike in vi, in emacs you don't have to enter a "insert" mode to add text -- what you type appears immediately on screen.

If you are a vi fan and have been experimenting with vi clones (vim, nvi and the rest), then you need to check out the VIPER package in emacs. Written by Michael Kifer, it's cool, like vi on steroids.

Open/Save/Quit

Action Vi Emacs
open file :e Ctrl - x Ctrl - f
save file :w Ctrl - x Ctrl - s
quit editor :q Ctrl - x Ctrl - c

Move Cursor

Action Vi Emacs
left h Ctrl - b
down j Ctrl - n
up k Ctrl - p
right l Ctrl - f
back one word b Esc - b
foward one word w Esc - f
beginning of line ^ Ctrl - a
end of line $ Ctrl - e
page up Ctrl - b Esc - v
page down Ctrl - f Ctrl - v
jump to line number 'n' nG Esc - x goto-line RET n

Delete Text

Action Vi Emacs
delete char x Ctrl - d
delete word dw Esc - d
delete line dd Ctrl - k Ctrl - k
delete 'n' lines ndd Esc - n Ctrl - k

Undo

Action Vi Emacs
undo u Ctrl - x u

Yank/Place

Action Vi Emacs
yank 'n' lines nyy Ctrl - SPACE, move cursor upto the first character of the line next to last line you want to yank, Esc - w
place (emacs calls this yank -- yank from the copy buffer) p or P Ctrl - y

Match Parenthesis

Action Vi Emacs
match paranthesis % This is one vi feature I am addicted to and which made the change to emacs tough because emacs does not have a direct way to do this even under it's various programming modes. Below is some emacs lisp code that I picked off from the Internet that will map the % key to do exactly what vi does. A big thanks to whoever wrote this. (Thanks to Eric Pement (Sed FAQ) for pointing out that this is from the Emacs FAQ). What you need to do is open the file .emacs under your home directory and stick the following lines as is in the file. Close and re-open emacs and, presto, you can use the % like you do in vi. Note that I have not tested this extensively, but it seems to work.
  (global-set-key "%" 'match-paren)
    (defun match-paren (arg)
      "Go to the matching paren if on a paren; otherwise insert %."
      (interactive "p")
      (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
            ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
            (t (self-insert-command (or arg 1)))))

Search

Action Vi Emacs
search forward / Ctrl - s (text incremental search)
search backward ? Ctrl - r (text incremental search)

Books & References

Emacs Web-Site
Emacs Manual
Emacs Lisp Introduction
Emacs Lisp Manual
GNU Emacs Pocket Reference, Authors: Debra Cameron, Pub: O'Reilly
Learning GNU Emacs, Author: Debra Cameron, Bill Rosenblatt & Eric Raymond, Pub: O'Reilly
GNU Emacs Manual, Author: Richard Stallman, Pub: Free Software Foundation

Glossary

RET: Many commands in emacs bring up a prompt line at the bottom of the editor screen (like the ":" command line at the bottom of the vi editor) where you need to provide additional information after which you press the Enter/Return key to cause the command to act.
Esc: Unlike the Alt/Ctrl/Meta/Shift key combinations, Esc key combinations require that you press and release the Esc key before you press the next key. On Unix workstations, you may be able to use the Meta key instead of the Esc key (in which case, you would keep the Meta key pressed when pressing the next key in the key combination).

On NTEmacs (GNU Emacs compiled for Windows 9x/ME/NT/XP/2K) you can use the ALT key for Esc key combinations (keeping the ALT key pressed when keying in the next key in the combination).

Emacs Lisp: Emacs is extremely powerful because it can be customised to behave any way you want it to. Emacs Lisp is the programming language that you use to customise emacs.

Site Links
  The Books I Own
  Main Page
  Vi in Emacs
  Linux on Vaio
  Study NZ
  Utilities
  Programming Fun?
  SED FAQ
  C Language
  Source Code Browsers
  C Struct Packing
  Walt Disney World
  PPP RFCs
  FSM/HSM
  Tcl/Tk
  Photographs of Flowers
  Random Photogaphs
  Put this on your site!
  SQLite
  The Sundial Bridge
  Repetitive Strain Injury (RSI)
  Selling Software Online (MicroISV)
  Tcl Tk Life-Savers
  The Experience Shows!
  Green Tips
  .htaccess tricks
  Web-Site Development Online Tools
  Blog
 

 

 

 


Site copyright of domain owner. All rights reserved.