emacs lisp call function with prefix argument programmatically

If I'm understanding you right, you're trying to make a keybinding that will act like you typed C-u M-x grep <ENTER>. Try this:

(global-set-key (kbd "C-c m g")
                (lambda () (interactive)
                  (setq current-prefix-arg '(4)) ; C-u
                  (call-interactively 'grep)))

Although I would probably make a named function for this:

(defun grep-with-prefix-arg ()
  (interactive)
  (setq current-prefix-arg '(4)) ; C-u
  (call-interactively 'grep))

(global-set-key (kbd "C-c m g") 'grep-with-prefix-arg)

Or you could just use a keyboard macro

(global-set-key (kbd "s-l") (kbd "C-u C-SPC"))

In this example, the key combination "s-l" (s ("super") is the "windows logo" key on a PC keyboard) will go up the mark ring, just like you if typed "C-u C-SPC".

Tags:

Emacs

Elisp