Skip to main content

Emacs Notes

Misc. Emacs Notes, Hints, Customizations

downcase-region / upcase-region

When the regiion is set, one may either convert the region's contents to all uppercase or all lowercase text via upcase-region and downcase-region, respectively.

C-x l downcase-region C-x u upcase-region

Note that this functionality is disabled by default, so as to avoid confusing new users. To enable it, add the following to your .emacs file:

(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)

Python Mode for files not ending in '.py'

You can set the mode for python files with file names not ending with ‘.py’ by adding the line:

# -*- python -*-

as second line in your file (the first usually being #!/usr/bin/env python).

Indenting Code

Sometimes, especially when creating restructuredText documentation and the like, one wishes to move a block of code to the right. There are multiple ways to do that.

  1. When mark is active and the region is non-empty, typing <TAB> will indent every line in the region.
  2. Regardless of whether or not mark is active, indent-region (C-M-\) will indent every line in the region.
  3. indent-rigidly indents every line in the region.
  4. indent-code-rigidly indents everly line in the region, except for lines that start within a string, unless the region also starts inside that string.

Line Numbers

To display the current line number in the modeline, M-x line-number-mode.

To display line numbers in the side of the display, use line-num.el:

(require 'linum)

;;; How to enable

;;; 1) M-x linum-mode

;;; 2) Enable globally
(global-linum-mode 1)

;;; 3) File load
(add-hook 'find-file-hook (lambda () (linum-mode 1)))

Python Customization

Gallina's python.el is included with emacs >= 24.3. Here is the documenation for it: https://github.com/fgallina/python.el

Keyboard Macros

Very handy for repetetive operations. These are the actual functions:

C-x (    start-kbd-macro

C-x )    end-kbd-macro

C-x e    call-last-kbd-macro

Also handy in conjunction with the above:

C-/      undo
         Undo any changes you made during failed attempt at
         defining macro

C-g      keyboard-quit
         Abort macro definition

RET      newline
         Use RET to exit incremental search that is imbedded within
         macro.

Elisp in the mini-buffer

Sometimes it's handy to run one-liner elisp commands from the mini-buffer. For example, let's say that you wished to enable global linum mode. Type M-:, which will display a prompt in the minibuffer. At the prompt, enter a valid elisp expression, then press RET. The expression will be evaled and the results displayed in the minibuffer.

What if you'd like to execute a similar command, but without retyping everything? C-x ESC ESC will call repeat-complex-command, which will display the most recent complex command in the minibuffer. You are free to edit the command and, when it is as desired, press RET, which will execute the command and display the results.

What if you want to do the same with an earlier command? M-n and M-p allow one to traverse the minibuffer history and do the same with any previous command.

Shell Commands

Sometimes it would be handy to be able to run a shell command and insert the results of the command into the buffer. C-u M-! will do just that.

Similarly, it would be handy to be able to run a shell command on the contents of a region and replace the region with the output of the command (e.g., to replace an arbitrary list of file names with a sorted list of file names). C-u M-| will do that.

Faces and Themes

Ugh. Ugly, complicated, confusing. Perhaps necessarily so.

A face is a collection of attributes which are applied to text in a specific context. A theme is a collection of faces that are applied to a buffer.

To find out which face is used at point, run C-u C-x =

To see all faces: list-faces-display

To see a list of all themes that can be found, M-x customize-themes. You can click on one and have it put into effect immediately. You can also choose to 'Save Theme Settings', which will update your .emacs file with your choice.

Modes

To find out everything about the mode currently in use in a buffer, use describe-mode. It will show all the enabled minor modes, the file in which it is defined. Hopefully, it will also list the names of all hooks that the mode calls.

Key Bindings

C-h b    describe-bindings
         Show all bindings
C-h c    describe-key-briefly
         Show key binding in mini-buffer
C-h f    describe-function
         Explain function and display the binding(s)
C-h k    describe-key
         Show what is bound to key
C-h m    describe-mode
         Show bindings for current mode
C-x l    downcase-region
C-x u    upcase-region
C-x C-e  eval-last-sexp
         Eval the elisp expression before point, print value in
         echo area.
C-x ESC ESC    repeat-complex-command
         Allows one to edit then re-execute the last complex command.

M-g g    goto-line
M-:      eval-epression
         Read single elisp expression in minibuffer, eval it,
         print value in echo area.
M-^      delete-indentation
         Join previous line to current line
M-SPC    just-one-space
         Remove all but one space at point

Commenting Out Code

comment-dwin is your friend. Regardless of the language you are using, you can comment out a block of code (or uncomment a block of code) with this function. No need to be painfully exact -- just ensure that point and mark are located somewhere on the first and last lines code to be modified, then run comment-dwim, or better, just M-;.

If, for some reason, what you mean isn't clear to the function, you can always revert to comment-region (or unbcomment-region). Hoewever, to use it, point and mark must be precisely located at the beginning of the first line and the end of the last line of the code block. It will guess the comment character base on the buffer's mode. If it guesses incorrectly, try again, using C-x r t, which will prompt you for the character to be inserted.

Change the Encoding of a Text File

C-x RET f Then, select the encoding you want and save the file

Useful Unbound Functions

list-colors-display List all available colors in a new buffer customize-themes Display and select available themes describe-char Describe the character at point

Emacs and ReStructuredText

Key Bindings

C-c C-h       describe-prefix-bindings
              Displays all key bindings for the mode

ADORNMENT
C-c C-a C-a   rst-adjust
              Cycle through possible adornments for current content
C-c C-a C-d   rst-display-adornments-hierarchy
              Display current hierarchy of adornments in document
C-c C-a C-s   rst-straighten-adornments
              Homogenize all adornments in document

LISTS
C-c C-l C-b   rst-bullet-list-region
              Convert each block of text in region to bullet list
C-c C-l C-e   rst-bullet-list-region
              Convert each block of text in region to bullet list
C-c C-l C-c   rst-convert-bullets-to-enumeration
C-c C-l C-i   rst-insert-list
              Insert a list item, prompting for type and, optionally, number
C-c C-l C-s   rst-straighten-list
              Homogenize list to use identical bullet types