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"))
Drew
  • 75,699
  • 9
  • 109
  • 225
alper
  • 1,238
  • 11
  • 30
  • 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). – dalanicolai Aug 13 '21 at 07: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). – dalanicolai Aug 13 '21 at 07:56
  • Hm what change should I make to work better? – alper Aug 13 '21 at 08: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. – dalanicolai Aug 13 '21 at 10:06

1 Answers1

1

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.

Tyler
  • 21,719
  • 1
  • 52
  • 92
  • 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 '21 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 '21 at 18:57
  • Thanks for considering to helping out – alper Aug 12 '21 at 12:00
  • See https://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 '21 at 14:19
  • I was not able to find any example related to how to add `defhydra ` into `my-mode-map` :( – alper Aug 12 '21 at 21:56