Emacs Notes & TipsPosted Nov 10, 2005 @ 22:01 EDT |
||
|
|
#1. Making Alt the Meta key under X.
Put this in the $HOME/.Xmodmap file
! Rebind keys to permit Alt key to be Meta in Emacs.
clear mod1
clear mod4
keycode 115 = Alt_L
keycode 64 = Meta_L
keycode 113 = Meta_R
add mod1 = Meta_L Meta_R
add mod4 = Alt_L
! Make the CapsLock key a Ctrl key
remove Lock = Caps_Lock
keysym Caps_Lock = Control_L
add Control = Control_L
and run xmodmap on it on X startup.
Confirm the keycodes with xev first.
#2. Differentiating between X mode and Console mode.
The function display-color-cells will return the number of supported
colors, which is normally 8 on a tty, 16 on MS-DOS and MS-Windows
text-mode displays, and a much larger number in the GUI versions.
#3. How do I really indent?
Typically M-i is bound to the function tab-to-tab-stop, which will tab
to the next tab stop. The tab stops are controlled via the
tab-stop-list variable.
#4. How do I change the tab width?
If you don't like the default tab stops, reset
them.
;; to set the tab stops
(defun set-tab-stops (n)
"Set the tab stops at intervals specified by the argument n."
(let ((tab-stops '())
(i n))
(while (< i 100)
(setq tab-stops (cons i tab-stops))
(setq i (+ i n)))
(setq tab-stops (reverse tab-stops))))
;; reset the tab stops to every 4
(setq tab-stop-list (set-tab-stops 4))
If you want to change the size of a displayed tab, change the value of
the tab-width variable.
(setq-default tab-width 4)
This just changes the default, so it can be overridden by a
buffer-specific variable using plain setq.
#5. How do I translate tabs to spaces?
Reset the value of the indent-tabs-mode variable to nil to prevent
inserting tab characters while you work. If you want to get rid of
existing tab characters, call the untabify function on the
region. Select the entire buffer if you want to do it all.
C-x h M-x untabify RET
#6. How do I view tab characters and newlines like with Vi's :set
list?
There is nothing built-in. You can use the whitespace.el package, or
try something simpler like the following.
;; custom miscellaneous functions
(defun vi-list ()
"Simulate a :set list in Vi."
(interactive)
(standard-display-ascii ?\t "^I")
(standard-display-ascii ?\
"$\
")
)
(defun vi-nolist ()
"Simulate a :set nolist in Vi."
(interactive)
(standard-display-ascii ?\t "\t")
(standard-display-ascii ?\
"\
")
)
#7. How do I modify the default load-path used to find Emacs packages?
Change the load-path variable, like so:
(setq load-path (cons "/home/msoulier/elisp" load-path))
#8. How do I jump to a matching parenthesis like Vi's % command?
Remap the % to do this if it's sitting on a parenthesis, and to call
self-insert-command otherwise. Granted, this means you'll never be
able to insert a % in front of a parenthesis without extra keystrokes,
but how often does that happen?
;; for simulating Vi's % capability
(defun match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s\(")(forward-list 1)(backward-char 1))
((looking-at "\\s\)")(forward-char 1)(backward-list 1))
(t (self-insert-command (or arg 1)))))
#9. How do I run some commands whenever a certain mode is set?
Add your own personal function to the hook for that mode, which is
typically by the name of <mode>-hook. For example, here's how to run
something when you enter python-mode.
(defun my-python-mode-hook ()
"My personal python mode customizations."
(message "Setting personal python-mode customizations...")
)
(add-hook 'python-mode-hook 'my-python-mode-hook)
#10. How do I customize the colours and settings in the emacs modes?
Most variables are available through the customization API. Try the
menu option "Edit->Customize Emacs", or access the customization
functions through
M-x customize- SPACE
#11. How do I jump the cursor to the top of the window, and the
bottom, like Vi's H and L commands?
You'll have to roll your own.
(defun point-to-top ()
"Put cursor on top line of window, like Vi's H."
(interactive)
(move-to-window-line 0))
(defun point-to-bottom ()
"Put cursor at bottom of last visible line, like Vi's L."
(interactive)
(move-to-window-line -1))
#12. How do I scroll the window without moving the cursor?
You'll have to roll your own.
(defalias 'scroll-ahead 'scroll-up)
(defalias 'scroll-behind 'scroll-down)
(defun scroll-n-lines-ahead (&optional n)
"Scroll ahead N lines (1 by default)."
(interactive "P")
(scroll-ahead (prefix-numeric-value n)))
(defun scroll-n-lines-behind (&optional n)
"Scroll behind N lines (1 by default)."
(interactive "P")
(scroll-behind (prefix-numeric-value n)))
(global-set-key "\C-q" 'scroll-n-lines-behind)
(global-set-key "\C-z" 'scroll-n-lines-ahead)
;; don't forget to move the "C-q" you just clobbered.
(global-set-key "\C-x\C-q" 'quoted-insert)
#13. The "C-x o" keystroke is a pain, especially when you have more
than two windows open. Is there a better way to do this?
The other-window function goes forward through the window list, but a
function to go backwards would help. other-window will with the right
argument, so try this:
(defun other-window-backward (&optional n)
"Select Nth previous window."
(interactive "P")
(other-window (- (prefix-numeric-value n))))
(global-set-key "\C-x\C-n" 'other-window)
(global-set-key "\C-x\C-p" 'other-window-backward)
#14. How do I make the compile commands easier to use?
If you're on a PC keyboard with functions keys, try something like
this:
(global-set-key [f4] 'compile)
(global-set-key [f5] 'switch-to-buffer)
(global-set-key [f7] 'next-error)
(global-set-key [f8] 'previous-error)
#15. How do I delete blank lines from a file?
C-x C-o is usually bound to the delete-blank-lines function. Read
about it with C-h f delete-blank-lines RET.
#16. How do I tell what face a piece of text belongs to?
Put the cursor on the text in question and enter:
M-x list-text-properties-at RET
#17. How do I tell when Emacs is garbage collecting?
Set the variable garbage-collection-messages to t, and watch the
*Messages* buffer.
#18. After I haven't done anything in Emacs for a while, the fontification
suddenly changes. What's going on?
C-h f lazy-lock-mode RET
#19. How do I find documentation on the available functions in Emacs?
Use M-x apropos RET, or search the info documentation. Note that
"C-h a" is not equivalent to apropos mode, as the former will only
tell you about commands (ie. interactive functions).
#20. How do I control where the line wraps in auto-fill mode?
fill-column's value is 70
Documentation:
Column beyond which automatic line-wrapping should happen.
Automatically becomes buffer-local when set in any fashion.
You can customize this variable.
#21. Sometimes I get an error about files that I've saved with Emacs,
where it complains that the file doesn't have a newline. Is this
a problem? How do I fix it?
It's not really a problem, but you can make sure a newline is inserted
with the following in your ~/.emacs:
;; make edited files end with a carriage return
(setq require-final-newline t)
#22. When I cursor past the end of the file, new lines are added to
the buffer. I don't like that, how do I turn it off?
Put this in your ~/.emacs:
;; don't let next-line add new lines at end of file
(setq next-line-add-newlines nil)
#23. When I compile, the compile window doesn't scroll along with the
output. Is there a way to fix this?
Yes. Set the variable compilation-scroll-output to true.
ie. (setq compilation-scroll-output t)
#24. How to get the tramp package working with password logins.
in .emacs:
; Configure Tramp for remote editing
(require 'tramp)
(setq tramp-default-method "sm") ; ssh with mimedecode
You can also control this on a per-connection basis with:
/r@sm:me@remotehost:/path/to/file
#25. How do I handle files from multiple operating systems?
You can choose the operating system coding system that you wish to use
with the set-buffer-file-coding-system function.
ie. C-x RET f unix/dos/mac
#26. In Emacs21, how do I control the amount of time Emacs waits
before updating the fontification of my file?
Customize the variable jit-lock-stealth-time.
#27. How do I tell if I have a conflicting site file of some kind?
Try this:
emacs -q -batch -f list-load-path-shadows
If it prints anything at all, it means you have conflicting Lisp files
on your load-path.
#28. How do I get rid of the menu bar and/or the tool-bar?
(menu-bar-mode nil)
(tool-bar-mode nil)
Or just run the functions with no args to toggle them on the current frame.
#29. How do I get the hostname of the machine that Emacs is running
on?
The variable system-name stores the hostname for you.
ie. (if (equal system-name "homebox")
(do-home-stuff)
(do-work-stuff))
#30. How do I run a shell command and capture the output?
The old way is to call the shell-command function in a temp buffer, which
permits you to manipulate the output easily as if it were any Emacs
buffer.
(with-temp-buffer
(shell-command "hostname" t)
(goto-char (point-max))
(delete-char -1)
(buffer-string))
Another way is to simply call shell-command-to-string, but this string
will contain all additional characters from the output, like newline
characters.
Thankfully, we can now call shell-command with an extra true parameter
to have it included in our current buffer.
ie. (shell-command "cal" t)
Interactive, just use a prefix command.
ie. C-u M-! cal RET
#31. How do you keep the shell-mode from scrolling by half a screen
when it reaches the bottom?
This should do it:
(setq scroll-conservatively 1)
#32. How do I set up AucTex mode for editing LaTeX?
(require 'tex-site) ; Load AUC-TeX
(setq-default TeX-master nil) ; Query for master file
(setq TeX-parse-self t) ; Parse file after loading
(setq TeX-auto-save t) ; Automatically save style
It's also important to ensure that the font-latex.el package is being
loaded as well, if you want the fontifying to be correct.
#33. Can I set up Emacs to autodetect changes to files that I'm
editing and reload them?
Yes. On a single buffer use auto-revert-mode. If you want to enable it
on all buffers, issue global-auto-revert-mode. Customize the minor
mode string like so:
(add-hook 'global-auto-revert-mode-hook
(lambda ()
(setq global-auto-revert-mode-text " GAutRev")))
#34. How can I force a reload on all buffers?
Code it. You can loop through the buffer list.
(let ((lst (buffer-list)))
(while lst
(if (buffer-file-name (car lst)) (revert-buffer (car lst)))
(setq lst (cdr lst))))
#35. Is there a way to determine if the point is in a comment region
or not?
(if (equal face-at-point 'font-lock-comment-face)
(comment-foo)
(normal-bar))
This is the extremely simple version of course, which could be used to
code a simple predicate for this very question.
(defun in-comment-block ()
(if (equal face-at-point 'font-lock-comment-face) t nil))
Or, if you want to get very generic, you can use this piece of work
from Toby Haynes, to permit generic determination of ones syntactic
context.
(defun syntactic-context ()
"Check in which syntactic context point is. Return nil if no special
context meaning, otherwise 'string if within a string, 'comment if within a
line-comment and 'block-comment if within a block-comment."
(let* ((beg (save-excursion
(beginning-of-defun)
(point)))
(state (save-excursion
(parse-partial-sexp beg (point)))))
(cond
((nth 3 state) 'string)
((nth 4 state) (if (member major-mode
'(c-mode c++-mode java-mode jde-mode))
(if (nth 7 state) 'comment 'block-comment)
'comment))
(t nil))))
#36. How do I get Emacs to stop beeping at me?
Change the bell to a visual one, just like an xterm, like so:
(setq visible-bell t)
#37. I was trying to use psgml-mode to edit my HTML, but the indenting
seems all wrong. Did I do something wrong?
Possibly. Make sure that you have a proper document type definition at
the beginning of the page. For HTML, something like this should do:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
#38. How do I turn off colour in Emacs? I don't want it when I'm in a
tty.
To turn off colour, this should do it.
(tty-color-clear)
(tty-set-up-initial-frame-faces)
Ordering is important here, so you might have to put this in your
after-init-hook.
A more complete solution would be like so, from Kai ?:
;; Support different color scheme
(defun kai-dark-background (dummy)
(add-to-list 'default-frame-alist '(background-mode . dark))
(setq-default frame-background-mode 'dark)
(mapcar 'frame-set-background-mode (frame-list)))
(add-to-list 'command-switch-alist '("-dark" . kai-dark-background))
(defun kai-light-background (dummy)
(add-to-list 'default-frame-alist '(background-mode . light))
(setq-default frame-background-mode 'light)
(mapcar 'frame-set-background-mode (frame-list)))
(add-to-list 'command-switch-alist '("-light" . kai-light-background))
You might wish to try with "emacs -dark" and "emacs -light" to see
whether that helps.
#39. When I loaded a really large file, I got an error stating
"maximum buffer size exceeded". What's up with that?
This is a limitation of the font-lock-maximum-size variable, which
controls the maximum size of a buffer for fontification purposes. The
default size is 256000. You can increase the size, or customize it by
mode as an alist, or even nil it to make it irrelevant.
C-h v font-lock-maximum-size RET
for more information.
#40. Is it possible to add keywords to cc-mode, so it recognizes more
of my custom types?
Sure. Use the font-lock-add-keywords function.
(font-lock-add-keywords
'c-mode
'(("\\<\\(FIXME\\)" 1 font-lock-warning-face t)))
Change FIXME and the face to whatever you want.
#41. How do I eval some arbitary lisp code?
The simplest way is M-: (eval-expression), but you can also put any
lisp expression in any buffer, move the cursor to the end of it, and
hit C-x C-e (eval-last-sexp).
See also: http://tiny-tools.sourceforge.net/emacs-keys.html
(This documented generously contributed by Mike Soulier.) | |