Month: November 2015

Get pop-up help for keybindings with which-key

In my post on showing unfinished keybindings early, commenter hmelman points out which-key which is a package that give you pop-up help for partially completed keystrokes. Simply install the package from MELPA and add

(which-key-mode)

to your emacs config file. Now type a prefix key like C-x and wait one second (this delay can be customised) and a new window will open with a summary of the possible completions for that prefix. There are some nice screenshots at the project web page so look at those to get the idea.

This is a great way to remind yourself of commands or discover new ones. For example try C-x 8 and wait to see what pops up – I’m sure you’ll find some commands under that prefix that you didn’t know about!

Advertisement

Show unfinished keybindings early

When you start typing a keybinding that has several parts to it (like C-x C-s to save), if you pause after entering the first part (called the prefix key, C-x in this example), then emacs shows the bit you have typed so far in the echo area (the line at the bottom of the screen where messages appear).

By default, emacs waits 1 second before echoing the keystroke, but I like to see it sooner. Of course this can be customised – add the following to your emacs config file to set it to 0.1 seconds:

;; Show unfinished keystrokes early.
(setq echo-keystrokes 0.1)

Update

Commenter hmelman points out which-key which is a package that give you pop up help for partially completed keystrokes. For many, this probably supersedes the echoing of help above, and is worthy of its own post.

Delete (non) matching lines

Recently I needed to clean up a pile of text I had pasted into emacs from a pdf. One of the things I wanted was to remove all lines that didn’t start with a comma. Emacs of course provides functions for this, so I highlighted the text and used M-x delete-non-matching-lines ^,

The ^, is a regular expression that matches lines starting with a comma. Emacs also offers the command delete-matching-lines which does what you would expect.

Org-mode basics VI: A simple TODO list

In the first part of my series on org-mode, I described how to create a rich structured notebook that can be exported to various useful formats. In the next few posts in this series I’m going to talk about another essential way I use org-mode, which is to organise myself! I use org-mode to manage my (depressingly long) task list including scheduled tasks and deadlines, to export a calendar feed for events, and to quickly capture useful information including emails and scanned documents. If you want to see a very advanced use case, look at Bernt Hansen’s Org Mode – Organize Your Life In Plain Text.

In this post, we’ll start by looking at setting up a simple todo list, and we’ll cover some of the more advanced topics later.

To start with, add the following code to your emacs config file:

;; set key for agenda
(global-set-key (kbd "C-c a") 'org-agenda)

;;file to save todo items
(setq org-agenda-files (quote ("/Users/bjm/todo.org")))

;;set priority range from A to C with default A
(setq org-highest-priority ?A)
(setq org-lowest-priority ?C)
(setq org-default-priority ?A)

;;set colours for priorities
(setq org-priority-faces '((?A . (:foreground "#F0DFAF" :weight bold))
                           (?B . (:foreground "LightSteelBlue"))
                           (?C . (:foreground "OliveDrab"))))

;;open agenda in current window
(setq org-agenda-window-setup (quote current-window))

;;capture todo items using C-c c t
(define-key global-map (kbd "C-c c") 'org-capture)
(setq org-capture-templates
      '(("t" "todo" entry (file+headline "/Users/bjm/todo.org" "Tasks")
         "* TODO [#A] %?")))

This sets up the file in which we will save our todo items (put your own choice of file here), configures a few other options, and sets up a capture template to quickly add todo items to the list (put the same file name for your todo file here too). Now you can highlight the code in your config file and use M-x eval-region or restart emacs to pick up the changes.

Once you have done this you can add your first todo item using C-c c t, which will pop up a small window with a prompt like this

** TODO [#A]

You can see this looks like an org-mode headline, and you should add your todo item and any notes to go with is to this like so:

** TODO [#A] make a todo list
Some notes here about how to do it

Then hit C-c C-c to save this item. The nice thing about this method of adding items (called org-capture) is that you can add an item from anywhere in emacs and get right back to what you were doing afterwards.

By default our item was given priority A, but you can change this easily by hitting shift and up or down arrow to cycle through the priority levels (I find that three levels, A-C is enough for me).

Let’s add a priority B item:

** TODO [#B] add another item to my list

Now we can have a look at our todo list using C-c a to launch the “agenda dispatcher”, a powerful interface for selecting different ways to view your tasks. For now we’ll just hit t in the dispatcher to view the todo items (i.e. use C-c a t). This switches to a buffer with our todo list – in this list view, you might want to:

  • Cross an item off your list (the best bit!). To do this put the cursor on the corresponding line and hit $ which marks it as done and archives the item in a file called todo.org_archive getting rid of it from your todo list.
  • Change the priority of an item using shift up/down.
  • View the notes to go with items by hitting E.
  • Edit or view an item in more detail by hitting RET with the cursor on the item that you want. This takes you to the item in your todo.org file where you can edit it or look at the notes you added to it in more detail.
  • Quit back to where you were before with q

That is all there is to it, and you now have a simple but powerful todo list in emacs. Just remember C-c c t to create a todo item and C-c a t to view the todo list.

That’s all for now. In the next part we’ll look at scheduling and deadlines.

Move through edit points

We’ve looked before at how emacs leaves a trail of breadcrumbs (the mark ring) through which you can navigate to hop around to places you’ve been in the buffer.

A nice alternative is to move round through points at which you made edits in a buffer. You can do this by installing the package goto-chg. Set it up by adding the following to your emacs config file:

(require 'goto-chg)
(global-set-key (kbd "C-c b ,") 'goto-last-change)
(global-set-key (kbd "C-c b .") 'goto-last-change-reverse)

Now you can use C-c b , and C-c b . to go back and forth through the edit points in your buffer. It takes you through your undo history without actually undoing anything.

For bonus points you can use C-u 0 C-c b , to give a description of the change you made at a particular stop on your tour.

Join line to following line

I wrote a while ago about using M-^ to join the current line to the previous line. In fact, when editing I find that I most often want to join a line to the following line. I used to do this by using C-e to move to the end of the line and then C-d to delete until I got the next line joined to the current one. It is easier to do this with a single key using a simple function, which I set to C-j. Add the following to your emacs config file:

;; join line to next line
(global-set-key (kbd "C-j")
            (lambda ()
                  (interactive)
                  (join-line -1)))

You can keep hitting C-j to keep joining the next line.

Update

I originally used M-j as the keybinding for this, but Kaushal Modi pointed out in the comments that C-j is a better choice.

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")))