Bite Sized Emacs

Emacs is a big complicated beast. You can spend lots of time looking through the documentation, but sometimes it’s nice to enjoy a quick bite of Emacs goodness.

Table of Contents:
1: Custom Keystrokes
2: Indenting only as far as you feel comfortable
3: Indentation on steroids
4: Colorful shells
5: Colorful men
6: No more beeping, only flashing
7: Turn off annoying menu bar
8: Customizing font appearance
9: Line wrapping on horizontally split windows
10: Normal looking line wraps
11: Auto backup a little smarter
12: Autocomplete
13: Jumping the line
14: Moving from one window to the other easilly
15: Delete the entire word
16: Enabling the Num Pad
17: Making and storing macros
18: Word Count

Morsel 1 – Custom keystrokes

Make your own custom keystrokes.

Morsel 2 – Indenting only as far as you feel comfortable

Change the indentation rules in cc-mode to as many spaces as you want (I use 4, which looks like normal tabs on other editors, some people use 8, or 2, or whatever).
Edit your .emacs.d/init.el (depending it could be ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el) like this (substitute the number you want where I write 4):

  1. ;; define indents as 4 spaces in cc-mode
  2. (setq c-basic-offset 4
  3.       tab-width 4
  4.       indent-tabs-mode t)

The first line (setting c-basic-offset) does most of the work usually. The other two lines deal with how big tabs show.

You can change the c indentation style using (setq c-default-style “[style name here]“). These styles include bsd,stroustrup, etc. Look at the Emacs wiki about indentation in C style.

Morsel 3 – Indentation on steroids

Indent an entire block of code according to applicable rules by selecting the code and then typing “C-M-\”.
For example, let’s say you just changed c-basic-offset to 4 from 8. Or you changed the c-default-style. If you now start typing a new program, then the new rules will be in effect. But, when you open a program you’ve been working on, the parts already written will look unchanged. Here’s the step by step as to what you type (remember C = Control key and M = Meta key… usually the Alt key):

  1. C-x [
  2. C-SPACE
  3. C-x ]
  4. M-w
  5. C-M-\

And now your entire code is indented according to the new rules! Translation of steps 1: C-x [ is beginning of file. 2: C-SPACE is set mark for region. 3: C-x ] is end of file. 4: M-w is copy or mark regin. 5: C-M-\ is indent entire marked region according to applicable rules.

Morsel 4 – Colorful shells

