2008-12-19

prefix arguments

You can jump to the next line in emacs with C-n ('next-line'). However, how can we specify that we want to jump to the nth next (or previous) line? Or, more in general, how can we specify a (numerical) argument to an interactive function that support them?

The answer is that you can provide prefix-arguments to such functions; this can be done in two ways:

  • positive numeric arguments can be specified with M-<number>; that is, to use 5 as the prefix argument, you'd type M-5;
  • any numeric argument can be specified using C-u; so to set -3 as the prefix argument, you'd type C-u -3. Some functions also take C-u as a kind of boolean parameter; you set the parameter by specifying C-u without any parameter.
Using this, we can jump to the fifth next line with M-5 C-n, and to the third previous line with C-u -3 C-n. For details, see the emacs manual, and the documentation for the specific function (use M-x describe-function or C-h f).

Note: after ten years of using emacs, I discovered the M-<number> notation only when writing this entry...

2 comments:

Anonymous said...

"Note: after ten years of using emacs, I discovered the M-number notation only when writing this entry..."

That happens a lot ;), great post!

Anonymous said...

Since you missed the M-number feature for 10 years: Is ist really usefull for you? Or in which way is it usefull?

I for instance know it since years but use it only in rare occasions: a) to iterate a simple keybord macro or b) to jump to a specific line in the buffer (since i cannot remember the M-g M-g key combo).

As a blog posting i would find it more interesting to learn ways in which people (you) actually use a function as opposed to a "neutral", "objective" description of a feature which i also can read in the manual.

Thanks, Gregor