4

The purpose of digit-argument and how to use them is described in Emacs manual.
Also read a question that asks how to disable them.

Even though have not disabled them, I have changed M-1, M-2, M-3 'cause I prefer to use them for delete-other-window, split-windows-right and slit-window-below, which I find more handy.

I also bound M-0 to delete-window, and just wondered...
In which case someone would be interested in using M-0 or C-0?
I cannot think any use case...

  • I think you just answered your own question. If both C-0 and M-0 allow you to specify a numeric prefix argument and you've remapped M-0, you can still use C-0. – wasamasa Jul 28 at 11:18
  • Well, the point is I want to rebind C-0 to switch-to-previous-buffer, because I NEVER use M-0 neither C-0, so that is the reason I am asking the Emacs community, what are they using those for... – nephewtom Jul 28 at 11:32
  • I suggest you to edit the question accordingly, otherwise it's unclear what your problem is. – wasamasa Jul 28 at 12:02
  • What do you find unclear @wasamasa? It seems others did find it pretty clear... – nephewtom Jul 28 at 18:16
5

There is a common convention that a numeric prefix argument means “do it this many times”. If a command follows this convention, a prefix argument of 0 is useless. However, not all commands follow this convention or the other common convention of having two behaviors, one with no argument and one with a prefix argument other than 1.

For example, save-buffer (C-x C-s) uses its prefix argument to determine when to make a backup. A prefix argument of 0 is the only way to specify “don't make a backup this time” (except of course if backups are globally disabled).

npostavs gave another example: kill-line (C-k) kills the text from the cursor to the end of the line with no prefix argument, C-1 C-k also kills the next line, C-- C-k or C-- C-1 C-k kills the previous line and the text on the current line up to the cursor, and C-0 C-k is the shortest way to kill the text on the current line up to the cursor (an alternative way is C-SPC C-a C-w if you don't mind setting the mark and don't want the combining behavior of repeated C-k).

M-0 and C-0 are also useful to type a prefix argument whose value is greater than 9. You can enter 10 as C-1 0 or M-1 0, but C-1 C-0 is more convenient if the main command also uses the Control modifier, and M-1 M-0 is more convenient if the main command also uses the Meta modifier.

You can rebind all of M-0, M-1, …, C-0, C-1, … if you want. Emacs lets you rebind keys pretty much however you want: it's a user preference. You can still type prefix arguments with C-u followed by the digits.

  • Ok, understood. I had already bound them to my preference and will stay that way. Some parts of Emacs are unnatural to me, and do not find them handy. Anyone interested in a new good editor for C/C++ should probably check 4coder. – nephewtom Jul 28 at 18:13
3

@gilles and @npostavs have given use cases. To summarize what they said:

  • When you're already using C- or M- for part of a key sequence, it can be easier to specify a zero prefix arg using C-0 or M-0 than using, say, C-u 0.

  • A zero numeric prefix arg can have a specific (not necessarily numeric) meaning for a given command. @Gilles mentioned save-buffer, and @npostavs mentioned kill-line.

It's also the case that a command could have a zero-arg behavior, a positive-arg behavior, and a negative-arg behavior. E.g., zero could act on the current thing (line, word, sexp, whatever), positive N could act on the Nth thing in one direction (e.g. forward), and negative N could act on the Nth thing in the opposite direction (e.g. backward).

Wrt the second point noted by @Gilles and @npovstas: I disagree with @Gilles that specifying particular behavior for a given prefix arg for a given command in any way flouts Emacs convention. It might not correspond to what most Emacs commands do, but it doesn't violate any convention, as far as I know.

It's true that for most Emacs commands:

  1. A zero prefix arg has no special behavior - it has no effect.
  2. If a prefix arg has an effect then (as @Gilles points out) it is usually one of these:
    • Its numeric value, N, is used by the command (often to perform an action N times or to move forward or backward (abs N) things (chars, words, sexps, lines, etc.).
    • Using a prefix arg has one effect; not using a prefix arg has a different effect.

But a prefix arg can, by design, take several forms, and a command can distinguish them and act differently depending on the form used. Differences include:

  • No prefix arg. Results in the value nil.
  • C--, M--, or C-u -. Results in the symbol -.
  • C-, M-, or C-u, followed by digit keys. Results in the non-negative integer with those digits.
  • C--, M--, or C-u -, followed by digit keys. Results in the non-positive integer with those digits. (But 0 and -0 are the same number, for Emacs.)
  • Plain C-u, possibly used multiple times: C-u C-u, C-u C-u C-u,... Results in value that is a list of an integer that is a power of 4: (4), (16), (256),...

Those are the possible raw prefix arg values. A command can choose to act specially for any of them.

Each of those inputs can also, or alternatively, be interpreted by the command numerically:

  • nil is interpreted as 1.
  • Symbol - is interpreted as -1.
  • An integer is interpreted as that integer.
  • A list of a power of 4 is interpreted as that power of 4.

Those are the possible numeric prefix arg values. A command can choose to act specially for any of them.

There are multiple ways to input the same raw or numeric prefix arg - those results are not distinguishable by the command. But there are also ways to input different raw or numeric args, and those can be distinguished.

A command is free to do whatever its designer likes with a given prefix arg. It could choose, for example, to act one way for even numeric input and another for odd numeric input, taking into account the particular number. Likewise, zero, positive, and negative numbers. Similarly, it can do different things, depending on whether you use C-u (producing (4)) or C-u 4 (producing 4).

There are lots of possibilities. What's important is that the command behavior is clearly documented in its doc string.

See (elisp) Prefix Command Arguments.

2

I cannot think any use case...

It just makes entering numeric prefix arguments a little easier. For example, with numeric argument of 0, C-k (kill-line) will delete from point to the beginning of line. You can invoke this with C-0 C-k which is easier than typing C-u 0 C-k.

  • Well, I find much more easy to use Shift+HOME and then DEL or Backspace key, which by the way, it works in sooo many Windows & Linux applications... – nephewtom Jul 28 at 18:07
  • @nephewtom The point is that some commands do particular things with a numeric argument of zero. kill-line is merely an example. – phils Jul 28 at 22:43
  • Yes @phils, may be... But I do not use them and find it useful to bind it to another command. And for a workflow that needs to use many different applications on Windows, it is simply more handy to have similar keybindings between apps. – nephewtom Jul 29 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.