Ads by Google

Monday, July 14, 2008

How not to learn Emacs!

Ever started learning Emacs and gave up?

How many times did you try?

Why?

Let me give you some reasons why Emacs learning appears to more difficult than it actually is. Basing your reasoning on one or more of the following, you're only prolonging the agony, stubborn and being ornery about it. In hindsight, it is no more and no less difficult than learning any tool.

It all boils down to, how much are you willing to unlearn and then relearn something basic? The following are all variations of the same theme over and over again.

  1. Emacs doesn't behave like vi or this/that editor. Of course it doesn't. Emacs behaves differently than most others editors in vogue currently. Every aspect of Emacs is pretty much guaranteed to be different from what you've generally used to with respect to your other editor. Obviously, not a modal editor; the GUI is skimpy when compared to the other editors; builtin interpreter; buffers and modes; weird help system; frames and windows et al.
  2. Emacs does not follow the Windows CUA UI standard of editing text. Yes, there is cua mode that provides these features but the default is certainly not one that Windows users are used to. Citing this as a reason has always been a bit puzzling for me. If vi users are way more legion than Emacs and they are willing to train themselves of h,j,k,l keys, there is some disconnect here. Why is Emacs more difficult than that? Yes, it can be incredibly frustrating with the copy paste actions that working on the Windows platform brings but it can be overcome. Trust me on that. I gave up initially because of the very reason. Too lazy to train my hunt-n-peck typing fingers to learn anew. But one day, once I made up my mind to figure out all things Emacs, I did not worry about this at all. Just took it for granted that C-y, M-y are the way to go.
  3. Emacs key-chording is weird. Again, a disconnect, people are willing to learn hotkeys, Windows shortcuts and the ilk but key chords? Emacs takes hotkeys to the extreme. :-)
  4. Emacs terminology is alien. A common complaint but if you're going to learn a new editor you might as well get used what IT is asking you to learn instead of trying to make it fit your world view. Once you learn the emacs terminology, then everything else will start falling into place. And soon you will be modifying Emacs to fit your world view.
  5. The Emacs help system is complex and bewildering to new comers. It only appears so; given time and patience in learning the tutorial and getting stuck, the help system will be a natural extension to working in any editing context. Combined with the info reader, it is simple, effective and gets what you ask for. Compared to other help systems from Borland, Microsoft, it is highly contextual given the way Emacs is crafted.
  6. Emacs use LISP. So? Not a frequent comment but I did hear it once when I remarked about it using Emacs-lisp builtin. And you can get by or defer learning or coming into contact with it as much as possible. The Emacs customize system tries to hide as much as possible for the lay user.

I should have started learning Emacs around 1996 thereabouts but ended up fulling committing to learning it only in 1999. Talk about inertia and excuses! All the above and variations is what I used. I learnt vi, nano, textpad, notepad+ or whatever, pfe or something and other editors. Finally got religion and converted to Emacs. And then when I tried to convert others, I ran into the same issues that I had.

I've noticed that the resistance is incredibly high for Windows users. The CUA stuff and the lack of shiny icons and the key chording makes it incredibly tough for them. But that is a hill that they have to climb themselves and encouragement is only that; encouragement.

If you have any of the hang ups as mentioned above, then it certainly is not the way that will get you proficient in Emacs. It only serves to limit your learning and enjoying the learning process.

Thursday, July 10, 2008

Adding Emacs to your Global Context Menu in Windows

If you are a windows user and want emacs to appear in the context menu, here's one way to do that. It involves tinkering with the registry and it creates a new instance of Emacs on every file you use it on.

Monday, July 7, 2008

Learning to use the Emacs keyboard macro system effectively

The Emacs macro system is probably the least used feature by newbies and intermediate users. Which is a shame as it's a really neat piece of code and the learning overhead is very small.

The latest versions of Emacs has the keys F3 and F4 mapped to start and end of the macro recording. And the next invocation of F4, runs the last created macro.

Recording a macro is easy. Hit F3 and start working. Of course, the idea of a macro is to do repetitive tasks and one needs to record the steps and apply it to the buffer text.

Here's where it is important to get things right.

One,don't worry about efficiency or the least number of keystrokes to achieve the task. Most times, the macros are one off and will rarely be suitable for another task later in the day or week. I've rarely had the chance to reuse any of the macros I've written. And every piece of "similar" text you receive, the odds are better that you do the macro from scratch again.

Second, use the emacs provided line commands instead of counting words, letters and spaces. Your macro is more robust if it is recorded in emacs contextual fragments i.e

C-a
C-e
M-f
M-e
C-n
C-j

etc.

