Monday, July 23, 2012

Terminal Editors for Quick Coding

I have recently come to realize that editing files in the terminal with the right set of tools and commands can be far quicker than working in a standard text editor. The biggest disadvantage is you cannot click where you want your pointer to be which can severely slow things down until you realize, there are better ways to find things using this console program anyways!

So first a quick rundown of advantages of using a console editor:


  • Quick find/replace on large files with no mouse interaction
  • Very powerful keybindings
  • Powerful external execution commands
It is amazing how much work has been put into these tools. Imagine all the work put into Microsoft Word, focused on a terminal editor!

My current tool of choice is vim, an enhanced version of the vi editor which in itself is built on the old ed line editor. I am just going to tick off the most commonly used commands that make using vim easier for me. Remember, escape will put you in command mode if you are in insert or replace mode.

Basic operation:
  • I: Insert text at the beginning of the line
  • i: Insert text at the cursor
  • A: Append text to the end of the line. Although there is a key for automatically going to the end of the line, I find it easier to just use this and press A to get back to command mode.
  • Control-U/Control-D: Move half a page up or down. Good if you just need to see a little more of the surrounding code, or are looking for something but do not know exactly what it is called for a search function.
  • x: Delete one character at the cursor.
  • dd: Delete a line (Also used for cut)
  • yy: Copy a line
  • p: Paste after cursor (or if its a line, past after this line)
  • u: Undo
These are the most basic commands you need to be functional. Insert, delete, cut, copy, paste, and search.

More Operations:
  • /[expression]: Search for a string. This can be a regular expression. Pressing n will find the next match.
  • ndd: Where n is a number of lines. Multiple delete. Works with yy, x and several other places.
  • d/[expression]: Delete until this expression matches. Good for deleting till the end of a programatic statement. Can also be done with y and other places.
  • D: Delete to the end of the line
  • ~: Possibly one of the lesser used operations until you realize its useful. Changes letter case.f
These next operations are command mode operations. You do these by pressing the : key.

Command Mode Operations:
  • [LINENO]: Simply enter a line number and it will jump to it
  • %s/expression/replacement/gc: Searches the file for expression, every time it is encountered it will prompt you to ask if it should replace it with the replacement. If you do not want it to prompt, remove the letter c at the end. Nice for renaming a variable quickly or removing a typo you made multiple times.
  • g/expression/command: Where expression is a regex and command is a vi command (such as d, which would cause it to delete the line). Runs the command when it finds a matching regex.
  • % ! sort: Sends the file to the sort commands and puts the response back into the file. Note that % stands for the entire file. Ranges can be used instead.
  • !wc %: Runs wc with the entire file as the input. Simply gives you a response but does not alter the file.
An interesting thing about vi is it also has a "visual" mode. For this, you press control-v and select what you want. Then you can use a command such as x (remove the selected area), I (insert in front of the selected lines, s (substitute the first character and keep typing), and more. I personally like this feature to fix indents. There may be a better way, but to delete an indent, just select the indent length, go down as far as you want, then just press x. This is far easier than typing 4x on each line. For adding indents, this is also very helpful as you can select the line, use I, and add four spaces (or how ever many you need).

VIM is a very powerful tool once you start using it, but it is not for everyone and it does have a learning curve. Until you learn a few functions, it will be very annoying. The mix of command mode versus typing mode can be very disconcerting for a new user, but I have found that I like it quite a lot. Those who want something else may like emacs, or if you prefer a smaller emacs, Linus Torvalds recently posted in a G+ post that he personally likes uEmacs/PK 4.0

In the beginning, command line editors can be far slower than notepad and other editors out there, but with a little practice, you will easily outpace your wordpad editing friends. xD