2

I am trying to apply following solutions's keybinding to overwrite all mode's keybindings for C-c > and C-c <.


From the answer for Can we do C-c > and > and > to continue indentation:

(defhydra python-indent (global-map "C-c")
  "Adjust python indentation."
  (">" python-indent-shift-right "right")
  ("<" python-indent-shift-left "left"))

which works for the python-mode; but it does not overwrite into thhe global bindings and such as in shell-mode, its bind remain as to sh-learn-line-indent.

Is there any way to force to overwrite the keybinding for C-c > and C-c <?


I have also tried following with the help of (https://emacs.stackexchange.com/a/68029/18414), which did not work:

(with-eval-after-load "sh-script"
    (defhydra python-indent (sh-mode-map "C-c")
      (">" python-indent-shift-right "right")
      ("<" python-indent-shift-left "left"))
4
  • A lower level command (and the one that python-indent-shift-right/left is using) is indent-rigidly, which already provides a transient when called interactively. There is also indent-rigidly-right-to-tab-stop (and left), which might be a better command to use if you would like to have different tab offset's (besides its name making more sense when using this globally). Aug 13 at 7:41
  • I just noticed that the transient provided by indent-rigidly, provides indent-rigidly-right-to-tab-stop (and left) under capital L/H, (so probably it is a little redundant to mention that you could use the transient package (instead of hydra) if you would like a way to set the offset interactively via the transient). Aug 13 at 7:56
  • Hm what change should I make to work better?
    – alper
    Aug 13 at 8:03
  • 'indent-rigidly` only indents when some region is active. Furthermore, it behaves weird when trying to modify the command. So in the end, I guess you are using the best (most practical) method already. Aug 13 at 10:06
1
+50

Update:

Since you're using a Hydra, I think the best way to do this is to create a minor mode, and then define the hydra in that minor-mode.

Start by creating your minor mode as described in this answer.

Then you just need to define the hydra in the mode-map for that mode:

(defhydra python-indent (my-mode-map "C-c")
  "Adjust python indentation."
  (">" python-indent-shift-right "right")
  ("<" python-indent-shift-left "left"))

Once you've run all this code, you can turn on my-mode, and you should have access to your keybinding in all buffers.

5
  • Thanks. But how can I bind it into (defhydra python-indent (sh-mode-map "C-c") ? Function that is defined as defhydra
    – alper
    Aug 11 at 17:42
  • Sorry, didn't realize hydra was going to be a complication. I don't think use-package will work in this case. You'll need to create your own minor-mode I think. I'll try to update this when I have a few moments later on.
    – Tyler
    Aug 11 at 18:57
  • Thanks for considering to helping out
    – alper
    Aug 12 at 12:00
  • See emacs.stackexchange.com/a/358/262. After you define my-mode as described there, you can add your defhydra to my-mode-map.
    – Tyler
    Aug 12 at 14:19
  • I was not able to find any example related to how to add defhydra into my-mode-map :(
    – alper
    Aug 12 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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