Category Archives: Text Editing

It’s All Text! Add-on for Firefox

It’s All Text! is one of my most-used Firefox add-ons and recently it’s been upgraded to run on 3.0b4.

Edit textareas using an external editor, because it’s all text!

Right click on a textarea, select “It’s All Text!” and edit the text in the editor of your choice.

Alternatively, click on the edit buttons added for your convenience. Right click on the edit buttons for even more options, including preferences.

There are good reasons why you want to use your own favorite text editor to do text editing, and this add-on makes the process extremely easy (no cut and paste needed). But my initial reason was to avoid the “back” button: on my ThinkPad, it’s right next to the arrow keys and if you happen to hit it by mistake, whoosh, all your text is gone…

Highly recommended.

P.S. Of course this post and many others are written using it. :P

Word Capitalization in Emacs and PowerPoint

One of my favorite keyboard shortcuts in PowerPoint is Shift-F3. It rotates the capitalization of the current word among lowercased, Capitalized and UPPERCASED. Note that you do not have to select the word, and the cursor does not have to be at the beginning of the word either(*).

But the title of this post clearly said Emacs, didn’t it? Well, since I missed this feature in Emacs, I just implemented it in a pinch and bound it to Shift-F3. :P The code is in this page in EmacsWiki. I have only tested it on GNU Emacs, and so I can only hope that it works in XEmacs.

(*) Actually, you rarely have to select a word in PowerPoint in order to format it. Commands like Bold Ctrl-B and Italic Ctrl-I will act on the current word when there is no selection. Also, similar behavior is in fact present in all Office applications. I just picked PowerPoint.

Goto Last Change in Emacs

From an interesting idea in gnu.emacs.bug…

How many times have you found yourself mosying thru a file when you wonder, where the heck was I just editing? Well, the best you can do is hit undo, ^F, and undo again, to get back.

… comes a very handy function in Emacs—get this elisp file and bind a key to goto-last-change-with-auto-marks. (I bind it to F4, but of course it’s up to you.)

I note that my typical usage is a bit different from the post above: I usually remember what I was just editing, but I am reviewing a different part of the file. This feature allows me to jump back to my editing point quickly.

I must have used it so frequently that I just pressed F4 in another editor… and that’s how I realize I should share it here.

P.S. You may also be interested in this bit of advice for a slightly different experience:

