Menu

Zur deutschen Version.

My Emacs How-To

Emacs Logo

I had started to use Emacs occasionally with version 20.7. Since 2016-11 I regularely used Emacs 25.1 and I am now on version 27.2 . This folder contains documents where I share information about

The Emacs documentation, both built into Emacs and available online, is very complete and detailed. However I found myself jotting down things while I learned. I share these below. These might be useful to people who have similar requirements.

View

Using "Emacs Frames" Rather Than "Emacs Windows"

I appreciate Emacs supports well classical terminal operation by providing "windows" functionality. However I am using Emacs from a MS Windows desktop and I am used to working with native windows there, which are called "frames" in Emacs.

I have customised (download my init.el) Emacs such that it uses "frames" by default (for example when it opens a *help* window for you).

'(pop-up-frames (quote graphic-only))

Zoom

C-x C-+ increase font site, repeat C-+ for more. Only affects current buffer

C-x C-- decrease font size

C-x C-0 go back to standard font size

Navigation

C-space C-space sets a mark and clears it again.

C-x C-x (exchange-point-and-mark) brings you back where you set the mark.

C-x r SPC r saves to cursor position in "register" r (one letter)

C-x r j r jumps back

Registers are volatile. Bookmarks are being saved.

Text-Alignment

In Emacs a paragraph is terminated by an empty line.

M-q "fill paragraph" aligns every line in a paragraph, breaking lines that are too long. The variable fill-column governs how long the lines are, the length defaults to 70 characters (more precisely: default right margin: 70).

However, to set the variable comment-auto-fill-only-comments to non-nil did not have the effect, to align automatically ;;; assembly language comments.

Modes I Use Often

Visualise Where a Search String Occurs in a Buffer

M-x occur

shows all lines in a buffer that contain the search string. They are shown in a separate frame/window, with hyperlinks you can follow/click. However, this works for one buffer only.

What M-x occur-mode does, I still need to understand.

Show a File in Hexadecimal and Edit

M-x hexl-mode

Emacs Serial Terminal Mode

Often I need a serial terminal app. to connect to a microcontroller via RS232 for debug purposes. As I use Windows, there is no out-of-the box app. ever since Hyperterm was not any more delivered with Windows. Fortunately such functionality is built into Emacs and can be activated via

M-x serial-terminal

See https://www.gnu.org/software/emacs/manual/html_node/emacs/Serial-Terminal.html

Searching in Files

Using the grep Command

Tools > Search Files (grep) ..., and input

grep -nH -r --include='*.org' regexp .

This will search recursively (-r) in all files with extension org for the string regexp. The -n option brings clickable links in the buffer, which contains the grep output. In my Emacs installation, the command grep -nH would appear in the minibuffer, when I select the menu item. I complete the rest manually.

Find files, that do not contain a string

grep -nH -r -L --include='*.org' string .

Under Linux, grep in Emacs will work out of the box. Under Windows, I needed to include git for Windows, for tools like bash and grep to be available in Emacs. In addition, I had to put in init.el

(setq grep-use-null-device nil)

to make sure grep does not complain about the "empty directory issue".

Using dired

dired is Emacs' "directory editor" mode that serves as a file explorer. There is the command M-x find-grep-dired which asks you a directory to start a recursive search and a regular expression to search the files for. Results are presented as a dired buffer.

Searching for Files

Using dired

There is M-x find-name-dired that asks you a directory to start a recursive search and a file name pattern to search for. Results are presented as a dired buffer, including nested subdirectories.

The M-x find-dired command is more general. It lets you run arbitrary 'find' commands, then displays the results in a dired buffer.

E.g. to list all executable files containing the string "foo", case insensitively, rooted at /usr:

M-x find-dired <RET> //usr <RET> -iname "*foo*" -executable -type f

Using the find Command Directly

The same menu item like above, Tools > Search Files (grep) ..., can be used to search for files:

find . -name 'body*.org'

This will find all files which start with body and have the extension .org. To ignore the file name case, you write

find . -iname 'body*.org'

Find directories, that do not contain a specific file:

find . -type d '!' -exec test -e "{}/xyz.ext" ';' -print

Note that find does not have a -n option that would produce an output buffer with links. But you can manually copy/paste the results of course.

