Search:

PmWiki

pmwiki.org

edit SideBar

Main / Emacs

ctrl x, ctrl f = open
ctrl x, ctrl v = reload file
ctrl x, k = close file (aka kill buffer)
ctrl x, ctrl s = save
ctrl x, ctrl w = save as

ctrl x, arrow = switch buffer
ctrl x, 0 = clear extra buffer windows
ctrl x, b = pull up specific buffer (also works well as a recall previous)
ctrl x, ctrl b = list buffers

crtl x, ctrl e = evaluate expression on the line (must highlight entire expression)

ctrl x, h = select all
alt w = copy
ctrl w = cut
ctrl y = paste
ctrl k = delete line
ctrl x, space = begin mark for rectangular (column) select

ctrl c, ctrl f = copy file path to clipboard (not the primary selection)

ctrl s = search
ctrl r = search reverse
ctrl s, ctrl w = highlight word at cursor and search
alt shift 5 = replace
alt g, g = goto line

ctrl x, tab = block indent interactive (follow with arrow key control)
alt i = indent
ctrl u, <num>, ctrl x, tab = force indent by <num> spaces (uses SPACES), block supported
alt x = invoke Emacs command line
ctrl q, tab = force insert a tab character (not spaces)

ctrl h, k = describe key
alt shift : = command line prompt for eval

alt x, hexl-find-file = lets you open a binary file in hex format

Accessing complex instructions

