appearance

Resize your windows to the golden ratio

If you prefer to use a single large emacs frame with multiple windows inside, you might like the golden ratio package. This automatically resizes your windows so that the window containing the point is the largest (size determined by the mathematical golden ratio). This means that the window you are working in is nice and large but you can still see what is going on in the other windows.

Install the package with the following:

(use-package golden-ratio
  :ensure t
  :diminish golden-ratio-mode
  :init
  (golden-ratio-mode 1))
Advertisement

Fun with fonts

I’ve been playing around with some different fonts to see how they look in Emacs. For a long time I’ve been using DejaVu Sans Mono, but I felt like a change. It’s easy to switch the font for the current frame, just use M-x set-frame-font and enter the name of an installed font, or put a line like this

(set-frame-font "DejaVu Sans Mono-14" nil t)

in your scratch buffer and put the cursor at the end of the line, and use C-x C-e to run eval-last-sexp which evaluates that bit of code. This will instantly change the appearance of the current frame.

Here are some of the fonts I’ve been trying out (I installed them using the Font Book on my Mac):

(set-frame-font "DejaVu Sans Mono-14" nil t)
(set-frame-font "Fantasque Sans Mono-16" nil t)
(set-frame-font "Source Code Pro-14" nil t)
(set-frame-font "Monaco-14" nil t)
(set-frame-font "Cousine-14" nil t)

I’ve decided to go with Google’s Cousine font at the moment, so I add the following to my emacs config file to make the choice permanent:

(setq default-frame-alist '((font . "Cousine-14")))

Uniquify your buffer names

If you open more than one file that has the same name (say test.txt), then by default Emacs will add a number to the end of the buffer name to distinguish them, so you would see test.txt <1> and test.txt <2> and so on. This is not very useful as it is easy to lose track of which file is which.

Luckily it is easy to fix with some simple tweaks (I’ve taken these from the configuration files for prelude). Add these to your emacs config file and your buffer names will be made unique by adding just enough of the path to the file. So you might see docs/test.txt and scratch/test.txt. Much nicer!

;; meaningful names for buffers with the same name
;; from prelude
;; https://github.com/bbatsov/prelude
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t)    ; rename after killing uniquified
(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers

Volatile Highlights

The package volatile highlights temporarily highlights changes to the buffer associated with certain commands that add blocks of text at once. An example is that if you paste (yank) a block of text, it will be highlighted until you press the next key. This is just a small tweak, but gives a nice bit of visual feedback.

You can install it in the normal way:

;; volatile highlights - temporarily highlight changes from pasting etc
(use-package volatile-highlights
  :config
  (volatile-highlights-mode t))

Change text size

To change the size of the text in the current buffer, use C-x C-- (i.e. control x then control minus) to decrease the size, and C-x C-+ or C-x C-= to increase the size. To reset to the default, use C-x C-0. These run the command text-scale-adjust which can take further input, so using C-x C-+ and then hitting + again will increase the size a second time, or hitting 0 will reset to default.

You can, of course, change the default text size and font. I add the following to my emacs config file:

;;use larger font
(setq default-frame-alist '((font . "Source Code Pro-14")))

Resize your emacs frame with keyboard shortcuts

I like my emacs windows to be the full height, and 86 columns wide and at the left of the screen if I am using a single monitor, or at the right hand side of the left screen if I am using two monitors.

I have set up a few functions to give me quick keyboard shortcuts to resize and move my frame in this way. These functions require the package frame-cmds (which you can install as usual) to provide the maximize-frame-vertically function.

With these I just use e.g. C-c b <left> or C-c b <S-right> to send my frame exactly where I want it to be.

;;set frame full height and 86 columns wide
;;and position at screen left
(defun bjm-frame-resize-l ()
  "set frame full height and 86 columns wide and position at screen left"
  (interactive)
  (set-frame-width (selected-frame) 86)
  (maximize-frame-vertically)
  (set-frame-position (selected-frame) 0 0)
  )

;;set frame full height and 86 columns wide
;;and position at screen right
(defun bjm-frame-resize-r ()
  "set frame full height and 86 columns wide and position at screen right"
  (interactive)
  (set-frame-width (selected-frame) 86)
  (maximize-frame-vertically)
  (set-frame-position (selected-frame) (- (display-pixel-width) (frame-pixel-width)) 0)
  )

;;set frame full height and 86 columns wide
;;and position at screen right of left hand screen in 2 monitor display
;;assumes monitors are same resolution
(defun bjm-frame-resize-r2 ()
  "set frame full height and 86 columns wide and position at screen right of left hand screen in 2 monitor display assumes monitors are same resolution"
  (interactive)
  (set-frame-width (selected-frame) 86)
  (maximize-frame-vertically)
  (set-frame-position (selected-frame) (- (/ (display-pixel-width) 2) (frame-pixel-width)) 0)
  )

;;set keybindings
(global-set-key (kbd "C-c b <left>") 'bjm-frame-resize-l)
(global-set-key (kbd "C-c b <right>") 'bjm-frame-resize-r)
(global-set-key (kbd "C-c b <S-right>") 'bjm-frame-resize-r2)

Regions, marks, and visible-mark

In my post on sorting lines, a commenter pointed out that a command would work on a region even when it was not active (highlighted). This prompted me to pay a bit more attention to how regions behave in emacs. I found this page of Xah Lee’s useful.

The key concept is that a region almost always exists in your buffer, even if it is not highlighted. The region is defined as the space between the mark and the point. The mark is the last place you placed a mark (e.g. when you use C-SPACE to start marking text), and the point is the location of the cursor. The region is highlighted if it is active, but it always exists even if it is not active and highlighted, even though you can’t see it. Some commands can still act on the region even if it is not active.

It can be confusing at first, as you can’t see the region if it is not active, and can’t generally see the mark. One way to see the current inactive region is to use C-x C-x which runs the command exchange-point-and-mark. From the emacs manual, this does the following:

Set the mark at point, and activate it; then move point where the mark used to be.

So the cursor moves to where the mark was and the region is highlighted for you to see. Using C-x C-x again puts you back where you were.

An alternative is to install the package visible-mark which highlights the position of the current mark, and an optional number of previous marks. I’m not sure I would want to use it all the time, but it is great for getting a sense of where the mark is, and also when the mark changes location, such as when you do a search, or paste text.

You can customise this by adding something like the following (taken from the package documentation) to your emacs config file:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; visible mark - show where mark is                                      ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defface visible-mark-active ;; put this before (require 'visible-mark)
  '((((type tty) (class mono)))
    (t (:background "magenta"))) "")
(require 'visible-mark)
(global-visible-mark-mode 1) ;; or add (visible-mark-mode) to specific hooks
(setq visible-mark-max 2)
(setq visible-mark-faces `(visible-mark-face1 visible-mark-face2))

This shows the two most recent marks with different colour highlights (pink for the most recent and gold for the next most recent). Quite instructive!

Finally, remember that you can use C-u C-SPACE to jump around the previous marks in the buffer, which is great for quickly getting back somewhere useful.