Counting characters instead of using Emacs word boundary definition will lead you astray. Your macro will work for some cases and break in case of the others.

Three, once you get the hang of this, you can edit the macro by M-x edit-last-kbd-macro and delete/add additional corrections you want, and make it available by hitting C-c C-c to recompile the macro.

Try it on a couple of lines of text by hitting F4 and see where the point ends. That typically will tell you what happens if the results are not what you expected when you run the macro. You might have to add a C-n or C-j to move or create a newline.

Once you get the hang of simple macros, you can then move on to more difficult versions of the same. I've once used macros to copy stuff across 2 buffers back and forth. The more you use macros, the more natural it will be to use it for any and every repetitive tasks.

You'll find it really useful when editing UNIX shell scripts, data files and configuration files

Friday, July 4, 2008

Quickly Creating LaTeX Tables in Emacs

One of the "that sucks" part of using LaTeX is the part about creating tables. While it is powerful, the idea of all those ampersands, slashes and \hlines makes me go ape. And it takes a couple of edit-compile-view cycles to get into the groove of table writing in LaTeX.

Thankfully Emacs has the table mode as part of the standard distribution. Invoke table-mode as M-x table-insert and create the table. Answer the wizard questions and enter the data for the columns. Once you've written the data into the table, generate the LaTeX table by hitting C-^ and choosing the export format as latex.

Done.

Paste that back into your tex file and add the \usepackage{tabular} or \usepackage{tabularx} to the preamble and compile the file. Adjust the spacing by deleting off unwanted table rows.

This beats the earlier method by a large order in terms of productivity if you can't be bothered to learn LaTeX tables.

And one other tip: Create the emacs table in the tex file and comment it out. If you ever want to make extensive changes to the table, do the following.

  1. Uncomment the emacs table
  2. Move point to the table or highlight the region
  3. M-x table-recognize will make it ready to edited as an emacs table
  4. make the changes and export it again as latex
  5. comment out the emacs table
Keeping the emacs table inline saves some time in creating the table again if you ever need to make changes. Of course, you could edit the LaTeX table code but I don't venture there at unless the changes are cosmetic.

Tuesday, July 1, 2008

Jumping to specific line in a buffer

Well, with the previous versions of emacs one had to map the goto-line to some key like C-z; now you can save that binding and hit M-g g to get prompted for the line number.

How good a binding is that? I leave that to you to decide.

Saturday, June 28, 2008

What font is the text in the current buffer?

To know what the font and what the glyph codes are, use C-u C-x = on the current character the point is on. You should see something like

character: i (105, #o151, #x69, U+0069)
charset: ascii (ASCII (ISO646 IRV))
code point: #x69
syntax: w which means: word
category: a:ASCII graphic characters 32-126 (ISO646 IRV:1983[4/0]) l:Latin
buffer code: #x69
file code: #x69 (encoded by coding system windows-1252-unix)
display: by this font (glyph code)
-outline-DejaVu Sans Mono-normal-r-normal-normal-16-120-96-96-c-*-iso8859-1 (#x69)

It can also be invoked as M-x describe-char.

Wednesday, June 25, 2008

pgfplots: Plotting in LaTeX

pgfplots looks like a promising package for getting plots into LaTeX. I should probably give it a spin. The manuals, in PDF, here and here are detailed and extensive. And the plots are lovely; beautiful. (There, I get all nerdy)

One reason that I'd like to learn this package is that the alternate approach is to use gnuplot to generate a jpeg and then include that into LaTeX file. That comes with a whole bunch of issues like fiddling with fonts to match the LaTeX font scheme one uses, scaling the jpeg image to fit page and colours.

Since pgfplots is another package with the TeX system, it should be technically be a case of simpler and smaller dependencies for plots. And it uses the same pgf package that is part of the larger beamer package developed by Till Tantau. If you're a MikTeX user, it is available as a download from the package manager.

Saturday, June 21, 2008

Common Emacs lisp idioms

This page by Xah Lee might be useful if you're into elisp programming.

Wednesday, June 18, 2008

Changing the default mode of the *Scratch* buffer

The default *Scratch* buffer in Emacs starts in fundamental mode which might not be useful for lots of people who either don't know or want to work on elisp.


If text mode makes sense for you as the default, the following addition to the .emacs file should fix it.

(setq initial-major-mode 'text-mode)

Note that you don't lose much if you're trying out a few lisp forms as they can be eval'ed anywhere by hitting C-x C-e that would have the same effect as doing it in lisp mode.

Sunday, June 15, 2008

Creating animations in PDF using LaTeX

If you ever wanted to simple animations of your graphs or pictures in your LaTeX paper then the animate package is the way to go. The documentation and examples are good to get you started.