

Top ten essential Emacs tips  - edw519
http://web.psung.name/emacstips/topten.html

======
fallintothis
Not to troll -- indeed, I am attempting to be constructive -- but in case
anyone was interested in (what I know of) vim's equivalent functionality,
here's what I have so far:

1) Several options:

    
    
      :help <something>
      :h <something>
    

Shift-K to run a program to lookup the keyword currently under the cursor (the
program defaults to man). On that note, you could just

    
    
      :! man <whatever> 
    

if so compelled.

2) qq = record macro to register q. @q = replay macro at register q. 100@q =
repeat macro q 100 times. @@ = repeat the previous @<whatever>.

The author notes that you can tell Emacs to "please repeat this macro until it
would cause Emacs to start beeping", which I found interesting. I'm not sure
if there's a specific way to do this in vim; I often just go with, e.g.,
1000@q (or whatever arbitrarily large number you want. You can check how many
lines are in a file, among other statistics, with g<ctrl-g>). So long as the
macro ended with a j (or whatever motion you wanted, technically) it would
continue carrying down the lines. It seems as though you could be able to
execute macros on some chunk of text, perhaps with visual line select. Hm...

Another interesting feature the author notes is "counters let you insert a
different number every time a macro is run." I've had some annoying
experiences bending over backwards for this, so I'd like to know an easier
way, but it's certainly possible using the C-A and C-X keys, which increment
and decrement a number under the cursor, respectively.

3) Ctrl-v for visual block mode (remapped to Ctrl-q for :behave mswin, since
Windows captures this as Paste); then, simply move around with the regular
keys and perform fancy editing to your heart's content. I use this often, for
instance, to insert comment marks in a chunk of text. Go to the first column &
line of the block, ctrl-v, hold down j till the region reaches the last line
you'd like, then shift-I, type your comment characters (e.g., "//" or ";" or
"#"), then <esc> and the insert takes effect on all lines of the previous
region.

Also useful during the many adventures in the different visual-select modes,
use gv to re-select the previous visual area, should it happen to become
deselected. For instance, when I am holding down j in the previous example and
prematurely hit Shift before letting go of j, it joins the lines up. Argh! u
(undo) then gv to continue on my way.

4) When I first read about this, I was immediately interested. For similar
functionality (couldn't claim whether or not it was the SAME, strictly, as I
don't actually use Emacs), there's the extension that I now adore: YankRing.
<http://www.vim.org/scripts/script.php?script_id=1234>

5) I tend to use :diffsplit, though there are other ways to work with this. As
per item 1, :help diff.

6) Sad to say I'm at a loss here -- not to say vim couldn't do this, but
rather I've never needed to do this in my range of experiences (clearly
limited compared to that of the author's). If anyone could fill this in, I'd
be interested.

7) The few times I work with a language that doesn't have a shell:

    
    
      :make
    

Though there are parameters and options to tweak with. For instance, which
program to use by default, whether or not to create a temp file for the
errors, jumping to the first line containing an error, etc. As stated, I do
most of my work in languages with shells anyways, so I'm not an expert with
this command. :help make, for more info.

8) Admittedly not built-in (being against vim's Do One Thing Right philosophy,
I suppose), but the plugin I use for this is vcscommand:
<http://www.vim.org/scripts/script.php?script_id=90> with a Mercurial backend,
since the base plugin is for CVS, SVN, SVK and git:
<http://www.vim.org/scripts/script.php?script_id=1898> Then, it's a matter of
:VCSVimDiff, :VCSCommit, :VCSAdd, which you can remap to whatever keystrokes
you like by editing your .vimrc (see item 10).

9) Again, I've never done such things, so if anyone could step in for our
fellow vimmers, that'd be great.

10) You can edit your .vimrc to remap keys as you'd like -- including binding
them differently per mode you're in.

Taking some silly examples from my .vimrc:

    
    
      "Maps ctrl-enter in insert mode to open a line below your current line:
      imap <C-CR> <esc>o
    
      "Maps ctrl-enter in normal mode to simply o, in case my muscle memory goofs and
      "doesn't notice I'm in normal mode:
      nmap <C-CR> o
    

