Adam Richardson's Site

Emacs Notes

Table of Contents

<2023-03-23 Thu>

Resources

Linking to info pages

  • The info function passed with the file or node will load the info section
(info "elisp")
  • When you want to go to a specifc node a info file place the file name in parenthesis
(info "(elisp) Simple Macro")
  • The path to a particular node can be seen in the mode line in the info buffer
  • If viewing in Emacs you can evaluate the S-Expression with C-x C-e
  • This will take you straight to the info page

How to read key commands

  • C means the control key
  • M means the meta or alt / option key
  • S means shift key
  • s means super or windows or command key
  • Combinations of keys are listed with hypens between them
  • C-M-x means press Control Alt and x all at the same time
  • Key chords are defined with spaces between them
  • C-c C-c means press Control and c at the same time then press then release then press them again
  • There is no timeout between chord keys
  • When you start typing a chord you will see the progress through the chord in the echo area

Basic movement keys

  • C-f move forward one character
  • C-b move back by one character
  • M-f move forword by one word
  • M-b move backward by one word
  • C-e move to the end of the current line
  • C-a move to the beginning of the current line
  • M-e move to the end of the current sentence
  • M-a move to the beginning of the current sentence
  • M-m move to the first non-whitespace character on the line
  • M-> move to end of buffer
  • M-< move to beginning of buffer

Using marks for navigation

  • To add to the mark ring use C-SPC C-SPC
  • To jump to mark use C-u C-SPC
  • More info here
(info "(emacs) Mark Ring")
  • If you want to clear the mark ring use (setq mark-ring nil) to empty the variable

Keyboard macros

  • Execute this lisp function to load the info page on keyboard macros (info "(emacs) Keyboard Macros")
  • To start recording a keyboard macro use C-x ( or <F3>
  • Once recording has started you will see Def in the mode line
  • If you press C-g you will cancel recording the keyboard macro
  • Once you have finished typing the macro commands press C-x ) or <F4>
  • To repeat the macro continue pressing <F4>
  • Use the function name-last-kbd-macro to give a name to the macro at the top of the ring
  • Once named you can invoke a keyboard macro like any other interactive function with M-x <NAME_OF_MACRO>
  • If you want to see the lisp behind the macro you can run M-x insert-kbd-macro

Keyboard macro counters

  • Each keyboard macro has a counter that increments each time it is inserted
  • To insert the current counter value in the buffer use C-x C-k C-c or kmacro-insert-counter

Recursively search for a file by name

Use M-x find-name-dired it will prompt you for a folder to recursively search then it prompt you for the file name.

Recurively search file files with given content

Use M-x rgrep it wil prompt for the thing you are looking for in the files, followed by the wildcard of the files to include(ivy will do a completing read for this and prepend the current path but just type the wild card pretending it is not there) and lastly the base directory.

File Local Variables

  • EmacsWiki: File Local Variables
  • It is possible to set the value of a variable local only to a file in Emacs
  • When you do this, Emacs will prompt you if you want to set those variables before actually doing it
  • This might be handy in an org mode file with a lot of code blocks, so you want to inhibit the evaluation check
  • Use M-x add-file-local-variable to interactively add a file local variabel to a buffer

Directory Local Variables

  • You can similarly add directory local variables with M-x add-directory-local-variable
  • This link covers using hooks with buffer / directory local variables

Dired Edit Mode

  • C-x C-q enters dired edit mode
  • In this mode you can edit the names of files as you would in a normal buffer
  • When finished type C-c C-c this will execute all the queued changes

C Indent Style

  • When in c-mode use the command c-set-style (C-c .) to change the indent style
  • gnu style is the default and uses 2 spaces for indent
  • The

Buttons

  • Info page on buttons, (info "(elisp) Buttons")
  • Below is code that creates a hello world button that displays the message "Hello, World"
(defun ajr-hello-world-button-pressed (button)
  (message "Hello, World!"))

(define-button-type 'ajr-hello-world-button
  'action 'ajr-hello-world-button-pressed
  'follow-link t
  'help-echo "Click to say \"Hello, World!\"")

(insert-button "Hello, World"
               :type 'ajr-hello-world-button)

Optional / Variadic Function Arguments

Optional

  • Info page on argument lists, (info "(elisp) Argument List")
  • Use the &optional keyword to indicate a param is not required
  • When an optional value is not supplied it will be nil
  • Required arguments cannot follow optional ones in the argument list

Variadic / Rest

  • When the last argument of the argument list is &rest <NAME_OF_REST_VAR> the function will accept any number of arguments
  • The rest variables will be stored in a list with the given name
  • The below example shows using higher order functions with optional params
(defun ajr-example-opt-var-low (a b &optional c &rest d)
  (append
   (list a)
   (list b)
   (list c)
   d))

(defun ajr-example-opt-var-high (&optional c &rest d)
  (ajr-example-opt-var-low "higher" "order" c d))


(ajr-example-opt-var-high "args" 1 2 3 4 5 6 7 8 "...")

View Emacs Build Information

  • To view the version of Emacs you are current using run the emacs-version function
  • To view the features compiled in read the system-configuration-features variable

Convert from decimal to hex

(format "%x" 123)

Using Emacs Serial Terminal

  • Mastering Emacs: Running Shells in Emacs Overview
  • Emacs has a serial terminal capable of talking to serial devices on your computer
  • To start the serial terminal use M-x serial-term
  • This will use term mode so you will need to use C-c C-j to enter line mode and C-c C-k to return to char mode