1

Inside my .emacs file I have these two functions:

(defun bh/switch-to-vs ()
  (interactive)
  (universal-argument)
  (shell "*vs*"))

(defun bh/switch-to-android ()
  (interactive)
  (universal-argument)
  (shell "*android*"))

I bind keys to those two methods like this:

(global-set-key [f1] 'bh/switch-to-vs)
(global-set-key [f2] 'bh/switch-to-android)

All working fine, but I prefer defining a function which takes the name of the shell as an argument. I need something like this:

(defun bh/switch-to (shell_name)
  (interactive)
  (universal-argument)
  (shell shell_name))

However, I have trouble calling this method from inside (global-set-key [f1] 'bh/switch-to vs) // not sure how to call it, getting various errors.

How to make this work?

1 Answer 1

1
(defun example-one (&optional shell-name)
"Example where `current-prefix-arg' is hard-coded to be non-nil."
  (interactive)
  (let ((current-prefix-arg t)
        (shell-name (or shell-name (read-string "Shell Name:  "))))
    (shell shell-name)))

(defun example-two (&optional shell-name)
"Example where the user has the option of using a universal argument:  C-u"
  (interactive)
  (let ((shell-name (or shell-name (read-string "Shell Name:  "))))
    (shell shell-name)))

(global-set-key [f1] 'example-one)

(global-set-key [f2] 'example-two)
2
  • Thank you but what I want to do is something like this: (global-set-key [f1] '(example-one "android")) - If I do this I get an error message Wrong type argument: commandp, (example-one "android"). What is wrong here?
    – W.M.
    Commented Sep 5, 2021 at 16:49
  • 3
    I have found the needed code, it is: (global-set-key [f1] (lambda () (interactive) (example-one "android")))
    – W.M.
    Commented Sep 5, 2021 at 16:54

Your Answer

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

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