Emacs

tips

  • list functions on the buffer: m-x occur
  • execute command multiple times: c-u 5 c-p
  • jump to first non-blank character: m-m
  • convet to unix format: c-x RET f enter utf-8-unix

custom dependency mangagement

(require 'package)
(setq package-archives
    ("gnu"        . "http://elpa.gnu.org/packages/")
    ("melpa"      . "http://melpa.org/packages/" )))

(package-initialize)

(when (not package-archive-contents)
    (package-refresh-contents))

(defvar my-packages '(
    slime
    cider
    cider-test
    ))

(dolist (p my-packages)
    (when (not (package-installed-p p))
        (package-install p)))

search word under cursor

(defun usrj/asterisk()
    (interactive)
    (save-excursion
        (occur (thing-at-point 'symbol))
        (other-window 1)))

copy current line

(defun usrj/copy-line-down()
    (interactive)
    (let (line)
        (setq line (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
    (move-end-of-line nil)
    (newline)
    (insert line)))

insert a markdown link

(defun usrj/md-link(url)
    (interactive (list(read-string "url: ")))
    (let (title buf)
        (with-temp-buffer
            (url-insert-file-contents url)
            (setq buf (buffer-string))
            (string-match "<title>\\(.*\\)</title>" buf)
            (setq title (match-string 1 buf))
            (message ""))
        (insert (concat "[" title "](" url ")"))))

key bindings

esc and <f12> as c-x (so I can access it using either hand), and <f2> as m-x

(define-key key-translation-map (kbd "ESC") (kbd "C-x"))
(define-key key-translation-map (kbd "<f12>") (kbd "C-x"))
(define-key key-translation-map (kbd "<f2>") (kbd "M-x"))

update 1

I've changed to using c-m for c-x and c-i for c-c, now it's easier.

update 2

I've changed to using <f2> and <f12> prefix for common actions.

update 3

I've switched to evil mode. most custom key bindings are moved under evil-leader.

using magit

basic flow

  1. M-x magit-status
  2. FF to pull (note, Fu in latest version)
  3. make some changes
  4. S to stage all changes including new files (highlight item, press <tab> to view changes)
  5. cc, type some comments, C-c C-c to commit
  6. PP to push (note, Pu in latest version)

interactive rebase

one thing is missing from the ? menu, interactive rebase (git rebase -i).

type ll(short log), highlight the commit, then press E

under windows

magit is very slow under microsoft windows, I didn't find a solution yet.

setup java environment

I use emacs-eclim package.

the setup is easy, check the install page (you don't need vim, the installer allows you skip this requirement).

if using gradle, apply the eclipse plugin, which auto complete will work for dependency libraries.

reading notes

notes

with-current-buffer and with-temp-buffer

(from How to Make an Emacs Minor Mode)

the with-current-buffer macro is used to specify a different current buffer for a body of code. It can be used to access other buffer's local variables. Similarly, with-temp-buffer creates a brand new buffer, uses it as the current buffer for its body, and then destroys the buffer.

optional default value

elisp doesn't support default value like common lisp:

(defun foo (a &optional (b 10)) (...))

optional arguments will always be nil if not provided, so can use or to s the default value:

(defun foo (a &optional b) (setq b (or b 100)) (...))

recommended packages

  • org-mode
  • magit
  • nyan-cat-mode
  • ace-jump-mode
  • ace-window
  • multiple-cursors
  • projectile
  • ecb
  • paredit
  • expand-region
  • undo-tree

short keys

elisp

| `c-m u` or `c-m <up>`      | move up one level            |
| `c-m d` or `c-m <down>`    | move down one level          |
| `c-m f` or `c-m <right>`   | forward s-expression         |
| `c-m b` or `c-m <left>`    | backward s-expression        |
| `c-m u` then `c-m <space>` | mark current s-expression    |
| `c-m u` then `c-m k`       | kill current s-expression    |
| `c-m t`                    | transpose s-expression       |
| `m (`                      | wrap selection in parenthese |

cider

| `C-c M-n`  | Switch to namespace of the current buffer           |
| `C-x C-e`  | Evaluate the expression immediately preceding point |
| `C-c C-k`  | Compile current buffer                              |
| `C-↑, C-↓` | Cycle through CIDER history                         |
| `C-↵`      | Close parentheses and evaluate                      |

paredit

| `M-(`, `M-"`      | wrap around               |
| `C-(`             | slurp backward            |
| `C-)`             | slurp forward             |
| `C-{`             | barf out backward         |
| `C-}`             | barf out forward          |
| `C-q )`           | force insert              |
| `C-M-f`           | move forward s-expression |
| `C-M-b`           | move backward             |
| `C-M-u` , `C-M-p` | move up                   |
| `C-M-d`, `C-M-n`  | move down                 |
| `C-k`             | kill                      |
| `M-<up>`          | kill backward             |
| `M-<down>`        | kill forward              |
| `M-s`             | splice                    |
| `M-S`             | split                     |
| `M-J`             | join                      |
| `M-?`:convolute   |                           |
move parentheses

| action          | function                    | hot-key  | alt-hot-key          |
|-----------------+-----------------------------+----------+----------------------|
| move ( to left  | paredit-backward-slurp-sexp | ctrl + ( | ctrl + alt + left    |
| move ( to right | paredit-backward-barf-sexp  | ctrl + { | ctrl + alt + right   |
|-----------------+-----------------------------+----------+----------------------|
| move ) to left  | paredit-forward-barf-sexp   | ctrl + } | ctrl + shift + left  |
| move ) to right | paredit-forward-slurp-sexp  | ctrl + ) | ctrl + shift + right |


move cursor

| action              | function             | hot-key        | alt-hot-key       |
|---------------------+----------------------+----------------+-------------------|
| move up one level   | paredit-backward-up  | ctrl + alt + u | ctrl + alt + up   |
| move down one level | paredit-forward-down | ctrl + alt + d | ctrl + alt + down |
| move forward        | paredit-forward      | ctrl + alt + f |                   |
| move backward       | paredit-backward     | ctrl + alt + b |                   |


select sexps

| action          | hot-key                |
|-----------------+------------------------|
| select forward  | ctrl + shift + alt + f |
| select backward | ctrl + shift + alt + b |


kill parentheses

| action           | function            | hot-key |
|------------------+---------------------+---------|
| kill parentheses | paredit-splice-sexp | alt + s |
|                  |                     |         |


add parentheses

| action      | function           | hot-key |
|-------------+--------------------+---------|
| wrap around | paredit-wrap-round | alt + ( |
|             |                    |         |


kill

| action        | function                             | hot-key              |
|---------------+--------------------------------------+----------------------|
| kill sexp     | kill-sexp                            | ctrl + alt + k       |
| kill line     | paredit-kill                         | ctrl + k             |
| kill backward | paredit-splice-sexp-killing-backward | alt + up             |
| kill forward  | paredit-splice-sexp-killing-forward  | alt + down           |
| raise sexp    | paredit-raise-sexp                   | alt + r              |
| join          | paredit-join-sexps                   | alt + J (upper case) |


re-indent

| action   | function               | hot-key |
|----------+------------------------+---------|
| reindent | paredit-reindent-defun | alt + q |

projectile

| `C-c p f`   | Display a list of all files in the project.                                   |
| `C-c p F`   | Display a list of all files in all known projects.                            |
| `C-c p l`   | Display a list of all files in a directory (that's not necessarily a project) |
| `C-c p s g` | Run grep on the files in the project.                                         |
| `C-c p b`   | Display a list of all project buffers currently open.                         |
| `C-c p 4 b` | Switch to a project buffer and show it in another window.                     |
| `C-c p j`   | Find tag in project's TAGS file.                                              |
| `C-c p k`   | Kills all project buffers.                                                    |
| `C-c p D`   | Opens the root of the project in dired.                                       |
| `C-c p e`   | Shows a list of recently visited project files.                               |
| `C-c p !`   | Runs shell-command in the root directory of the project.                      |
| `C-c p p`   | Display a list of known projects you can switch to.                           |
| `C-c p S`   | Save all project buffers.                                                     |
| `C-c p m`   | Run the commander (an interface to run commands with a single key).           |
| `C-c p o`   | Runs multi-occur on all project buffers currently open.                       |

org mode

org mode's table is very useful for me, for the rest of keys check The compact Org-mode Guide

| C-c ¦               | Convert the active region to table.                                                                             |
| C-c C-c             | Re-align the table without moving the cursor.                                                                   |
| <TAB>               | Re-align the table, move to the next field. Creates a new row if necessary.                                     |
| S-<TAB>             | Re-align, move to previous field.                                                                               |
| <RET>               | Re-align the table and move down to next row. Creates a new row if necessary.                                   |
|---------------------+-----------------------------------------------------------------------------------------------------------------|
| M-<left>, M-<right> | Move the current column left/right.                                                                             |
| M-S-<left>          | Kill the current column.                                                                                        |
| M-S-<right>         | Insert a new column to the left of the cursor position.                                                         |
| M-<up>, M-<down>    | Move the current row up/down.                                                                                   |
| M-S-<up>            | Kill the current row or horizontal line.                                                                        |
| M-S-<down>          | Insert a new row above the current row. With a prefix argument, the line is created below the current one.      |
| C-c -               | Insert a horizontal line below current row. With a prefix argument, the line is created above the current line. |
| C-c <RET>           | Insert a horizontal line below current row, and move the cursor into the row below that line.                   |
| C-c ^               | Sort the table lines in the region.                                                                             |
Search Blog: