Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose/usage of prefix argument with emacs?

Tags:

emacs

As I asked here, I can choose the lisp for running SLIME with prefix argument(C-u), and I is written in here, I see that C-u is a way to insert the output in the current buffer.

I use 'C-u 10 SOMETHING', in order to run SOMETHING 10 times.

What's the usage/purpose of using prefix argument (C-u)? Is C-u the only prefix argument?

like image 929
prosseek Avatar asked Aug 25 '10 20:08

prosseek


People also ask

What is prefix command?

A command prefix is a prefix of a command (in the sense of rule 2 of the dodekalogue, i.e., a sequence of words) that is constructed with the expectation that zero or more arguments will be appended to it and that the resulting command will be evaluated.

What does CU do in Emacs?

Like all other commands in Emacs, these commands can be given arguments which cause them to be executed repeatedly. The way you give a command a repeat count is by typing C-u and then the digits before you type the command.


2 Answers

Also bear in mind that C-u as a prefix on its own defaults to passing '(4) as the argument, and each C-u that you insert before the command multiplies this by 4.

Thus you can write a quick way of having a few simply choices that are selected between by using ctrl-u prefixes, for example:

(defun insert-date (prefix)
  "Insert the current date. With prefix-argument, use ISO format. With
   two prefix arguments, write out the day and month name."
  (interactive "P")
  (let ((format (cond
                 ((not prefix) "%A, %d %B %Y %H:%M %Z" )
                 ((equal prefix '(4)) "%d/%m/%Y %H:%M")
                 ((equal prefix '(16)) "%d/%m/%Y")
                 ((equal prefix '(64)) "%H:%M:%S")
                 ))
        (system-time-locale "en_GB"))
    (insert (format-time-string format))))

(global-set-key (kbd "C-c d") 'insert-date)

(The above elisp produces a function that inserts a long format date on the key (in this case C-c d), the date + time in short format on C-u C-c d, the short format date on C-u C-u C-c d, and the short format time on C-u C-u C-u C-c d)

You could use this trick to make a 'start-slime' replacement that used clojure by default, but sbcl if you press C-u before the key binding.

like image 59
NikkiA Avatar answered Oct 03 '22 17:10

NikkiA


Check out the documentation for prefix command arguments.

But, in short, it's a way to interactively provide more information to the command.

  • for alpha-numberic keys, generally bound to 'self-insert, it tells them how many of that character to insert
  • for some commands (M-x comment-region) it means to invert the command, i.e. C-u M-x comment-region uncomments the region.
  • for some it just tweaks the behavior, C-u C-s does a 'isearch-forward-regexp instead of a regular 'isearch-forward.

So, it all depends on how the command uses the prefix argument.

As far as other "prefix arguments", there are C--, M--, M-3 (or any other digit), and some others.

like image 31
Trey Jackson Avatar answered Oct 03 '22 18:10

Trey Jackson