redetho logo

Zur deutschen Version.

My EMACS How-To

EMACS Logo

I had started to use EMACS occasionally with version 20.7. Since 2016-11 I am using EMACS 25.1 and have been using it regularely. 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 http://www.gnu.org/software/emacs/manual/html_node/emacs/Serial-Terminal.html

Searching in Files

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 ouput. 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 tweak the configuration in init.el to make sure grep does not complain about the "empty directory issue".

Searching for Files

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. If there is a simpler way, please tell me.

Search and Replace in Multiple Files

TAGS

Dired Mode to Search File Content

If you want to search multiple files in one or more specific directories, then dired is quite appropriate.

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

Checking different versions of a file

To switch on 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.

M-x ediff-revision

allows to compare two file revisions.

Viewing Web-Pages

Alt+x eww

The most useful keys

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 http://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.

For More Tips and Tricks See Also

"Hack EMACS" Rick Dillon on Youtube

Xah Lee's EMACS site http://ergoemacs.org/

Chris Wellons' articles about EMACS https://nullprogram.com/tags/emacs/ for example about EMACS' handling of the "working directory" versus Vim's: https://nullprogram.com/blog/2017/08/22/

"Publishing a book using org-mode" from Lakshmi Narasimhan https://medium.com/@lakshminp/publishing-a-book-using-org-mode-9e817a56d144

Emacs org-mode examples and cookbook http://ehneilsen.net/notebook/orgExamples/org-examples.html


Last change: 2021-12-05
© 2002-2021 Dr. Thomas Redelberger redethogmx.de