(defadvice goto-last-change-with-auto-marks (before mav-goto-last-change activate)
"Split the window beforehand to retain the current view"
(unless (eq last-command 'goto-last-change-with-auto-marks)

Debugging Bad \hboxes with AUCTeX

When you use AUCTeX to compile a LaTeX document that contains an error, the compilation will fail and AUCTeX will remind you in the minibuffer area: LaTeX errors in `*foo output*'. Use C-c ` to display. By pressing C-c `, which invokes the next-error function, you can navigate to the source locations of the errors and fix them one by one. It surely is one of the most convenient features of AUCTeX.

But AUCTeX also has a lesser known feature in this regard: by pressing C-c C-w, which by default binds to TeX-toggle-debug-boxes, you can tell AUCTeX whether the debugger should consider underfull/overfull \hboxes as errors too. When preparing for the final(*) revision of a document, this feature really makes locating the bad \hboxes a breeze!

(What’s remaining are of course the \vboxes, but there doesn’t seem to be any good way to locate them using the output messages alone.)

(*) It is usually not a very productive idea to fix bad boxes before the document content is finalized.

LaTeX Source Specials

As I have hinted in this post about pdfopen, it is possible to do some sort of round-trip LaTeX editing with the right tools. There are two directions to make the round-trip complete.

The forward jump is editor-to-previewer: given the current cursor location in the editor, jump to the corresponding paragraph in the previewer. The backward jump is symmetrical. The magic behind this is called “source specials”.

While it sounds somewhat like a Chinese dish with special sauces, it really isn’t. :P Instead, when you generate the DVI file, you ask latex to tag the DVI file with “source specials”. Imagine each word in the DVI file is tagged with the source filename and the line number of the word.

The forward jump is to ask the previewer to display the location that is right after the editor’s cursor location (filename and line number). The backward jump is to ask the editor to put the cursor closest to the current location in the previewer (usually the location of a double-click).

Let’s get to the example. I will show how to get round-tripping to work in Windows using MiKTeX distribution 2.4 and GNU Emacs. Other configurations are similar.

(Round-tripping between LaTeX and PDF is similar in principle, but the tools are not very mature yet. I will save this for a later post.)

  • Install the AUC TeX package into Emacs.
  • To enable tagging in latex, invoke it with the --src-specials option. Say the primary LaTeX source is main.tex, AUC TeX invokes it this way, :
    latex --src-specials \nonstopmode\input{main.tex}

  • To do the forward jump with YAP (the DVI previewer in MiKTeX), say when the cursor is on line 65 of hetero.tex, AUC TeX will execute:
    yap -1 -s65hetero.tex main.dvi

  • To enable the backward search, install the gnuserv package. Google can help you locate many links, like this and the rest.
  • Assuming you have gnuclientw installed on your path, in YAP, go to View->Options->Inverse Search. Inside Program combo, you should see “GNU Emacs (Single Instance)”. Example command line:
    gnuclientw.exe -F +%l "%f"

  • Note that installing gnuclientw addresses the problem in this comment. In general, you rarely want to invoke emacs directly. Use gnuclientw instead.

Have fun!

Long Word Completion

It has been complained to me that I use long variable names in my code, which I think is a defendable practice. Worse, I also use stupidly long “abbreviations” in LaTeX

\newcommand{\LovaszLocalLemma}{Lov\'asz Local Lemma}

which doesn’t abbreviate at all…

Well, these complaints are understandable, but what if you don’t have to type these long words in full?

Type this_super_very_super_long_word into Emacs. Press Enter. Now type th and then hit Alt-/. Bang!

I can’t imagine how many seconds of my life I have saved with this feature (surely more than the time it takes me to compose this post to share it with you). These days, even when I type moderately long word like moderately, I use completion.

Vim users don’t have to feel left out. Try Ctrl-P and Ctrl-N instead.

P.S. The next complaint I will get is that Alt-/ is not all that easy to hit. I agree. That’s why I have this line in my .emacs:

(global-set-key [(control ?h)] 'dabbrev-expand)

Your key choice may vary.

Emacs Local Variables

If you use Emacs to edit your LaTeX files, then you want to know about Local Variables. They are “comments” that you write at the end of a file, say in this example:

blah blah

% Local variables:
% mode: latex
% TeX-master: "main.tex"
% End:

The purpose of local variables is to allow you to give Emacs extra customization when you load a file. In this example, we want Emacs to switch to latex mode and set the TeX-master variable to “soda.tex”. mode is actually a special variable controlling the major mode of the file. The TeX-master variable is used by AUCTeX so that when you invoke LaTeX on the current buffer (Ctrl-C Ctrl-C), it will instead invoke LaTeX on the “master” file. This is crucial when you break the paper into multiple files and use \input to string them together because this alleviates the need to switch to the master file before you invoke LaTeX.

There is also another way to specify local variables by putting them in the beginning of a file. Here is how our example would look like:

% -*- mode: latex; TeX-master: "main.tex"; -*-

blah blah

There is also a special local “variable” call eval. The value for that variable is simply evaluated as an expression, and hence it can actually be somewhat dangerous. Fortunately Emacs also provides enable-local-eval to let you catch the evals.

Happy TeXing in Emacs!

RefTeX Mode in Emacs

In a secret mission, last week I forced myself to write a 20-page document with TeXnicCenter, a Visual-Studio-like text editor for LaTeX. It was a rather painful experience given its current BibTeX support. Here is the feature list for this (quote) “extremely feature rich” editor. (Perhaps I missed the bib support feature somewhere in this feature list?)

One feature I like about TeXnicCenter is its Navigator (screenshot), which is a tree widget showing the section structure of the document. In Visual Studio, this would be showing the class structure. It is very useful for navigating between sections and can help somewhat when inserting labels.

And that reminds me that a couple years ago I have blown my officemate away by showing him how to use RefTeX. This comes standard with Emacs. (It’s in lisp/textmode/reftex.el.)

Once loaded, it adds the following five key bindings, along with another five that are related to indexing:

  • C-c =
    reftex-toc shows you a table of content in a separate window and let you navigate among sections. Pressing Enter on a section brings your cursor right there.
  • C-c [
    reftex-citation scans your current bib file and help you insert \cite. It prompts you for a regex and then show you all matching entries. Press Enter on the desired entry and the proper \cite will be inserted for you! (This is a killer feature if you have long bibitem key to your bib entries.)
  • C-c )
    reftex-reference shows you the table of content of sections, equations etc. It works like reftex-citation and inserts \ref.
  • C-c (
    reftex-label helps you insert \labels by offering a somewhat intelligent default.
  • C-c &
    When the cursor is on a \ref, reftex-view-crossref brings up the corresponding \label in a separate window. When the cursor is on a \label, then it shows you occurrences of the label one-by-one. The same dual features are available when you are on a \cite and a bib entry.

P.S. Emacs means GNU Emacs. I suppose XEmacs will be similar.

CLCL – The Clipboard Manager

Those of us that use Microsoft Office should be familiar with the concept of a clipboard manager (since it has one built-in for the Office applications). Basically, a clipboard manager lets you maintain a history of your clipboard contents.

Say you want to copy a number of objects from several different slides to a new slide. Without a clipboard manager, you copy the objects from the first source slide, then you go to the destination slide, paste the clipboard content onto it, and then you repeat by going to the second source slide. Notice that you need to keep going back and forth to the destination slide because your clipboard content will be overwritten by a subsequent copy command. See how a clipboard manager can help?

But what if you are not working in an Office application? Well, that’s what CLCL is for. It’s a free clipboard manager for Windows and works for all applications.

But in my experience the single most important feature of CLCL turns out to be its ability to beep when you copy something to the clipboard. I love this audio feedback because some applications (Firefox in particular) seem to have trouble copying things. I don’t want to wait till I paste to realize that the copy did not happen. For this feature alone, I happily keep CLCL running all the time. :P

To enable this audio feedback, download the tool_utl plugin from CLCL’s homepage. Then go to Options->Tool->Add. The DLL you want is tool_utl.dll and the function name is play_sound. Then click Properties and go to the Windows\Media directory to select your favorite wave file. I use windows xp pop-up blocked.wav.

P.S. I was told that in Mac there is actually an visual feedback to indicate that the copy (Command-C) was successful. Specifically, the Edit menu item will blink once. But there is a problem. It blinks so fast that it is hard to spot. So what some users end up doing is to hold down Command-C to repeat the blinking (basically turning Edit into reverse video). Heh, I guess we all have our problems.

Emacs “Hello Kitty” Colour Theme Request

Just saw this at

“Hello Kitty” Colour Theme Request

After a discussion on #emacs, it was found that we could attract more women and young children to being Emacs users if we had a “Hello Kitty” colour theme; something with a soft pink and yellow.

Interesting idea, but is the color theme really the problem? :P

All but the first few lines of a file

You would think that there must be a utility in Unix like head or tail that would chop off the first few lines of a file and output the rest. Well, I haven’t found it yet.

In any case, perl worked.

cat foo.txt | perl -e "print splice @{[<>]}, 5"

Nano for Pico Users

I have come to know quite a few Pico die-hards over the years. Their primary reason for sticking with Pico is usually its simplicity. For this, I also think that Pico is hard to beat.

But it’s possible to match that simplicity while adding new features. Enter GNU Nano.

Among the many added features such as case-sensitive search, regexp search and replace, auto-indent and multiple file buffers, Nano also supports syntax highlighting. I guess even some Pico die-hards would agree that some of these are useful features. In fact I myself is a big fan of syntax highlighting (as you can see here).

Really, next time before you use Pico, give Nano a try. It may very well increase your productivity (which is why I am writing this). For starters, put these four lines into your ~/.nanorc and go:

set autoindent
set historylog
set multibuffer
set tabsize 4

Have fun!


I just upgraded to WinMerge and I can’t be happier.

WinMerge is a GUI for comparing and merging files. The new merge mode is very handy (F9) and it even highlights what’s changed within a region (F4). It can also compare between whole directories. Get it!

WinMerge Screen Shot

For those of you that work mostly in the command line (I do), you can put these two lines into wdiff.bat and put it in a directory in your path (I store all my small programs in “C:\Program Files\Misc”):

@echo off
start "" "C:\Program Files\WinMerge\WinMerge.exe" %*

Then you can invoke WinMerge just like diff. Note that %* means “all arguments passed to this batch file”.

Color in GNU Emacs and Vim

For those of you who use GNU Emacs or Vim, do you know that you can change the color theme/scheme very easily?

Really, take a look at these two pages and look at the varieties that you can get.

And in case you don’t know, both GNU Emacs and Vim have native versions in Windows. I use them daily and happily. :)