spacemacs: how to define a new key-binding with a leading [SPC]? - emacs

I would like to bind a set of additional keys for spacemacs. The following statement is used:
(global-set-key (kbd "SPC-1") '(fzf/start "01-personal"))
yet it does not work that Emacs complains that SPC is not a prefix key. Just wonder how spacemacs is able to do it. Did some searches, but didn't find the information for it. Can anyone help?

A more complete answer, is to first declare a prefix, and then set leader keys. For example:
(spacemacs/declare-prefix "o" "own-menu")
(spacemacs/set-leader-keys "os" 'ispell-buffer)
Using "o" as a prefix is a good idea, as it is guaranteed to be available for customization. Other prefixes might be used by different layers.
You can also add nested prefixes, for example I use the following to work with IDs in orgmode:
;; org-ids
(spacemacs/declare-prefix "od" "id")
(spacemacs/set-leader-keys "odc" 'org-id-copy)
(spacemacs/set-leader-keys "odu" 'org-id-update-id-locations)
Which allows me to press SPC o d c to copy an Org header id (and create one if it doesn't already exist).

Just did some further search, the right way to set such keybinding under spacemacs is:
(spacemacs/set-leader-keys "1" 'keymap)

Related

How do create a new file that matches an existing file in Emacs Prelude?

In Emacs Prelude, I cannot figure out how to create a new file when a file exists that "matches" the name of the file.
In the example screen capture, I am trying to create a file called spawn.exs in a directory that already contains a file called spawn_basic.exs. When I use C-xC-f to create a new file, emacs is matching spawn.exs to spawn_basic.exs and hitting Enter results in opening spawn_basic.exs instead of creating spawn.exs.
I've tried some workarounds (i.e. using touch to create the file) but I'm looking for a simpler solution that would require only one command. If there is not a command to do this, is there a way to disable the file name matching feature in Prelude? I like most everything else about Prelude compared to vanilla Emacs so I would rather not switch back.
In Prelude, C-x C-f is bound to helm-find-files. It's usually
great, but for this case I don't see a good workaround for bypassing
its match (as you've described).
However, ido-find-file has a similarly friendly completion system,
yet offers a nice solution to bypass the completion. When you type
spawn.exs, open by pressing C-j instead of RET. I think Helm
should do this too (instead of treating them the same), so you might
want to submit a feature request (to match Ido's "verbatim entry").
Ido works well as a find-file replacement, and it's recommended over
Helm for simple cases like this
in Mastering Emacs. So you could
remap it with:
(global-set-key (kbd "C-x C-f") 'ido-find-file)

How to create a layer keybinding to execute a shell command in Spacemacs

I want to add a keybinding in dotspacemacs/user-config, to execute and external "elm-format" command, in the elm-layer, with some parameters:
elm-format --yes current-file.elm
I couldn't find how to do it, what I found on how to define keybindings is:
(define-key elm-mode-map (kbd "f") 'elm-format)
So I'm not sure about somethings here:
How do I know which keymap should I be adding a keybinding to? It
wasn't clear by just reading the elm layer code [1]
Reading the elm layer code, the way they define what the keybinding should is with a single quote prefix " 'elm-package-refresh ", but that's not
a command at all so I guess it's referencing something external? How should I put a command there?
Let me know if you need any further clarifications.
[1] https://github.com/syl20bnr/spacemacs/tree/master/layers/%2Blang/elm
How do I know which keymap should I be adding a keybinding to?
If you use emacs 25 or later, you can use SPC h k d to describe elm related keys in elm-mode and the keymap information is contained along with the key description.
How should I put a command there?
You can always write a command wrapper for this kind of stuff.

User super key for meta commands in emacs

I'm trying to learn emacs and would like to use the super key on my keyboard for any meta commands in emacs, because I use dwm as my window manager and it uses the meta key extensively. I have read this page, using it to try
(setq x-meta-keysym 'super)
(setq x-super-keysym 'meta)
with no luck. Ideally I'd like to simply flip them, but only in emacs. when I inspect the variables x-meta-keysym and x-super-keysym with C-h v the commands do seem to be evaluated correctly. When I check the super key with the linux utility xev it confirms that the key I'm pressing is mapped to Super_L.
I agree with #Tyler
In my case I use dwm as window manager, so meta (ALT) key is used to control almost all its features. When using emacs, meta key conflicts with some key bindings, eg: when I'm moving around words M-f or Mb (that's why I came to this thread ;)), so I found three choices to get rid with this issue:
1) change emacs key binding and use super key as meta key
(setq x-meta-keysym 'super
x-super-keysym 'meta)
Both symbols (variables) are required, because if you just use only x-super-keysym 'meta, it enables both keys as meta key. If you just use only x-meta-keysym 'super, it disables both and de command buffer will return s-x is undefined.
2) change dwm key binding for meta to super key in config.h file. take a look in http://dwm.suckless.org/customisation/windows_key
/* key definitions */
-#define MODKEY Mod1Mask /* meta (ALT) key */
+#define MODKEY Mod4Mask /* super (windows or cmd) key */
3) override xmodmap keymap bindings swapping mod1 to super and mod4 to meta (be sure to check what xmodmap returns at first glance, and not affect keybindings of other programs)
Finally, I recommend not to change default emacs meta key because is less comfortable to your fingers and may cause you to respond slowly to keystrokes. Try options 2 or 3 depending your environment.
So, it turns out that the above commands work when emacs is not used in terminal mode. I had been trying it in the terminal exclusively which is why it wasn't working. I'm not sure how to fix it in the terminal, but it's good enough I suppose.
edit: it was the terminal emulator that was gobbling the keypresses before they got to emacs, so the original commands do work, so long as the keypress actually makes it to emacs.

Find a good hotkey for new emacs commands

In Emacs everything is about hotkeys. Everybody who creates new shortcuts knows the problem:
Which hotkeys should be used?
Which hotkeys are used by the most popular Emacs extensions (org-mode...) and should be avoided?
Are there reserved hotkeys for "users" that will never be used by extensions?
Which hotkeys should be avoided, if the code should be public? (Some keys like right/left Win are sometimes missing on keyboards, M-TAB will be catched by the windowmanager)
Is there a list of all reserved hotkeys?
Sticking to the reserved C-c <letter> sequences is as close as you'll get to guaranteeing that you won't conflict with any other code (although it's still just a convention; sometimes you'll see people making code available that uses one of those sequences, but you can report those cases as bugs to the author).
I would suggest using some of the C-c <letter> sequences as prefix bindings for grouping related functionality together. For instance, you might use C-c w <key> as the pattern for window-related functionality. That gives you a dramatically larger number of reserved bindings, probably with better mnemonic properties, and of course the subsequent <key> can be anything at all, not just a letter.
That also lets you use C-c <letter> C-h to list everything you've bound under that prefix, which can be convenient.
The GNU Emacs Lisp Reference manual has a detailed page on key binding conventions:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html
The keys C-c <letter> are reserved for the users, so packages should normally not use them (note that "letter" is really a letter of the alphabet, not "any key" or "any character"). But most users just use which ever key they like and when that binding conflicts with some package, they handle the conflict in an ad-hoc way (either by using another key, or by specifically overriding the package's binding).
automate key-binding checks evaluating forms like
(key-binding [(control c) (delete)])

Emacs How to redefine Shift-R for expected use

I've checked my elisp files to make sure that I do not have any bindings that contain Shift+R (and I have not found any). I expect SHIFT+R to print an uppercase character, but instead I get R R undefined inside of the Emacs command line. This is only in C/C++ major modes.
Any suggestions?
Update: Describing the key shows that it is undefined. How would I define it for the normal, expected use (capitalizing the letter R)?
I assume by the 'expected use' you mean to insert the 'R' character. For this, you'd need to bind the key to 'self-insert-command':
M-x global-set-key R self-insert-command
Or, in your .emacs or .emacs.d/init.el file:
(global-set-key "R" 'self-insert-command)
Of course, this should be the default....
I'm getting a little deja-vu here and if memory serves the behavior I encountered some years ago was that (on Windows) certain accessibility settings unset or changed the keycode for the right shift key. Sorry I cannot be more specific but maybe this will stimulate someone else to come up with the real answer. A test you can make: does the behavior work with both shift keys or just one? If the answer is just one shows the bad behavior, is that bad behavior shown with all keys?
Try C-h k (describe-key), then press Shift-R. describe-key will then tell you what is bound to that key. At least that will give you a hint as to whether or not there is an active binding. If there's a binding, perhaps it will give you a hint of something else to search for in your startup files.
You sound like you're having the same problem I had. Typing Re... in any html buffer would try to execute an R- command, when every single R-* command was undefined. Turned out that I had a typo in my .emacs file. I had a global-key-map set to (kbd "REF") instead of (kbd "RET"), and fixing it made the problem immediately vanish. So I'd recommend checking for anything similar in your .emacs file.