开发者

What's the purpose/usage of prefix argument with 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 开发者_StackOverflow中文版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?


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.


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.


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,...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