Just to be clear: to use find, I manually overtyped the grep -nh string in the minibuffer with the find command.

Search and Replace in Multiple Files

TAGS

Use TAGS

If you have a file open in a buffer (which sets the current directory) and then do

M-x tags-search

then Emacs tries to open a so called TAGS file in that directory. If the TAGS file is not in the same directory, then you could supply a relative or absolute path. Then Emacs asks you a search pattern.

M-,

looks for the next hit until all files have been processed.

Similarly

M-x tags-query-replace

does a query and replace.

To change the TAGS file that is being used, use

M-x visit-tags-table

The current directory can be changed in Menu, Files, Open Directory... . The path separator seems to be / even under Windows.

How to generate a TAGS file

To use the Emacs "TAGS" feature, you first got to generate a TAGS file specific to the project you are working. One possible procedure:

The script consists of two lines:

TYPE flist.txt | C:\Users\xyz\OneDrive\myPrograms\emacs-25.1\bin\etags.exe -
PAUSE

This will generate the file "TAGS" in the same directory. Replace the path above with the path where you installed Emacs (see my approach under Windows).

Searching or replacing newline

To search for or replace something with newline, input C-q C-j. C-q is the "quote" character, that tells Emacs, that the following character shall be taken literally. C-j is the ASCII code for newline. Similarely, C-m is the ASCII code for carriage return. C-i is the ASCII code for Tab. Tab instead of C-i should also work.

Setting Search Case Sensitivity

By default search regexp treats upper and lower case the same. This is set by the variable tags-case-fold-search

See https://www.gnu.org/software/emacs/manual/html_node/emacs/Identifier-Search.html

In older Emacs versions, e.g. 20.7, tags-case-fold-search was not existing, only case-fold-search, but it seemed to have the same effect.

Find Non-ASCII Characters in a Buffer

Use the search regexp

[[:nonascii:]]

The cursor will be set past the found character.

Search and Replace using Regular Expressions

The following examples are source code changes. They also work with M-x tags-query-replace

Example 1 - Re-Order Parts of Identifiers

To change

xyz_Q_OFFS or xyz_D_OFFS

to

Q_xyz_OFFS respectively D_xyz_OFFS

Query regexp:

\([A-Z0-9]+\)_\([DQ]\)_OFFS

Replace expression:

\2_\1_OFFS

Example 2 - Replace Part of a String

From

aOutVol

to

MA_A_OutVol

Q:

\(\s-\)a\([A-Z][a-zA-Z0-9]+\)

R:

\1MA_A_\2

Example 3 - Change Upper/Lower Case

From e.g.

EB_VolAttOsc1_OFFS

to

EB_VOLATTOSC1_OFFS

i.e. to upper case

Q:

\(EB_\)\([A-Z][a-zA-Z0-9]+\)\(_OFFS\)

R:

\1\,(upcase \2)\3

Similarly: downcase

Example 4 - String with Embedded Blanks

Q:

abc[ ]efg

Example 5 - Change assembler addressing syntax

To change

abc_efg\w

to

(abd_efg).w

Query regexp

\([a-zA-Z0-9_]+\)\\w

Replace expression

(\1).w

ASM-Mode with my AVR Changes

Comments

; one or twice or three-times according to hierarchy level.

M-j new line in a comment

M-; insert or re-align comment

Comparing files

To use a powerfull mode to compare files do M-x ediff.

I wanted to have the two files side by side rather than one on top of each other. So C-h v ediff-split-window-function and "customize" this variable. The default is "Split vertically" which I changed to "Split horizontally" then "Apply and Save". What happened in my case, that two frames where created. I can avoid this by creating two windows before, side by side.

The keys q or z and the ediff session. When you prefix the key like C-u q, the ediff buffers will be closed (killed) in addition. If you customise the variable ediff-keep-variants, you can reverse the behaviour, i.e. kill the buffers without prefix and keep them with prefix.

M-x ediff-revision allows to compare two file revisions.

Viewing Web-Pages

Alt+x eww

The most useful keys

Inserting Special Characters

I recommend to use UTF-8 encoding in files. Any Unicode character can be inserted using

C-x 8 RET code RET