You can also set abbreviations, which will expand as you type:

    
    
      "Fix typo
      abbr teh the
    
      "automatically expand {{ into a curly-brace block, the idea being to hit Enter
      "after typing the {{
      ab {{ {<cr>}<esc>kA

~~~
PieSquared
I've been using Vim for about a year and a half now and actively trying to
learn it well, and I think I am somewhere near knowing most of what it can do.
Not all, of course, but definitely most. However, I've been thinking about
trying Emacs as well, because unlike Vim (it seems) it has a [more] complete
scripting language and a few other neat things. On the other hand, it's hard
to give up a year's worth of experience of learning one editor, just to stop
using it and go to another one.

Does anyone have significant experience in both, enough to know the
peculiarities and such of each editor? I'd love to hear what you have to say
on how they compare. Is learning Emacs worth it any more than learning
everything Vim has to offer? Since I plan on using this for many years, I'd
like to choose the optimal tool, even if it means wasting a year of
experience.

~~~
silentbicycle
I used vi (vim) for about five years, before switching to emacs (gnu emacs),
which I've used for about two. They're both excellent text editors, but are
clearly optimized for different things, and have tremendous differences in
interface, extensibility, and culture.

The big advantage for Emacs lies in being a sort of Lisp virtual machine. It's
extensible enough to be nearly an OS in its own right (certainly a "desktop
environment" or whatever), and has decades of extensions already available for
it. Where vi(m) has two fundamental modes (with replace being a submode of
insert, I suppose), emacs has modes specific to most tasks -- if you want a
mode that integrates with an external Common Lisp (slime), OCaml (tuareg), or
Ruby (ruby-mode), it's there. This doesn't just mean syntax highlighting and
indenting, it means more abstract things like being able to insert the type
signature for a Haskell function under the cursor, jump to the definition of
an object's superclass, or check/edit/archive your email. (For a really good
example, check out this screencast of Marco Baringer using Slime:
<http://common-lisp.net/~mbaringer/slime.torrent> ) Emacs Lisp is not the
greatest as Lisp implementations / dialects go, but it has a lot of advantages
for extending an editor compared to something like Python or Perl. (I have a
suspicion Smalltalk or Forth would work well, though.)

I know that vim is extensible, but typically you process a region by typing
:%!some_cmd to pipe it back and forth; most integration between vim and
external programs that I've observed has been at the level of piping text (in
the quintessentially Unixy way). This loses a lot of deeper context that could
better tie things together.

The main strength of vi, I think, is in its ubiquity. While I use emacs to do
most things, I also have a heavily customized environment that I carry around
with me. (It's under version control, etc.) I can be extremely productive with
it, _once I've moved in_ , but vi is available everywhere. I'm talking about a
basic vi (nvi?) with one level of undo and no syntax highlighting, perhaps,
but it's still pretty good for editing text, particularly if you're a touch-
typist and the interface has become second nature. It edits text. It doesn't
tie things together into one harmonious environment, but it edits text, and
it's really efficient once you know it.

For what its worth, there's an editor called "mg" which has the default emacs
keyboard config out of the box, but is not really very programmable. While I'm
very familiar with the basic keyboard controls, it feels crippled to me. I use
it while doing computer setup stuff, because it's in the default OpenBSD
install, and it's a decent pager, but it's really not the same.

Anyway, that's my experience with each. Emacs seems far more powerful in the
long run, but it takes quite a bit more time to learn to use it to anywhere
near its full capacity, and doing so will rely on carrying around your
configuration. (It also has a relatively alien culture and vocabulary, which
takes some getting used to.) Vi does what it does everywhere, but cannot grow
indefinitely to fit your needs. It's focused on just being a good text editor,
rather than a full environment. If you're talking years, though, I would
definitely give emacs a shot.

(Incidentally, I think emacs is quite reasonable under the Dvorak keyboard
layout (better than qwerty, I think), while vi feels far more awkward. The
hjkl keys get scattered badly, for one thing. That, a growing fascination with
Lisp, and being utterly blown away by the Slime screencast were why I
ultimately switched.)

~~~
PieSquared
Thanks. That was probably the first time I have heard an honest comparison of
the two editors (well, honest-seeming at least ;-P), although I've been
looking for one for a while. Also, I think you just got me to give Emacs a
try. Hopefully I'll still be able to switch into Vim-mode at need (heh, so now
my life is modal too... Curse you, Vim, you have invaded my soul!) Anyway,
thanks.

~~~
silentbicycle
There's viper-mode to emulate vi, but the first real roadblock is probably
learning how to explain to the built-in help system what you're trying to
figure out.

C-h t (that's Control-h, let go of control, t) will open up a thorough
tutorial. Also, <http://emacswiki.org> has a lot of info at all levels of
expertise.

------
silentbicycle
Two more quick tips:

1\. The "kill ring" (what other interface cultures might call a clipboard) is
a circular stack, not just one slot. If you try to paste ("yank") and discover
you cut/copied over what you meant to paste, you can hit alt-Y (or M-x yank-
pop) to replace it with successively earlier things. When you reach the end,
it loops back to the newest.

I think it has a stupid name, but it's really useful.

2\. Learn the terminology. Emacs came of of a very different user interface
culture, and sometimes feels like it came from a parallel universe. The
documentation (info pages, apropos, etc.) become much easier to search when
you know what various parts of the interface are actually called. I don't know
of any "Emacs to vernacular Windows/Mac/Linux interface cliche" glossary, but
reading through the documentation, even what should be the really basic parts
(e.g. "how to open a file", the tutorial) will help you acclimate. ("find-
file", buffer, mark, "major mode", etc.) It helps that the Emacs manual is
available on the web -- that way, people don't need to figure out how to
navigate info pages first. :)

~~~
jrockway
_I think it has a stupid name, but it's really useful.

2\. Learn the terminology._

You wouldn't think the name was stupid if you followed your second piece of
advice. A ring is a data structure that's like a queue, but after it fills up,
the old elements go away. The kill ring is called such because it's a ring
that stores killed text.

~~~
silentbicycle
The kill/yank names rub me the wrong way because where "cut and paste" suggest
X-acto knives and glue sticks to me, that is, editing text on paper, there's
no metaphoric grouping between "kill" and "yank". It also makes expressing the
act of copying awkward. Copying is called "kill-ring-save", which is a kind of
terse "kill-it-but-don't-actually-kill- it-just-copy-it-as-if-you-did". It
further breaks down any metaphoric concept tying them together.

I'm not arguing that "copy/cut/paste" are better terms because they're already
used by most people, but rather because they collectively express the
operations in terms of an existing, analogous, and likely familiar idea. (I
think the "ring" part of it is perfect, though.)

------
jrockway
Uh yeah, this:

    
    
       (global-set-key "\C-cs" 'shell)
    

is deprecated now. You need to say:

    
    
       (global-set-key (kbd "C-c s") 'shell)

------
Paperflyer
Here is an interesting thing:

Search google.com/trends for Emacs and Vim: It suggests that in the last few
years, both editors have lost users, but Emacs lost them faster than Vim.
While Emacs started out with twice the popularity of Vim, Vim is now stronger.

Personally, I tried them both for some weeks each, but in the end settled with
TextMate and WriteRoom, as both Emacs and Vim somehow felt too clunky for me.

------
julianb
Regular expression search and replace is also killer.

~~~
ajross
Yes. Although if you're still learning regexes, I'd suggest staying away from
the emacs implementation. It's different from perl's (or pcre, which is the
library that everything not perl actually uses) syntax, which is the one you
_really_ want to know. There are a bunch of annoying incompatibilities.

~~~
silentbicycle
M-x re-builder can help you learn them interactively, though.

~~~
davidw
Wow, that's a cool one... I love that about emacs: even after years of using
it, I'm still learning lots of cool things.

------
sophist
1) Use vim instead.

