2

I am trying to apply following solutions's key binding to overwrite all mode's key bindings 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 the 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 key binding 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). Commented Aug 13, 2021 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). Commented Aug 13, 2021 at 7:56
  • Hm what change should I make to work better?
    – alper
    Commented Aug 13, 2021 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. Commented Aug 13, 2021 at 10:06

1 Answer 1

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
    Commented Aug 11, 2021 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
    Commented Aug 11, 2021 at 18:57
  • Thanks for considering to helping out
    – alper
    Commented Aug 12, 2021 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
    Commented Aug 12, 2021 at 14:19
  • I was not able to find any example related to how to add defhydra into my-mode-map :(
    – alper
    Commented Aug 12, 2021 at 21:56

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.