5.3. Editors#

The vast majority of OS developers use text based editors, that can be accessed from a terminal, by using ssh to get to a machine, and over a wide variety of connections. The two most popular options are vim and emacs, which are both available in the container image provided with this book. While the choice of which is better is the oldest rivalry in computing1, the author of this section feels comfortable asserting that it is a self-evident truth that you will be more productive, get a better grade, and probably become a better person if you master emacs (and anyone from the “cult of vi” that touches this chapter puts their soul at risk2).

With vim (the most popular variant of vi today), most users use it to modify a file, and then exit vim when they want to compile, debug or run their programs. To open a file, type vim <filename>. When you enter vim, you will automatically be in normal mode where you can navigate using your cursor. To insert text, type i to enter insert mode. or ‘a’ to enter ‘append mode’. when you are done changes, press the esc key to enter normal mode, next press : to enter command mode, lastly press w (for write) and enter to save. To quit, enter command mode and q (quit). There is also an incredibly arcane command set that enables members of the “cult of vi” to do very complex operations with a few key strokes, but if you spend your time learning that you will probably not have any time left to do OS assignments.

Now that we have discussed everything you ever need to know about vim, lets talk about the better alternative. While you could use emacs to just edit files, power users live in emacs and use to to compile, debug, and run their programs. To open a file in emacs, type emacs <filename>, or if you are in emacs already (which you should always be) type <ctrl>-x <ctrl>-f to open a file. Unlike in VIM, you will immediately be able to write in/edit the file. Once you are finished editing, type <ctrl>-s to save the file. When you want to leave (which you never should) you type <ctrl>-x followed by <ctrl>-c to exit. There are many control and escape sequences to do powerful things and a really good reference card is available here.

A few of the more powerful features that you should take advance of in emacs are:

  • Type <ctrl>-x <ctrl>-f to open a directory, where emacs will let you move around the filesystem while staying in the editor.

  • Starting up a shell which enables you to copy content easily from other buffers in emacs to the shell, and run programs directly there.

  • Running make within emacs. When you do this emacs parses the error messages, and hitting <ctrl>-x <backquote> will automatically pop you into the right file on the line where the compiler emitted an error.

  • Run debugger from within emacs (<esc>-x gdb). When you do this, emacs parses the output of gdb, and automatically pops you into the correct source code file where the code you are debugging lives.

  • Run grep in emacs (<esc>-x grep) where emacs parses the result, and hitting <ctrl>-x <backquote> will automatically pop you into the right file on the line where grep returned a result.

These are just a few of the very powerful ways that emacs will improve your life as a software developer (and likely you a a person). While it is a steep learning curve, you will find after a while that your fingers know all the common keystrokes, and you productivity will increase exponentially.

Warning

One danger all real emacs users face eventually is Carpal tunnel syndrome, strongly recommend you rebind the caps locks key to be the control key.


1

Try doing a google search for emacs and then do one for vi.

2

All true believers in the church of emacs consider vi (the predecessor to vim) as the editor of the beast (vi-vi-vi being 666 in roman numerals).