Show the colors properly in shell mode.

  1. ;; Deal with colors in shell mode correctly
  2. (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

You know you need this if you got to shell mode in Emacs (M-x shell) and you see something like “^[[1;37m091".

Morsel 5 - Colorful men

To read the Unix / Linux man pages in Emacs just type:

  1. M-x man

Then the name of the command of interest. If you want to look at the manual pages in color, type (I mentioned this in my Completely Random Guide to Emacs)

  1. M-x woman

If you want the F1 key to bring up the woman pages, that is, manual pages in color, then add this to your .emacs.d/init.el

  1. (global-set-key [f1] ‘woman)

Morsel 6 - No more beeping, only flashing

Turn off the audible Emacs warning, but have your screen flash when it would have sounded:

  1. ;; Turn off bell, but make it visible
  2. (setq visible-bell t)

Morsel 7 - Turn off annoying menu bar

Turn off the menu bar at the top of the screen:

;; Turn off menu bar at top of screen
(menu-bar-mode -1)

You can also do the same thing with the tool bar if you have that:

(tool-bar-mode -1)

You can toggle these back on or off by typing

M-x menu-bar-mode or M-x tool-bar-mode

Morsel 8 - Customizing font appearance

Change font appearance (this will often be useful using a major mode for a programming language or text type):

M-x customize-face

If you hit enter and enter again to pick the default, you'll be able to change the default appearance of the face that is used to show the word type you we're on (comment, string, keyword, etc.). Your changes will automatically be saved to .emacs.d/init.el, so you'll be able to see how the changes were done.

Morsel 9 - Line wrapping on horizontally split windows

Usually line wrapping is on by default. You can see it is when a line comes to the edge of the screen, the '\' is shown and the line continues one below. If the line is truncated on the other hand, you won't be able to see the rest of the line, you'll only be able to see a '$'. You can use truncate lines to toggle back and forth between the wrap and truncate states by typing (this will be local to that buffer):

M-x toggle-truncate-lines

The tricky thing is that horizontally split windows (windows split by a vertical line) will still truncate. One line in your .emacs.d/init.el will let you wrap lines for horizontally split windows:

(setq truncate-partial-width-windows nil)

Morsel 10 - Normal looking line wraps

M-x longlines-mode

or if you have it (Emacs 23+)

M-x global-visual-line-mode

Will toggle normal looking line wraps on and off. If you want to get fancy, you can set up some default in your .emacs.d/init.el

  1. ;; Wrap lines visually
  2. (add-hook 'text-mode-hook 'longlines-mode)
  3. (setq longlines-wrap-follows-window-size 1)

Morsel 11 - Auto backup a little smarter

You'll soon notice Emacs scattering funny looking ~FILE and #FILE# backups across your directories. I like throwing all the auto backups into one directory and putting some sort of version number on them. This code is straight from the Emacs wiki, it goes into you .emacs.d/init.el:

  1. (setq
  2.      backup-by-copying t      ; don't clobber symlinks
  3.      backup-directory-alist
  4.      '(("." . "~/.saves"))    ; don't litter my fs tree
  5.      delete-old-versions t
  6.      kept-new-versions 6
  7.      kept-old-versions 2
  8.      version-control t)       ; use versioned backups

Morsel 12 - Autocomplete

This is quite useful, but you do have to install it. The installation instructions in the manual are clear. Take a look at autocompletion for Emacs.

Morsel 13 - Jumping the line

C-n and C-p move your pointer up and down one line, but I can do this with my up and down arrow keys, so a redefined C-n and C-p to move up and down 5 lines at a time:

  1. ;; Move up and down five lines at a time
  2. (global-set-key "\C-n"
  3.     (lambda () (interactive) (next-line 5)))
  4. (global-set-key "\C-p"
  5.     (lambda () (interactive) (next-line -5)))

Morsel 14 - Moving from one window to the other easilly

You split your window using C-x 2 and C-x 3 into as many pieces as you want. The default for moving to the next window is C-x O, but if you have 10 windows, this might take a while.

Here's how to use the arrow keys on your Num Pad to simply move up, down, left, or right (up up down down left right...... nevermind) among your windows:

  1. ;; move to window to the left
  2. (global-set-key (kbd "<kp-4>") 'windmove-left)
  3. ;; move to window to the right
  4. (global-set-key (kbd "<kp-6>") 'windmove-right)
  5. ;; move to window below
  6. (global-set-key (kbd "<kp-8>") 'windmove-up)
  7. ;; move to window above
  8. (global-set-key (kbd "<kp-2>") 'windmove-down)

Morsel 15 - Delete the entire word
This one's from a StackOverflow post that I cannot find right now. The kill-word function will delete a word from where your cursor is forward. What if you want to delete the entire word, including the part before your pointer?

  1. ;; kill entire word
  2. (defun my-kill-word ()
  3.   (interactive)
  4.   (backward-word)
  5.   (kill-word 1))
  6. (global-set-key (kbd "M-d") 'my-kill-word)

Morsel 16 - Enabling the Num Pad

Depending how you're using Emacs. The keys for the num pad may not work the way you want. You can enable them to work like this:

  1. ;; Num pad enable
  2. ;; The arithmetic operators already have keybindings,
  3. ;; so you may not want to use those
  4. (global-set-key (kbd "<kp-1>") "1")
  5. (global-set-key (kbd "<kp-2>") "2")
  6. (global-set-key (kbd "<kp-3>") "3")
  7. (global-set-key (kbd "<kp-4>") "4")
  8. (global-set-key (kbd "<kp-5>") "5")
  9. (global-set-key (kbd "<kp-6>") "6")
  10. (global-set-key (kbd "<kp-7>") "7")
  11. (global-set-key (kbd "<kp-8>") "8")
  12. (global-set-key (kbd "<kp-9>") "9")
  13. (global-set-key (kbd "<kp-0>") "0")
  14. (global-set-key (kbd "M-O n") ".")
  15. (global-set-key (kbd "<kp-enter>") 'newline)
  16. ;; Optional arithmetic operators
  17. ;; These will change the regular F key defs too
  18. ;; and you'll overide some macro and other settings
  19. (global-set-key (kbd "<f2>") "/")
  20. (global-set-key (kbd "<f3>") "*")
  21. (global-set-key (kbd "<f4>") "-")
  22. (global-set-key (kbd "<kp-separator>") "+")

Of course, you have to make some choices sometimes. If you have the num pad enabled to show numbers and symbols, then you can't use it to move from one buffer to another. This is when a defining your own minor mode might come in handy.

Morsel 17 - Making and storing macros

Define the macro. To start recording the macro:

  1. C-x (

Now type the keys, commands, etc you want done. This is just like in Excel.
Stop the macro recording with:

  1. C-x )

Now to save this macro we have to save it and insert it into our .emacs or init.el file.
Name the macro:

  1. M-x name-last-kbd-macro

Ok, now open up your .emacs or init.el file, and move your pointer (cursor) to where you want the code for your macro definition to go and type:

  1. M-x insert-kbd-macro

Now type in your custom keybinding, something like:

  1. (global-set-key (kbd "C-c n") 'my-macro)

Of course, the keybinding (C-c n) and name (my-macro) will be your own.

Morsel 18 - Word Count
Emacs doesn't have a built in word count function. You can Lisp through a function, or you can avoid reinventing the wheel by calling the Unix word count function, "wc". Just remember that the "-w" option shows the actual word count. These lines of code in your .emacs or init.el file will define the function, "word-count" to count the words in the current file. I also set "C-c c" as the shortcut for this function:

  1. ;; Word count
  2. (defun word-count nil "Count words in buffer" (interactive)
  3. (shell-command-on-region (point-min) (point-max) "wc -w"))
  4. ;; Shortcut for Word count
  5. (global-set-key (kbd "C-c c") 'word-count)

Thanks to Karsten Wade and the discussion on this page. There are many word count alternatives in the Emacs Wiki, and there's also a word-count-mode that you can download. I just like the code above for its simplicity if you're on a Unix like system anyway.
Have fun chewing on these eMACS!

Tags: , ,

Leave a Reply

blog