alt x, recover-file = restore auto-save file (#filename#.ext)
alt x, delete-trailing-whitespace = remove trailing spaces

Config

To diagnose problems with the config file run with --debug-init
Emacs Config File
https://www.emacswiki.org/emacs/buffer-stack.el (about)
http://www.emacswiki.org/emacs/download/buffer-stack.el (download)

To add an emacs plug-in, download the .el file and place into ~/.emacs.d/ Then add these lines to .emacs

(add-to-list 'load-path "~/.emacs.d/")
(load "myplugin.el")
(require 'myplugin)

Features of my config file:

ctrl ; = block comment
ctrl ' = block uncomment
alt left = prev buffer
alt right = next buffer
ctrl z = undo
ctrl c, ctrl f = copy file path
alt up, alt down = scroll one line
ctrl+shift d = add disassembly pane

I have included ibuffer which is an add-on improvement over the standard buffer listings.
http://mytechrants.wordpress.com/2010/03/25/emacs-tip-of-the-day-start-using-ibuffer-asap/

Other hints about buffers
http://www.gnu.org/software/emacs/manual/html_node/elisp/The-Buffer-List.html

How to assign syntax highlighting to an unrecognized file extension

http://unix.stackexchange.com/questions/25184/emacs-file-extension-recognition

Font face manipulation

alt-o, d Remove all face properties (facemenu-set-default).
alt-o, b Apply the bold face (facemenu-set-bold).
alt-o, i Apply the italic face (facemenu-set-italic).
alt-o, l Apply the bold-italic face (facemenu-set-bold-italic).
alt-o, u Apply the underline face (facemenu-set-underline).
For this to stick when you save and close, you must enable enriched mode with alt-x enriched-mode.

Why are my makefile comments highlighted?

The indented ones are because they are passed by make to the shell as commands and are not interpreted immediately as comments.

Link Highlighting

https://emacs.stackexchange.com/questions/4001/saving-and-restoring-highlighting-patterns
https://stackoverflow.com/questions/385661/how-to-highlight-all-occurrences-of-a-word-in-an-emacs-buffer
ctrl-x w p phrase <RET> face <RET>
To create an init action that happens every time a file is opened, you have to reference it with add-hook and use find-file-hook.

File Mode Edits

ctrl-h, v major-mode
tells you what file mode you are in. You can view any variable's current value with
ctrl-h, v

For shell scripts,
to set the wrapped-line continuation indent, TODO - nothing is working, emacs is broken
to set the default new line tab width, use (setq sh-indentation 2);

Source Tagging for Reference Linking

Cscope

Install Cscope and the Emacs lisp extension with sudo apt install cscope cscope-el.

cscope -R at top level will generate the database and launch the UI.
cscope -R -b will not launch the UI.
ctrl D will exit.

etags

This will take in all the .c/.h/.cpp/.hpp files in the tree where executed and generate a TAGS file:

find . -type f -regex ".*\.*[ch]\(pp\)?$" | etags -

alt . = find definition
alt shift ? = find references (only works in emacs v25+)

The first time you try to use it, Emacs will ask where the TAGS file is location. After that you should be fine.

Update Notes

Apparently pc-selection-mode is obsoleted and replaced with delete-selection-mode

List of Emacs options

https://docstrings.github.io/list.html

.emacs file

(message "* --[ Loading my Emacs init file ]--")
(message "* --[ * Created by TSheffield *  ]--")
(message "* --[  Note: requires plug-ins   ]--")

(defun c-mode-common-hook ()
  (c-toggle-electric-state -1)
  (c-set-style "k&r")
  )
;;(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

;; make vertical buffer split the default if applied
(setq split-width-threshold 1)

;;add disaster.el disassembly viewer, uses ctrl-shift-d
(add-to-list 'load-path "/home/ts/.emacs.d/addons")
(require 'disaster)
(global-unset-key (kbd "C-S-d"))
(global-set-key (kbd "C-S-d") 'disaster)
;;(define-key c-mode-map (kbd "C-c d") 'disaster)

;; shows cursor column num after line (line#,col#)
(setq column-number-mode t)

;; disable C-z kill and set to undo
(global-set-key "\C-Z" nil)
(global-set-key "\C-Z" 'undo)

;; enable clipboard share
(setq x-select-enable-clipboard t)

;; set colors
(set-background-color "#2f2f2f")
(set-foreground-color "white")

;; set indent
;;(setq-default standard-indent 3)
;; turns off tabs for indentation
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
;;(setq tab-width 3)
(defvaralias 'c-basic-offset 'tab-width)
(setq tab-stop-list (number-sequence 4 120 4))
;;(setq tab-stop-list (number-sequence 8 120 8))
;; If you wish to change the amount of spaces that the Tab key inserts, check out the 
;; TabStopList page. Do not confuse this with tab-width, which is only good for displaying 
;; existing tabs in files!

;; set shell script mode indentation default (2 is for HR coding style)
(setq-default sh-indentation 2)
;; none of this crap works, emacs ignores it
(setq-default sh-indent-after-continuation nil)
(setq-default sh-indent-for-continuation 0)

;; line highlighting
;;(defvar running-xemacs (string-match "XEmacs" emacs-version))
;;(if (not running-xemacs)
    ;;(pc-selection-mode 1))

;; add block comment capability
(global-set-key (kbd "C-;") 'comment-region)
(global-set-key (kbd "C-'") 'uncomment-region)

;; show file path
(setq frame-title-format
      (list (format "%s %%S: %%j " (system-name))
        '(buffer-file-name "%f" (dired-directory dired-directory "%b"))))

;; make Completions buffer disappear for good
(add-hook 'minibuffer-exit-hook 
      '(lambda ()
         (let ((buffer "*Completions*"))
           (and (get-buffer buffer)
            (kill-buffer buffer)))))

;; view scons files in Python mode
(setq auto-mode-alist
      (cons '("SConstruct" . python-mode) auto-mode-alist))
(setq auto-mode-alist
      (cons '("SConscript" . python-mode) auto-mode-alist))

;; use the ibuffer add-on
(require 'ibuffer)
;(global-set-key (kbd "C-x C-b") 'ibuffer-other-window) - opens in split
;(global-set-key (kbd "C-x C-b") 'buffer-menu) - opens emacs default buffer list
(global-set-key (kbd "C-x C-b") 'ibuffer)
(setq ibuffer-default-sorting-mode 'alphabetic)

;; try buffer stack add-on (MUST first download buffer-stack.el and place into .emacs.d/addons)
(require 'cl)
(add-to-list 'load-path "~/.emacs.d/addons")
(load "buffer-stack.el")
(require 'buffer-stack)
(global-set-key [(f5)] 'buffer-stack-rebuild )
(global-set-key [(f9)] 'buffer-stack-bury)
(global-set-key [(control f9)] 'buffer-stack-bury-and-kill)
(global-set-key [(f10)] 'buffer-stack-up)
(global-set-key [(f11)] 'buffer-stack-down)
(global-set-key [(f12)] 'buffer-stack-track)
(global-set-key [(control f12)] 'buffer-stack-untrack)


;; fix next and prev buffer commands
(global-set-key (kbd "M-<right>") 'next-buffer)
(global-set-key (kbd "M-<left>") 'previous-buffer)

;; be rid of useless buffers and stop split screen on init
(setq inhibit-startup-screen t)
(custom-set-variables '(inhibit-startup-screen t) )
(add-hook 'emacs-startup-hook
          (lambda () (delete-other-windows)) t)
(kill-buffer "*scratch*")


;; work on sorting buffer list goes here
(defun reorder-buffer-list (new-list)
  (while new-list
    (bury-buffer (car new-list))
    (setq new-list (cdr new-list))
    )
  )

;; buffer name sort idea from https://www.emacswiki.org/emacs/BufferMenuSorting
(defun sort-buffers ()
    "Put the buffer list in alphabetical order."
    (interactive)
    (dolist (buff (buffer-list-sorted)) (bury-buffer buff))
    (when (interactive-p) (list-buffers))
    )

  (defun buffer-list-sorted ()
    (sort (buffer-list) 
  	(function
  	 (lambda
  	   (a b) (string<
  		  (downcase (buffer-name a))
  		  (downcase (buffer-name b))
  		  )))))

(global-set-key "\M-b"    'sort-buffers)

;; set up backtab
(global-set-key (kbd "<backtab>") 'un-indent-by-removing-4-spaces)
(defun un-indent-by-removing-4-spaces ()
  "remove 4 spaces from beginning of of line"
  (interactive)
  (save-excursion
    (save-match-data
      (beginning-of-line)
      ;; get rid of tabs at beginning of line
      (when (looking-at "^\\s-+")
        (untabify (match-beginning 0) (match-end 0)))
      (when (looking-at "^    ")
        (replace-match ""))
      (when (looking-at "^  ")
        (replace-match ""))
      (when (looking-at "^ ")
	(replace-match "")))))

;; set up block tab
(global-set-key [C-tab] 'indent-for-tab-command)

;; set word wrap style as default
(global-visual-line-mode t)

;; stop creating ~ backup files
(setq make-backup-files nil) 

;; set default font and size
(set-frame-font "Ubuntu Mono 11" nil t)
;;(print (font-family-list)) 

;; highlight web links in text
(defun do-web-highlight ()
  (highlight-phrase "http:.*" 'hi-blue)
  (highlight-phrase "https:.*" 'hi-blue)
  )
(add-hook 'find-file-hook 'do-web-highlight)

;;add keys to scroll window up/down by one line
(global-set-key (kbd "M-<down>") (kbd "C-u 1 C-v"))
(global-set-key (kbd "M-<up>") (kbd "C-u 1 M-v"))

;; no line wrap
(global-visual-line-mode -1)
(setq-default word-wrap nil)
(setq-default truncate-lines t)


Page last modified on June 15, 2026, at 03:59 PM