Friday, August 30, 2013

Scratching the surface of Emacs

Setting Emacs Up
----
This is an in-depth look at GNU/Emacs version 24.0, written in Emacs 24.0, and posted with Emacs 24.0. If you can't tell from that line alone, there is quite a bit to this monster of a text editor. Before it's possible to dive into specifics, it's important to understand the infrastructure that Emacs provides for extensions and customization, to see why it became the phenomena that it did.






Emacs is written in C at its core. However, it's fully extensible in a language called Emacs Lisp, which is a dialect of Scheme Lisp. One of the only functions of Emacs' core is to interprest this Emacs Lisp (abbreviated elisp) to create the user's text editing experience.

On a higher-level, the editor's "Prelude" supports various Modes. If you're editing C code, you're in c-mode (this can be invoked with the editor command M-x c-mode, where M is Alt). If you're editing Lisp, you will be in lisp-mode, etc. All of these mods and features can be invoked with the M-x command, which is basically a run dialog for the commands Emacs has at its core.

All of these modes are supplied via Elisp files, and before we can truly get started with Emacs we will want a package manager to install these little Elisp files. By default, Emacs has M-x package-install, which is what we will keep using, but it's recommended that you hook it up to the MELPA repositories. To do this, follow this quick 3-step guide here: http://profectium.blogspot.com/2013/07/how-to-install-melpa-in-gnu-emacs.html

Now that you have Melpa installed, you'll have a much easier time installing modes and tools for the languages and workflow that you desire. The only extensions that I personally use are ghc-mod, haskell-mode, scion, and the Zenburn theme. Themes can be customized in your .emacs file, or from the graphical frontend from the Emacs menu bar (Options -> Customize Emacs -> Custom Themes).

Speaking of Options, before you even start editing text, it is a good time to run through and set all the options you want. Go through the options menu, tick what you like, untick what you don't, and when you're done click Options -> Save Options. I recommend Highlighting matching parenthesis, hiding the Tool Bar, showing Column, Lines, and Size, and using a monospace font.

Navigating Files
---
Unlike Vim, which is a modal editor, Emacs is always in editing mode. You can just start typing into a new file (which you make with C-x C-f, followed by the name of the file). This will write your text to the Emacs buffer, which can be flushed with C-x C-s. This will save the file you're working on. To navigate Emacs like you would vim, there are a certain set of keybindings every user should memorize (trust me, it's VERY hard at first, but more rewarding than the arrow keys once you get it down). The following key bindings are ordered by category. Remember that for the same modifier key, you don't have to lift your finger up.

C-p: Move one line up
C-n: Move one line down
C-a: Move to the beginning of the line
C-e: Move to the end of the line

M->: Move to the end of the buffer (Alt+Shift+Greater Than)
M-<: Move to the end of the buffer (Alt+Shift+Less Than)

C-f: Move one character forward
C-b: Move one character back

M-f: Move one word forward (a word is considered to start at punctuation or a space. Contiguous punctuation is considered one stop)
M-b: Move one word back

===Copying and Pasting
C-y: Copy selected text to the system clipboard
C-w: Cut
M-w: Paste
C-x h: Select all

===Killing and Deleting
C-d: Same as the delete key
C-k: Kill the line from the current point

===Editor functions
C-s: Search for text from the current point
C-g: Kill the M-x minibuffer


===Windows
C-x o: Switch between Windows
C-x 2: Horizontally split window, making one new one.
C-x 3: Vertically split window, making one new one.
C-x 0: Kill the current window, but do not close the buffer.

===Buffers (think of these like open files, or tabs in Notepad++)
C-x (Arrow key left or right): Next or Previous buffer
C-x b: Select buffer by name (like everything in Emacs, supports tab completion)
C-x k
---

With these keyboard shortcuts, you will have solid usage ability within the Emacs editor. It will take a while to gain the muscle memory to learn these commands, but they're much worth learning. Many IDE's and Web Browsers (namely firefox) have support for these Emacs keybindings. You can also set what each key binding does yourself if you have a bone to pick with one of the defaults. Every keyboard command can also be accessed through M-x. M-x Tab will show a list of every command in the Emacs editor, so pull up a chair, and experiment with some of the ones that sound interesting.

Emacs offers a lot more than this simple tutorial covers. This is meant to cover the simple commands used in editing a file. However, Emacs can be made to be your IRC client, Email client, Web Browser, Git interface, Media Player, terminal... Whatever you want it to be, really. There have even been experimental  window managers for Emacs. Some of these functions are installed by default under the Tools menu, and can be accessed by way of M-x.

P.S: For a persistant text editing session, Emacs can be run as a server.

And, for whatever reason, if you want to close Emacs you can type C-x C-c.


Here is a small look at Emac's power:



No comments:

Post a Comment