Force c-mode
If you want to force c-mode to be loaded (instead of c++-mode in a .cpp file, for example), insert the line below as the first line in you .cpp file:
/* -*- mode: c -*- */
Useful when you want to use ANSI C style comments (/* comment */) instead of C++ style comments (// comment).
Sample Emacs .emacs file
Sample .emacs file can be found here.
You need several emacs packages for the above sample file - browse packages here.
Reload .emacs file
If you modify .emacs, changes normally take place next time you start Emacs.
To "reload" .emacs, that is, to make the changes take place immediately, open .emacs and type:
M-x eval-buffer
cperl-mode
By default, perl-mode is loaded automatically when editing a .pl, .cgi, or .pm file.
You can switch between perl-mode and cperl-mode by typing
M-x perl-mode
M-x cperl-mode
I prefer cperl-mode by default, so I added the following lines to my .emacs file:
You can view the available indentation style and switch between them interactively using
M-x cperl-set-style ENTER TAB
html-helper-mode
Assuming you have the html-helper-mode package installed in the emacs-packages directory, you can add the following lines to .emacs file:
The html-helper-mode adds an HTML menu to Emacs, where you can use shortcuts for almost any HTML tags/entities/elements.
Reloading an Emacs feature
Let's say you hacked the html-helper-mode.el file to suit your needs. Changes in the files do not reflect the behaviours of html-helper-mode immediately - you have to reload the package first:
M-x unload-feature
RET
html-helper-mode
RET
If you have added the line (autoload 'html-helper-mode ... (see above) to your .emacs file, it is sufficient to open a new file with the .html to reload html-helper-mode.
You can also reload the feature manually:
M-x load-library
RET
html-helper-mode
RET
(Remember that in the case of html-helper-mode, the package tempo.el is also loaded, so if you want to disable all html-helper-mode features manually, you have to unload-feauture tempo as well.)
Of course, unloading/loading a feature isn't limited to html-helper-mode, but can be applied to any Emacs feature. You can get a list of loaded features:
M-x unload-feature
RET
(press TAB here to see a list of loaded features)
replace-regexp (or query-replace-regexp)
Search and replace:
M-x query-replace-regexp
Replace 4 or more digits at the beginning of a line with the sign '>':
M-x replace-regexp RET
^[0-9]\{4,\} RET
> RET
Search and replace 6 digits with an empty string:
M-x query-replace-regexp
[0-9]\{6\}
RET
RET
Search and replace 6 or more digits followed by a space with an empty string:
M-x query-replace-regexp
[0-9]\{5,\}[ ]
RET
RET
Search and replace 3 or more digits preceded by possible space(es) and followed by a space, with one space:
M-x query-replace-regexp
[ ]*[0-9]\{3,\}[ ]
RET
RET
Search the first occurence of a dot in a line (.) and remove the rest of the line, including the dot:
M-x query-replace-regexp
[.].*$
RET
RET
The same as above, but with a colon instead as a dot:
(This is useful when you have a file with a grep results from various files, and just want a list of the files; use sort -u from the command line to sort the list and eliminate duplicates.)
M-x query-replace-regexp
:.*$
RET
RET
Insert a TAB at the beginning of a line, and add a continuation character at the end:
(This is useful when for example you have a list of files, and want to list them in a Makefile or a shell script.)
M-x query-replace-regexp
^\(.*\)$
RET
C-q (prepare for control character)
RET
TAB\1 \\ (TAB means "just press TAB!")
RET
Timestamp regexp: Search and replace hours and minutes (separated by colon) followed by a space with an empty string:
M-x query-replace-regexp
[0-9]\{2\}[:][0-9]\{2\}[ ]
RET
RET
Timestamp regexp: Search and replace hours and minutes (separated by colon) preceded and followed by a space with an empty string:
M-x query-replace-regexp
[ ][0-9]\{2\}[:][0-9]\{2\}[ ]
RET
RET
Timestamp regexp: Search and replace non-formatted timestamp (hhmmss) with a formatted one (hh:mm:ss):
M-x query-replace-regexp
\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)
RET
\1:\2:\3
RET
Control characters, part 1: Replace Newline or Carriage Return (NL/CR) for any other string:
M-x query-replace-regexp
C-q (prepare for control character)
C-j (this is for UNIX NL - use C-m for DOS CR)
RET
<replace-string>
RET
Control characters, part 2: Replace comma-separated values for equal signs and NLs:
M-x query-replace-regexp
,
RET
=
C-q (prepare for control character)
C-j (this is for UNIX NL - use C-m for DOS CR)
RET
Rectangles in Emacs
You can cut and paste rectangles, i.e. columns, in emacs.
Put the cursor at the first row and column of the rectangle you want to cut or copy.
Select the rectangle (C-SPC) and move the cursor to the last row and column you want to include.
To cut (kill) a rectangle:
C-x r k
To paste (yank) a rectangle (starting where the cursor is positioned):
Sort lines alphabetically in Emacs
Select the lines you want to sort.
M-x sort-lines
Kill various buffers in Emacs
C-x C-b (List all buffers - splits window into two buffers)
C-x o (Switch to buffer list)
d (shows a D at the beginning of each line with the name of the buffer to be deleted)
x (kill all selected buffers, i.e. beginning with a D)
C-x k (kill the buffer list itself)
C-x 1 (unsplit window)
---------------------------------------- OLD AND/OR OBSOLETE INFO FOLLOWS ----------------------------------------
Emacs as external editor for KMail
Check the following checkbox:
KMail -> Settings -> Configure KMail... -> Composer -> Use external editor instead of composer
Specify editor: (older versions of Emcas) :
emacs %f -f iso-accents-mode
The -f iso-accents-mode arguments are not necessary, but convienient when typing in other languages than English.
Specify editor: (newer versions of Emacs, details explained below)
emacs %f
Emacs and mixing human languages in the same document:
In Emacs 22, iso-accents-mode is deprecated.
One (in my opinion not very easy-to-use) solution is to use C-\ latin-1-prefix instead (inside Emacs, not as a command line option).
If you plan to type in different languages, and even mix languages in the same document, including both Emacs and other applications, a better solution is to configure X (assuming you are using a UNIX-like graphical environment) to handle non-English character.
See Configure X to handle non-English characters for details.