Charles' Emacs Page
Useful Links
.emacs
Having a good .emacs files is very useful. If you can't find one
anywhere else, please grab mine.
Note You may have to comment out (using ;
characters) parts relating to Tramp and html-helper-mode
if you do not have these packages installed.
Random
Here are a few useful things you can add to your .emacs file.
- ;; Use "y or n" answers instead of full words "yes or no"
(fset 'yes-or-no-p 'y-or-n-p)
- ;;Turn off the toolbar
(tool-bar-mode)
- ;; turn on visual bell
(setq visible-bell t)
Here are some random commands to run in emacs
- highlight-regexp (or C-x w h) highlights all
matches. unhighlight-regexp (or C-x w r)
un-highlights the word.
- indent-region (or M-C-\) indents a marked
region of text.
- C-s M-y searches for whatever is currently in the
buffer.
- C-s C-w searches for single word after current
mark. Hitting C-w repeatedly increases with additional
words (e.g., C-s C-w C-w C-w searches for three
words after current mark).
- C-s C-y searches for rest of line after mark. C-s
C-M-y searches for character after mark. (Both are repeatable
in same way as above).
Tramp and Hobo
Tramp
Tramp is a very useful package that lets you use a local version of
emacs and edit remote files. Please visit the Tramp homepage for more
details. Tramp is installed as part of the base package for Emacs 22.
Once you have tramp installed, the usage is pretty easy. You use <ctrl>-x <ctrl>-f as normal and find the file using
<ctrl>-x <ctrl>-f /[cplager@fcdflnx2.fnal.gov]localpath
<ctrl>-x <ctrl>-f /[cplager@fcdflnx2.fnal.gov]/absolute/path
Do not forget the / at the
beginning of the filename.
Tramp and AutoSave
The autosave feature in emacs can be quite annoying when using
tramp (this can take 20 seconds and you will not be able to use Emacs
while this is happening). I recommend one of two things: either
turning off autosave in buffers you are editting with tramp or
telling emacs to autosave tramp files in a local directory.
To turn off autosave in a buffer, You can do <alt>-x
auto-save-mode (or by typing <F11> if you are using my .emacs
file).
To autosave tramp files in a local directory, add similar lines to
your .emacs file:
(setq tramp-auto-save-directory "~/emacs/tramp-autosave")
trampString.pl
I wrote a perl script called trampString.pl that you run on a remote string to edit a particular file:
cplager@fcdflnx4> trampString.pl ~/.emacs
/[cplager@fcdflnx4.fnal.gov]/cdf/home/cplager/.emacs
Hobo
Hobo is a similar package to tramp that is less flexible (i.e.,
always uses scp), but faster and easier to install (see hobo.el
for details; Joseph Casadonte has this and other emacs
information here). Here's what I did to get hobo up and running
;; Hobo
(setq load-path (cons "/home/cplager/emacs" load-path))
;; hobo.el is in /home/cplager/emacs
(require 'hobo)
(setq hobo-use-agent nil)
(global-set-key [f10] 'hobo-find-file) ;; use [f10] key to load new file
Note: I have found that if I load a file with hobo
and then change the mode (e.g., change
to html_helper_mode), emacs forgets that the buffer is a hobo
buffer and doesn't push the changes back onto the original server.
Auto time stamps for HTML files
Here's a useful function for auto-time-stamping html files. After
grabbbing d a copy of html-helper-mode.el
(if you don't already have it installed) and adding
;; Auto dating for html files
(setq load-path (cons "/cdf/home/cplager/emacs" load-path)) ;;html-helper-mode.el is saved in /cdf/home/cplager/emacs
(autoload 'html-helper-mode "html-helper-mode" "Yay HTML" t)
(setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))
(setq html-helper-do-write-file-hooks t)
to my .emacs file, I add following lines:
<!-- hhmts start -->
Last modified: Fri Apr 15 14:06:28 CDT 2005
<!-- hhmts end -->
Rectangle functions
To do rectangles in emacs, you set your mark (C- usually) at one
corner of the rectangle and the point at the other (or left-click one
corner and right-click the other) and enter a rectangle command such as:
- C-x r k to kill a rectangle to
the rectangle kill ring
- C-x r y to paste a rectangle
from the kill ring (doesn't matter where the mark is set)
- C-x r t to paste a rectangle
where each line is a string you enter in at the prompt.
C-h a rectangle gives you help on
rectangle functions
Macros
There is a most recently-recorded macro, which I use a lot, and also I
think named macros, which I haven't used yet. C-h a macro gives you help on macros, and you
can also look up the subject in the emacs info browser (C-h i).
To record a macro:
- C-x ( to begin recording. Note
the "-Def" that appears in the emacs "mode line"
- C-x ) to stop recording
- C-x e to run macro
- C-u 100 C-x e (for example)
runs a macro 100 times, or until something makes emacs beep like a
failed search.
I often perform a search in a simple emacs macro to put my cursor at the
next appropriate spot. This works nicely because I often want to run the
macro many times until the end of the document. The search will fail,
which makes a beep, which stops the macro from running.
Also, if you're recording a macro and make a mistake, as I often do,
hitting C-g a time or two will get you out of macro record mode.
Running shell commands on selected text
Personally, I find Perl's syntax for making substitutions much more
powerful than what's built-in to emacs. What emacs let you do is run a script command on part of an emacs buffer.
To do this:
- <ctrl>-<space> - to
mark where you start
- <ctrl>-u - to mark where
you end (if you leave out <ctrl>-u, the output
of the shell command will appear in a separate buffer instead of replacing
the highlighted buffer)
- <right-alt> | - you'll see
Shell command on region:
- perl -pe 's/ThiS/that/g' - To
change 'ThiS' 'that'
- <enter>
Last modified: Wed Jul 6 10:02:13 EDT 2011