0

I want to be able to change a global keybinding by calling an Emacs function.

The command in question is:

gsettings set org.gnome.desktop.wm.keybindings switch-applications "['<Super>Tab']"

This Linux shell command sets the keybinding for changing between applications.

When I call shell-command within Emacs and enter the above command, the expected result always happens: application switching becomes bound to Super-Tab.

The correct form for a simple shell-command is (shell-command "command"). I have successfully tested that this works by evaluating (shell-command "ls").

However, when I evaluate the function

(shell-command "gsettings set org.gnome.desktop.wm.keybindings switch-applications "['<Super>Tab']"")

instead of this working as it does when I offer this string directly to shell-command prompt, I receive the error message: eval-region: Invalid read syntax: "]".

I have tried every syntactic variation of this command that I can think of and read threads and blog posts discussing simple uses of shell-command, but I have found nothing applicable to this problem.

I would be grateful for any advice.

2
  • 1
    Just checking, but are you backslashing the double quotes? As in (shell-command "gsettings set org.gnome.desktop.wm.keybindings switch-applications \"['<Super>Tab']\"")? Otherwise the string representing your command is ending prematurely at the first double quotes. Commented Feb 28, 2023 at 17:06
  • @kozina-adjacent: please make your comment into an answer. It is without a doubt the correct answer.
    – NickD
    Commented Feb 28, 2023 at 17:31

1 Answer 1

3

When including double-quotes inside a string, be sure to "escape" them, which in Emacs is accomplished with a backslash, otherwise the string representing your command will end prematurely. (Well, at least earlier than you probably intended it to.) See String syntax in the Emacs manual (or in Emacs: C-h r i string syntax <RET>).

In your case, the original command includes double-quotes:

gsettings set org.gnome.desktop.wm.keybindings switch-applications "['<Super>Tab']"

...which need to be backslashed in the string argument to shell-command:

(shell-command "gsettings set org.gnome.desktop.wm.keybindings switch-applications \"['<Super>Tab']\"")

3
  • Thank you; this answer, as well as solving the problem, is something I didn't know that I didn't know.
    – Orixi
    Commented Feb 28, 2023 at 19:22
  • @Orixi Glad I could help. By the by, you can access the Emacs manual in Emacs itself with C-h r (C-h is a prefix argument that directs to many help functions.) Once the Emacs manual is open, you can hit i to search for a topic like string syntax. And welcome to the forum and Emacs! Commented Mar 1, 2023 at 9:06
  • 1
    @Orixi Note that literally every delimited format (not only within Emacs) has this requirement if the delimiters themselves are to be allowed within the delimited text. The concept of encoding a would-be special character to prevent it being special is known as "escaping" (not uncommonly using backslashes, but it varies).
    – phils
    Commented Mar 1, 2023 at 10:48

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.