For code you could supply a hexadecimal number or a name. The name can be completed using Tab. Caveat: there are many UTF-8/Unicode characters. Hence you might want to limit the search by providing some hints to the search.

Examples:

My Description Glyph Hexadec. Unicode Name
Fixed space   00A0 NO-BREAK SPACE
Soft hyphen ­ 00AD SOFT HYPHEN
Degree ° 00B0 DEGREE SIGN
Plus-minus ± 00B1 PLUS-MINUS SIGN
Micro µ 00B5 MICRO SIGN
Times sign × 00D7 MULTIPLICATION SIGN
Non-breaking glue 200D ZERO WIDTH JOINER
EUR currency symbol 20AC EURO SIGN
Fixed minus 2011 NON-BREAKING HYPHEN
Ohm 2126 OHM SIGN
Simple short thin arrow left 2190 LEFTWARDS ARROW
Simple short thin arrow up 2191 UPWARDS ARROW
Simple short thin arrow right 2192 RIGHTWARDS ARROW
Simple short thin arrow down 2193 DOWNWARDS ARROW
Simple short thin arrow right and left 2194 LEFT RIGHT ARROW
Minus to match + 2212 MINUS SIGN
Almost equal to 2248 ALMOST EQUAL TO
Less Or Equal 2264 LESS-THAN OR EQUAL TO
Greater or equal 2265 GREATER-THAN OR EQUAL TO
Dot operator 22C5 DOT OPERATOR

I put Fixed Space, Fixed Minus and Soft Hyphen onto function keys (see my init.el), so that i can insert them quickly. ‎ When working with UTF-8/Unicode characters, you can check what font Emacs is using to render a character on screen by typing C-u C-x = with the cursor on the character.

I noticed Emacs v25.1 slowing down considerably, when including specific symbols. On Windows 10, the fonts used by Emacs were for example "MS Gothic" and "Malgun Gothic", whereas I had set "Consolas" as the default font. In other words: If Emacs could find the glyph in Consolas, then there was no slow-down. If it needed to pick from those other fonts, things slowed down.

Under Windows, you might want to check which glyphs are available in a font using the "Character Map" utility.

On Linux, e.g. "Inconsolata" and "DejaVu Sans Mono" are alternatives to "Consolas".

Miscellaneous

Set How Line Endings are Written

M-x set-buffer-file-coding-system

then: unix or dos or mac

I should also try C-x RET f

Copy a Rectangle

C-x SPC then size the rectangle using the cursor keys, then C-c

Beautify

Open the file and then indent it by indenting the entire region:

M-x find-file /path/to/file RET

C-x h (M-x mark-whole-buffer)

C-M-\ (M-x indent-region)

Version Control

VC is enabled automatically whenever you visit a file governed by a version control system. To disable VC entirely, set the customisable variable vc-handled-backends to nil (see Customizing VC).

See https://www.gnu.org/software/emacs/manual/html_mono/emacs.html

Track Files That Change Outside Emacs.

Use

M-x auto-revert-mode

See https://www.gnu.org/software/emacs/manual/html_mono/efaq-w32.html

Spell Checking

Change language using M-x ispell-change-dictionary (M-x isp-c-d will also do), then e.g. "deutsch"

Re-Read a File with Correct File Encoding

If Emacs heuristics fails, which tries to guess a file's encoding, do:

C-x RET r or M-x revert-buffer-with-coding-system

See (info "(emacs) Specify Coding") for details.

Load init.el Again

M-: (load user-init-file)

Re-Load Emacs Lisp Code

Run Emacs Lisp File

Either put in the *scratch* buffer (require 'xyz) or do M-x lisp-interaction-mode

Define Your own Keyboard SHortcuts

C-c <letter> is explicitely reserved for the user and no mode should claim them. Also C-<number> or <M-<number> seem to be available.

Check the Emacs Compilation Options

C-h v system-configuration-options will show the options used for compiling Emacs.

For More Tips and Tricks See Also

About Emacs Terminology

Emacs Terminology Other Terminology
window pane
frame window
face style? font?
region selection
kill / yank cut / paste
point cursor
mode line status bar

Last change: 2024-11-04
© 2002-2024 Dr. Thomas Redelberger redetho(a‍t)gmx.de

Close menu