2

I am trying to create a command that will indent a selected region by 4 spaces.

The appropriate commands are: C-u 4 C-x <TAB>, when C-u is a shortcut for "universal-argument" command and C-x <TAB> is a shortcut for indent-rigidly, so I've written this function:

(defun my-tab ()
  (interactive)
  (universal-argument 4)
  (indent-rigidly))

But when I'm trying to run the function (with M-x my-tab) I get this error:

Wrong number of arguments: (0 . 0), 1

What is the problem?

Thanks!

2
  • 1
    When you want to turn a key sequence into a function, it might be easier to record a macro: hit F3 to start recording, type the keys in question, hit F4 to stop recording the macro, and then name and save the macro in your .emacs file. Commented Jun 8, 2017 at 11:07
  • Questions about such an error msg are common. For some reason the questions often don't mention the function that got the wrong number of args. People who answer don't have a problem with this. But you and other askers can learn how to get the answer yourself, by looking at the name of the function cited in the error msg - in this case indent-rigidly. Then use C-h f to look at its doc and see just what that function expects as arguments. Emacs really tries to give you all the help you need to fix the problem. But you have to look at what it tells you - in this case, the function name. Commented Jun 8, 2017 at 13:54

1 Answer 1

9

If you look at the documentation for indent-rigidly (C-h f indent-rigidly), you'll notice that it takes 3-4 arguments:

(indent-rigidly START END ARG &optional INTERACTIVE)

So, you should supply the start and end positions to it as well. You should also just give the ARG normally, instead of using universal-argument.

(defun my-tab (start end)
  (interactive (if (use-region-p)
                   (list (region-beginning) (region-end))
                 ;; Operate on the current line if region is not to be used.
                 (list (line-beginning-position) (line-end-position))))
  (indent-rigidly start end 4))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.