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

NickD
  • 29,717
  • 3
  • 27
  • 44
GOPS
  • 11
  • 3
  • 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? – Fran Burstall Sep 28 '22 at 07:31

4 Answers4

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?

tinlyx
  • 1,342
  • 1
  • 13
  • 27
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.)

phils
  • 50,977
  • 3
  • 79
  • 122
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()

GOPS
  • 11
  • 3
  • 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 Sep 12 '22 at 16:23
  • yes, that was just a example, in my case it should be
    command2   
    command3  
    command4 ")```
    
    – GOPS Sep 12 '22 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 Sep 13 '22 at 01: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 Sep 13 '22 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.

tinlyx
  • 1,342
  • 1
  • 13
  • 27
  • 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 Sep 13 '22 at 22:55
  • @GOPS I think it's only a single character or number. – tinlyx Sep 13 '22 at 23:11