I wanted to define a function to execute a command multiple times and ended up using fset:

(fset 'my-shrink (kbd "C-u 43 C-x {"))

Now, I want to include this in a function, like this:

(defun my-todo ()
  (find-file "~/Dropbox/orgfiles/tasks.org")
  (my-shrink)             ; this won't work

but it failed with "Invalid function: my-shrink". Apparently, my-shrink is not a function, so I can I call it in a function? Or, alternatively, how to do something similar to my-shrink in a function? BTW, all my-shrink does it to run shrink-window-horizontally 43 times.

  • Since you've accepted an answer that doesn't at all say "how to execute an fset command in a function", please change your question and its title, to say that you're looking for how to shrink a window or whatever... The Q&A should be clear, so that it can help others.
    – Drew
    Mar 3 at 15:56

Well, you could define it this way:

(defun my-shrink ()
  (dotimes (_ 43)
    (shrink-window-horizontally 1)))

But that's not quite as elegant as this:

(defun my-shrink ()
  (shrink-window-horizontally 43))

And since that's so short, maybe you don't even need to define anything at all. Just call (shrink-window-horizontally 43) in my-todo.

  • Thanks. I didn't realize that shrink-window-horizontally takes arguments. Not I also learned a way to repeat any command multiple times in a function.
    – Jordan He
    Mar 3 at 5:03
  • 2
    You can find that sort of information by using the C-h f command, then typing in a function name. It will show you the argument list and the documentation of the function, including links to the source code and the manual.
    – db48x
    Mar 3 at 5:06

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.