Month: July 2015

Cut or copy current line with easy-kill

I’ve previously written about how to cut the current line without selecting it, using some custom lisp code. I’ve since noticed that my recommended setup, prelude already deals with this in a nicer way by using the package easy-kill. If you don’t use prelude, then install easy-kill and add

(global-set-key [remap kill-ring-save] 'easy-kill)

to your emacs config file.

Once you have done this, then C-w with no region selected will cut (kill) the current line, and M-w with no region selected will copy the current line. This is one of those neat shortcuts I use all the time and miss whenever I have to edit text outside of emacs!

The easy-kill package also offers the easy-mark function, that expands highlighted regions by word, line or other useful units, but I prefer expand-region for this purpose.


Basic spell checking

Use M-x ispell-buffer to run spell-check on the whole buffer, or M-$ to check the spelling of the word at the cursor position. The spell check will suggest corrections for the words, or let you save the word to your personal dictionary (by hitting i).

Emacs can use several back-ends to do the actual spell checking, and there is a good comparison of them here. For myself, I have found hunspell to give the best suggested corrections. I installed hunspell with macports

sudo port install hunspell

and then added the following to my emacs config file:

;; spelling                                                               ;;
;; use hunspell
(setq-default ispell-program-name "hunspell")
(setq ispell-really-hunspell t)
;; tell ispell that apostrophes are part of words
;; and select Bristish dictionary
(setq ispell-local-dictionary-alist
      `((nil "[[:alpha:]]" "[^[:alpha:]]" "[']" t ("-d" "en_GB") nil utf-8)))

Use LaTeX syntax to enter non-standard characters

Sometimes it is handy to be able to enter non-standard characters in emacs documents. For example I sometimes need to use a £ symbol but my US keyboard doesn’t have one. Other times I need to add an accent or similar to a letter like é or ö. There is a nice overview of the ways to do this at Mastering Emacs, and here I’ll pick out the method I find most useful.

Since I use LaTeX a lot and am familiar with its syntax for symbols, I use C-\ to run the command toggle-input-method and then enter TeX as the alternate input method. Now I am in TeX input mode I can enter a string like \pound and have it appear as £ or \"o to give me ö. Pressing C-\ again now toggles back and forth between normal and TeX input modes.

For symbols you use a lot, you can also set up abbreviations e.g. by entering gbp C-x a - C-\ \pound we set the text gbp to expand to £.

Use abbreviations to expand text

Emacs has the ability to use abbreviations to save time typing. For example, if I type fn and then press space or almost any punctuation character, it is automatically expanded to the word function. This is also great for correcting common typos, like “teh” for “the”.

To make this work the most seamlessly, paste the following lines into your emacs config file, changing the location of your abbreviation file if you want:

;; abbreviations                                                          ;;
(setq-default abbrev-mode t)
;; save abbreviations upon exiting xemacs
(setq save-abbrevs t)
;; set the file storing the abbreviations
(setq abbrev-file-name "~/docs/my-abbreviations.el")
;; reads the abbreviations file on startup

and then highlight the lines and run M-x eval-region or restart emacs.

Now you can type the abbreviation you want, followed by C-x a - and you will be prompted for the expanded text. e.g. to set up the abbrevation above, I used

fn C-x a - function

You can set as many of these as you want. You will be asked if you want to save your abbreviations when you exit emacs.

If you type something that you don’t want to be expanded, then follow the abbreviation with C-q, so typing fn C-q SPACE will give you the string fn followed by a space.

Scrolling and moving by line

There are (of course) lots of ways to scroll the window and move the cursor in emacs. To move the cursor up or down by a line you can use the arrow keys, or C-n and C-p.

To scroll the window, you can use C-v and M-v to scroll down or up respectively by nearly one full screen. This is equivalent to page up/down.

Normally when you scroll the window, the cursor stays with its current line until it hits the edge of the window and then it moves to the next line. In other words the cursor follows the text as the text scrolls. I prefer the cursor to stay at the same position in the window when I scroll it, so the text moves under the cursor. I also like to add the following simple commands which scroll the window up or down by one line. Putting these two tweaks together, the window moves by one line while the cursor stays still.

;;keep cursor at same position when scrolling
(setq scroll-preserve-screen-position 1)
;;scroll window up/down by one line
(global-set-key (kbd "M-n") (kbd "C-u 1 C-v"))
(global-set-key (kbd "M-p") (kbd "C-u 1 M-v"))

Note how these commands work by passing a prefix argument to the normal scroll commands – this is described in the help for those functions which you can see by using e.g. C-h k C-v.

I used the keybindings M-n and M=p by analogy with C-n and C-p.

Here is a little animation to illustrate how this works.


Convert tabs to spaces

To replace tabs with the appropriate number of spaces, use M-x untabify.

To do the reverse and convert multiple spaces to tabs, you can use M-x tabify.

Both commands work on a region. To run on the whole buffer use a prefix argument (i.e. C-u M-x untabify).

Append to kill-ring

We previously looked at cycling through the history of the kill-ring (emacs’ clipboard). A somewhat related idea is that you can append text that you cut or copy onto the last entry of the kill-ring so that you can accumulate several pieces of text into a single clipboard entry.

You do this by preceding a cut or copy command with C-M-w (this also happens automatically if you use successive cut/copy commands without anything else in between).

This is explained in the emacs manual, but as a simple illustration in the animation below I cut the line bbbbbbb and then move to the ddddddd line and hit C-M-w before cutting that line. My paste command then pastes the two lines.