1

I am using shell inside Emacs. Often I need to execute a series of shell commands around ~40 chars, as of now I copy all the commands together and paste it from documentation. Is there any way to save all these commands in Emacs and execute in few key strokes.

One option is, define a keyboard macro and save it. But how can a define a keyboard macro from a text. I don't want to define a keyboard macro by typing each command letter by letter.

Any ideas?

I am using Emacs 27.2 with spacemacs

1
  • It is not really clear what you are asking for here. Are you asking for commands that insert text into a buffer (look into registers)? Do you need to save these definitions for use in another session? Commented Sep 28, 2022 at 7:31

4 Answers 4

1

If I understand you correctly, you can put the commands you want to execute in a shell buffer (e.g. associated with a .sh file), then you can use the Shell-script mode to execute a selected region of commands, with sh-execute-region.

C-M-x runs the command sh-execute-region. Pass ... region to a subshell for noninteractive execution. The working directory is that of the buffer ...

The output is shown at the bottom of the Emacs window.

Related:

differences of shell-mode vs. shell-script-mode?

1

Keyboard macros can simply be text by default. You only need to ensure you're using the right text.

If you want to be very clear you can break it down using the key sequence syntax:

(global-set-key (kbd "C-c x") (kbd "T h i s SPC i s SPC m y SPC t e x t RET"))

But both of the following are identical to that:

(global-set-key (kbd "C-c x") (concat "This is my text" (kbd "RET")))
(global-set-key (kbd "C-c x") "This is my text\r")

(As you can tell by evaluating the kbd expressions.)

1
  • Yes, this is what I was looking for. Thank you
    – GOPS
    Commented Sep 13, 2022 at 23:16
0

I got it. Just need to define a function to print the long text and and then create a KBD macro to call that function.

    (defun printhello ()
      "Insert hello"
      (interactive)
      (insert "hello World")
      (newline)
      )

M-x eval-region or restart emacs and M-x printHello If using spacemacs, add it under user-config()

4
  • 2
    This just inserts the string "hello World" into whatever buffer is current when you evaluate the function. What does it have to do with your question?
    – NickD
    Commented Sep 12, 2022 at 16:23
  • yes, that was just a example, in my case it should be (insert "command1 command2 command3 command4 ")
    – GOPS
    Commented Sep 12, 2022 at 21:30
  • So you just want the text of the commands in your buffer? Same as if you paste them in from the documentation? BTW, what's wrong with pasting from documentation? Do the commands ever change? What happens if they do? Are you trying to execute these commands? How does inserting them in a buffer help you execute them?
    – NickD
    Commented Sep 13, 2022 at 1:09
  • Yes that correct, I will be calling it from a shell buffer, so it will dump all the commands in to the shell I just need to press enter to execute all in one shoot. I can paste from documentation, but calling an emacs function is much faster and easy.
    – GOPS
    Commented Sep 13, 2022 at 19:30
0

If you just want to paste all the commands into the shell buffer as said in comments, you can also store them into a register in Emacs config file, and then "paste" it whenever you need.

First, you can add below to your init.el, which stores the commands into a register (here s):

(set-register ?s "hello world")

Then, when using it, you can paste the content of register (s here) with C-x r i s. You can use a different register than s or multiple registers to store the set(s) of commands you like.

2
  • That also works. Thank you for this tip. Is it possible to name a register instead of a single char? otherwise its bit difficult to remember which register hold what.
    – GOPS
    Commented Sep 13, 2022 at 22:55
  • @GOPS I think it's only a single character or number.
    – tinlyx
    Commented Sep 13, 2022 at 23:11

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.