I would like to use C-u in a function (for instance, regexp) where calling it with C-u has a different effect. How can I do this in Emacs? The documentation doesn't show how to do this with Emacs Lisp.

(defun test ()
  (interactive)
  (align-regexp)) ; I would like to add the C-u prefix to this.
up vote 14 down vote accepted
(defun my/test ()
  (interactive)
  (let ((current-prefix-arg 4)) ;; emulate C-u
    (call-interactively 'align-regexp) ;; invoke align-regexp interactively
    )
  )

Hope that helps.

  • Excellent. Does what it has to and with some explanation. – PascalVKooten Oct 11 '12 at 13:45
  • 4
    Note that the necessary argument value to emulate C-u varies, depending on the expectations of the original function (i.e. its argument to interactive). If it accepts a "raw" prefix argument, then you would want to pass the list '(4) rather than the numeric value 4. See C-h i g (elisp) Prefix Command Arguments RET – phils May 24 '14 at 11:07
  • I don't understand, it seems this is not the poster asking for. I think it should be: do one thing if not called with C-u, and do another thing once called with C-u. – CodyChan Apr 9 at 14:49
  • Thanks, I used this with org-journal to suppress the automatic time-entry creation. – DuckPyjamas Jun 24 at 22:50

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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