Saturday, June 4, 2011

Effective Text Processing - VIM & Emacs v. IDEs

Howdy hackers,

Do you lead a life of extensive text processing? Maybe you're a programmer and it's your job. Or maybe you write for a news paper and currently use microsoft word for all of your editing (please don't... There's a better way...)

Let me guess, if you're not a VIM or Emacs-er already, you use a fancy IDE to write code because it provides syntax highlighting, offers "magical code completion", and a unified environment" which automatically compiles your code. Chances are you've gotten into an argument (or several) with a VIM or Emacs 'elitist' and spent more time defending your choice of tools rather than learning about their advantages or disadvantages -- and I'm sure the emacs/vim users were just as guilty.

My Philosophy
I am not in the business of converting people from one tool to another. From personal experience, I know many people tend to become comfortable with tools with which they are familiar. I'm writing this essay because I spend a lot of time typing in front of a computer. I'd like to share my experience of what's worked well for me.

I've programmed in a variety of environments including netbeans, eclipse, dev's bloodshed interface, emacs, vim, pico, nano, ed, notepad, etc, and find that different tools have different advantages. Being a lisp programmer who spends most his time on the CLI (command line interface), emacs is my favourite editor to date (though elisp's lack of tail recursion and TCO is disappointing).

That said, I must admit I feel a bit guilty and hypocritical about writing this post. For years my hubris clouded my judgment, preventing me from acknowledging the advantages of VI. Despite the steep learning curve, I find myself preferring VIM's macro system and there really isn't much emacs can do that vim can't (and vis versa). I don't want to make this a VIM versus emacs religious battle, but I will say that emacs' abundance of modes, interactive and supported environments (like SLIME, python-shell, and and sql-mysql (not like I'm in love with sql), bash friendly key-bindings, lower learning curve, indentation, etc, make it a better fit for my life style... Also, I am a heavy org-mode user, something I don't have with vim. However, if you see a talented VIM hacker magicking, it's difficult not to concede that VIM may indeed have the upper hand for text processing.

But what about IDEs? Why the heck would someone use a command line tool (or even X emacs) when perfectly good IDEs are available for specific languages. Before I answer, I invite you to read the hacker community's thoughts @
why do some programmers hate ides or think programmers that use ides are bad programmers.

Why VIM or Emacs over an IDE?

In case you're not comfortable trusting my experience, see for yourself why Google recommends solutions like VIM.
  • Macros (available in vim and emacs)
Imagine being able to perform a sequence or pattern of operations over an entire document? Sure, you can perform search and replace on text, but what about deleting the third word in every paragraph? What about turning a comma separated value file into SQL queries? Emacs and VIM allow you to start recording a series of commands and keystrokes as a key-binding and play back the 'maco'.

If you just recorded a macro in emacs via C-x ( to start and C-x ) to end, you can use C-u 5 e to execute it 5 sequentially times. (In vim you would use 'q' to start recording a macro, followed by the letter for which you wish the completed macro to be bound. Then, type the operations you wish to be invoked by the macro, type q to stop recording, and execute by typing: <# times to execute -- default 1> @ <key to which macro is bound>.
  • Repeat Commands 
 This is a really important feature for me. With simple commands like C-u 30 C-k (emacs) you can cut 30 lines from a document.
  • Terminal Programs & Code, side-by-side
 If you program in Python, (perhaps you use windows and idle) there aren't many great tools for writing and testing your code side by side. Emacs' python-shell will let you run code in an interactive REPL in one buffer, and type code in the other (so you don't have to re-write your code when you close the shell). Also, it's nice to manage IRC, a terminal, your database session, a game of Tetras, chat with your doctor Eliza, write elisp, and organize yourself with org-mode, all within one program (text editor... or operating system? Hmm...)
  • Interactive REPLs (read eval print loop) 
 Some IDEs support multiple languages, but they tend to support a primary language (netbeans - java, visual studios - .net languages, etc). Emacs supports a variety of modes (including python, perl, c[++], haskell, php, common lisp, scheme, elisp, etc and so on). In fact, most of these language have built in REPLs (interactive shells - read eval print loop) to let you test code without ever leaving your programming environment
  • Superior Navigation and Context Switching
This is kind of a silly point, but I rarely use a mouse. I'd rather switch contexts / buffers / screens with key-bindings. Also, in this regard, I'd highly recommend looking into a tile window manager like awesome or stumpwm, as well as terminal multiplexing with a program like GNU Screen... But that's for a different essay.
  • Syntax highlighting, tabbing, and code completion
Emacs and VIM have great syntax highlighting, tabbing, whitespace notification, and code completion: Vim even allows you to easily generate html tags with its concise html with tools like tidy.

If you're really into programming, an IDE is a great tool. However, I prefer using a programmable text editor with tons of features added by the community, to accomplish exactly what I want.


- Michael E. Karpeles

Python Tips & Tricks

Howdy hackers,

Some exciting news, Babolog is now Baybo -- just

Here are a few helpful Python tips and tricks which I hope will help you save you time on menial tasks. If you have any feedback or suggestions, please let us know. We'd love to hear from you!

If you like these examples, consider searching for, "The Hacker's Guide to Python on Ubuntu" on The resource explains many of the lesser know features of the language which can save you a lot of time and hassle. HGPU should be available for purchase for $1.99 in the next few days so keep your eyes open.

Generate a List Containing All Letters of the Alphabet
>>> import string
>>> ' '.join(string.uppercase).split(' ')
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

Alternate Approach to Conditionals
# <expr> if <boolean> else <expr>
>>> True if (y == 1) else 3
Iterate in Steps (with slicing) [start:stop:step] 
Iterate over a list from a [start:end:by-step-increment]. This syntax allows you to specify both a start and end position, as well as specify an 
increment for stepping through the iteration.   
# Strip the first two items and from the 8th element on
# and Iterate the remaining elements by increments / steps of 2 
>>> for item in range(9)[2:8:2] 
...    print item 

Hope you found these tips helpful.

- Michael E. Karpeles