5

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?

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871

3 Answers3

8

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.

NikkiA
  • 1,274
  • 9
  • 6
  • 1
    +1 Now I finally understand what's meant by "two prefix arguments" (as seen in e.g. the magit command magit-pull). BTW, Xah Lee wrote about this and other possible `prefix` values at http://ergoemacs.org/emacs/elisp_universal_argument.html. For instance, `C-u` `-` without a number makes `prefix` equal to the symbol `-`. – echristopherson Sep 30 '14 at 20:14
  • Note that checking the prefix argument as in this example will cause a surprising inconsistency: a number-less prefix argument like `C-u` to work, but an explicitly-numbered prefix like `C-u 4` will be ignored as if there's no prefix. As I understand it, best practice is to first unwrap the prefix if it's a list: `(when (listp prefix (setq prefix (car prefix))))`. Personally, when only increments of prefix argument are significant like this, I would also consider using `<=` instead of `equal` for comparisons, so that explicit numbered prefixes between the powers of 4 worked intuitively too. – mtraceur Apr 27 '23 at 03:03
6

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.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
0

Another way to look at it is that a prefix arg lets you roll two or more different but similar/related commands into one, and put them on similar but related keys.

  • Put the most commonly used one on KEY, for some KEY.
  • Put another one on C-u KEY.
  • Put another one on C-- KEY.
  • Put another one on C-+ KEY.

Or put many of them on different numeric prefixes: ..., C-u -2 KEY, C-u -1 KEY, C-u 0 KEY, C-u 1 KEY, C-u 2 KEY,...

Drew
  • 29,895
  • 7
  • 74
  • 104