17 emacs snippets
emacs calendar functions
Open the calendar:
(You can close the calendar with
q, which invokes
C-for arrow-right is
C-bor arrow-left is
C-nor arrow-down is
C-por arrow-up is
- Use the
M-<n>prefix to move a multiple of the above. E.g.
M-90 <right-arrow>) moves forward 90 days. (In calendar mode, the meta key is optional, simply
90 <right-arrow>will move forward 90 days,
16 <down-arrow>will move forward 16 weeks, etc.)
owill let you interactively select a date.
.will return to today
- (Right-click on the calendar will generally open up a context-menu about these.)
p dis (calendar-print-day-of-year)
C-<space>, move to another date and type
(calendar-count-days-region)(show the number of days between the dates (including the endpoints).
H yto generate an html-format calendar (month and year, respectively). (
t <something>generates a number of interesting calendars in TeX, if you are so inclined.)
hto see known holidays for the selected date, or
ato see all holidays for the three month period that is displayed. Use
M-x list-holidaysfor even more. Use
Mto see a list of lunar phases. Use
Sto see sunrise/sunset information.
Also see the emacs manual.
emacs cursor movement shortcuts
M-g g <N>or
M-g M-g <N>-- go to line
M-g c <N>-- go to character
M-g <TAB> <N>-- go to column
in the current line
<right>-- forward one char (
forward-char) or right one char (
right-charmoves backward when editing right-to-left text.)
C-<right>-- forward one word (
forward-word) or right one word (
<left>-- backward one char (
backward-char) or left one char (
left-charmoves forward when editing right-to-left text.)
C-<left>-- back one word (
backward-word) or left one word (
<down>-- down one line (
<PageDown>-- down one page (
<up>-- up one line (
<PageUp>-- up one page (
C-x C-n-- set current column as "goal" column for up/down movement
C-u C-x C-n-- cancel current goal column
Searching in Emacs
C-r-- search / search backward in current buffer
M-x ioccur-- list lines in current buffer matching regexp
M-x rgrep-- recursive grep (find in files)
C-x C-x to toggle between positions
For example: Use
C-s foo to search for something. Maybe use
C-s again to step thru. Now use
C-x C-x to flip back to the point where you started (and
C-x C-x again to return).
Change font size in emacs
To change the font size in the current buffer:
C-x C-+- increase font size
C-x C--- decrease font size
Follow either with
0 for continued adjustment.
To change the font size in all buffers:
(set-face-attribute 'default nil :height 120) ;; where `height` is 10x point size
M-y to yank (paste) into the emacs i-search prompt
Change Text Case in Emacs
To change the letter case in Emacs:
(upcase-word)- will convert following word to UPPER case (or the rest of the current word).
(downcase-word)- will convert following word to lower case (or the rest of the current word).
(capitalize-word)- will Capitalize the following word (or the rest of the current word).
For example, consider the text
The quick brown fox jumped.
If you position the cursor on the
q (fifth column), then
If you position the cursor on the
u (sixth column), then
One could probably write an elisp function based on
(capitalize-word) and some heuristics or dictionary look-ups to create a true title case function (e.g., not capitalize
the in the middle of a phrase), but
M-c is a quick and easy approximation.
Also, note that there is a version of upcase and downcase that work on the selection (region) instead of the next word.
Of course, if you're using "CUA Keys", you can't easily type
C-x without invoking "cut".
Default fonts with emacsclient/emacs --daemon
I've been using
set-default-font in my
.emacs file to configure emacs to use my favorite programming font.
(set-default-font "Droid Sans Mono Slashed-10") ;;; set default font
emacs --daemon to keep an instance of emacs running as a background process on my development machine so that I can run
emacsclient to call up an emacs window (frame) instantaneously (and keep the same session running even after a close the emacs frame).
Suddenly (after an
aptitude safe-upgrade, I think, but I'm not sure what triggered this change), emacs frames that are created by
emacsclient connecting to the
emacs --daemon instance no longer used my default font when initially opened. The default font worked properly for stand-alone emacs instances (opened with
emacs), and for
(set-default-font) after startup worked fine, but it no longer worked automatically.
To fix this, I had to set up a
default-frame-alist, which I believe defines commands to execute whenever a new frame is opened.
(set-default-font "Droid Sans Mono Slashed-10") ;;; set default font (setq default-frame-alist '((font . "Droid Sans Mono Slashed-10"))) ;;; set default font for emacs --daemon / emacsclient
By the way, I also discovered the
describe-font elisp function while trying to diagnose this issue.
(describe-font "Droid Sans Mono Slashed-10") ;; or M-x describe-font <RETURN> Droid Sans Mono Slashed-10 <RETURN>
which opens a
*Help* buffer containing:
name (opened by): -unknown-Droid Sans Mono Slashed-normal-normal-normal-*-13-*-*-*-m-0-iso10646-1 full name: Droid Sans Mono Slashed:pixelsize=13:foundry=unknown:weight=normal:slant=normal:width=normal:spacing=100:scalable=true size: 13 height: 17 baseline-offset: 0 relative-compose: 0
various emacs shortcuts
- In org-mode,
<s[tab]inserts a BEGIN_SRC/END_SRC block. (Think "insert source".)
C-x r b- jump to bookmark
C-x r m- create (make) bookmark
M-o M-S- center line / center paragraph
M-<Page Up>, etc. - move in other window/frame (without losing focus on the current window/frame)
C-e- beginning of line / end of line
M-e- beginning of sentence / end of sentence
C-f- backward one character / forward one character
C-n- previous line / next line
C-l- recenter top/bottom
C-j- newline and indent
C-c h- hide lines matching
C-h e- show echo area messages
Numbering lines in emacs
A few ways to add line numbers in emacs:
M-x linum-mode <RET>will annotate the buffer with line numbers. (These numbers are decoration, in the "fringe" rather than part of the buffer text.) Also see Linum Plus.
C-x r Nwill insert line numbers into the selected region. (These numbers are content, they are added to the text of the buffer.)
M-x line-number-mode <RET>show the line number of the current line in the modeline.
C-x lwill report (in the minibuffer) the total number of lines in the current buffer as well as the number of lines before and after the cursor
M-x what-line <RET>will report (in the minibuffer) the current line number
Also see emacswiki.org/LineNumbers.
Simple emacs mode for .gitignore files.
Through the power of generic-mode, adding the following lines to your
.emacs file will add syntax-coloring support for
.svnignore, etc. files. And by "syntax-coloring" I mean that lines that start with a
# will be marked as comments.
(require 'generic-x) (add-to-list 'auto-mode-alist '("\\..*ignore$" . hosts-generic-mode))
Actually, any text after an un-escaped
# will be marked as a comment, which isn't the way Git and SVN interpret those files. (TODO: it would be pretty simple to add a dot-ignore-generic-mode that handles this correctly.)
Spell checking cheat-sheet for emacs.
ispell-region(depending on whether something is selected)
[SPACE]- Skip this word—continue to consider it incorrect, but don't change it here.
r newword [RETURN]- Replace the word, just this time, with new. (The replacement string will be rescanned for more spelling errors.)
R new [RETURN]- Replace the word with new, and do a query-replace so you can replace it elsewhere in the buffer if you wish. (The replacements will be rescanned for more spelling errors.)
a- Accept the incorrect word—treat it as correct, but only in this editing session.
A- Accept the incorrect word—treat it as correct, but only in this editing session and for this buffer.
i- Insert this word in your private dictionary file so that Aspell or Ispell or Hunspell will consider it correct from now on, even in future sessions.
i, but you can also specify dictionary completion information.
u- Insert the lower-case version of this word in your private dictionary file.
l word [RETURN]- Look in the dictionary for words that match word. These words become the new list of “near-misses”; you can select one of them as the replacement by typing a digit. You can use
*in word as a wildcard.
C-g X- Quit interactive spell checking, leaving point at the word that was being checked. You can restart checking again afterward with
x- Quit interactive spell checking and move point back to where it was when you started spell checking.
q- Quit interactive spell checking and kill the spell-checker subprocess.
Via the emacs FAQ.
Using (quoted-insert), or how to enter a newline character in the emacs minibuffer
To insert a newline character in the mini-buffer, use
dos2unix in emacs
M-% C-q C-m [RETURN] [RETURN]
M-% is bound to
C-q is bound to
(quoted-insert), which allows
C-m to insert the control character
Case insensitive sort-lines in emacs.
M-x set-variable [RETURN] sort-fold-case [RETURN] t [RETURN] M-x sort-lines
Using js-mode's indent logic in js2-mode
Here's a somewhat hack-y workaround: switch to
indent-region and then switch back.
;; use js-mode's indent logic, by pressing C-M-| (C-M-S-\) (defun rlw/js-indent-region () (interactive) (js-mode) (indent-region (region-beginning) (region-end)) (js2-mode) ) (define-key js2-mode-map (kbd "C-M-|") 'rlw/js-indent-region)
- mooz's fork of js2-mode
- thomblake's js3-mode
- I think the defunct espresso-mode is now the built-in
- I'm not sure where that leaves
- Steve Yegge's js2-mode
The first two are supposed to address js2-mode's indentation problems (among other enhancements).