I have a block of code like this:
ec2_shell_exec(tag: "ls /
ec2_shell_exec(tag: "sudo yum install git-core
ec2_shell_exec(tag: "pwd
Whats the easiest way to add a ") to this block of code?
Please note, emacs runs in terminal mode aka -nw mode.
Interactively
C-M-% $ RET ") RET
don't forget to press 4 keys at the same time: Ctrl for C, Alt for M, and Shift-5 for %
if the above still does not work (e.g., you are in a terminal with emacs -nw), you can do M-x query-replace-regexp RET $ RET ") RET
Programmatically
(while (not (eobp)) ; you have to edit the condition!
(goto-char (line-end-position))
(insert "\")")
(forward-line))
A keyboard macro should be easy to type on most terminals:
Move to your first line
Start recording a keyboard macro with C-x (
Go to end of line with C-e
Type ")
Move down with C-n
Stop recording macro with C-x )
Type C-x e to replay once
Type e for each successive time you need to repeat it
Summarizing the two solutions here (for folks running emacs in terminal):
M-x query-replace-regexp RET $ RET ") RET
And
first highlight ec2_shell_exec(tag. And call mc/mark-all-like-this which is from multiple-cursor. You will see all ec2_shell_exec(tag being hightlighted. Now call end-of-line to move every cursor to the end. Finally, you can insert anything you want. Press C-g to end operation
video demo: https://www.youtube.com/watch?v=jNa3axo40qM
Thanks adobe and tom!
Related
I have a problem for execute a personal macro in another session in Emacs. I succeeded to create macro and execute then but, after I want to save it for execute them in another time.
For this I write this code in ~/.emacs
(fset 'psTest
(lambda (&optional arg) "Keyboard macro."
(interactive "p")
(kmacro-exec-ring-item (quote ("^X2^X2^X2^X2" 0 "%d")) arg)))
but when I call my macro in another file [ M- x psTest ], Emacs doesn't execute my macro but writes key in my file
^X2^X2^X2^X2
all my commands:
In terminal:
user#PC $ emacs ~/.emacs
In emacs:
C-x (
C-x 2
C-x )
C-x C-k n psTest
M-x insert-kbd-macro [ENTER] psTest [ENTER]
C-x C-c
In terminal:
user#PC $ cat ~/.emacs :
(fset 'psTest
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("^X2" 0 "%d")) arg)))
user#PC $ emacs ~/test
In emacs:
M- psTest
Now my macro [M- psTest] write ^X2 in my file instead of execute [^X2] which split the screen.
Where is my error?
Thanks
The problem lies in the sequence "^X2" in your macro definition. It contains two characters ^ and X rather than the single character 0x18 in the charset ascii (ASCII (ISO646 IRV)) which is used by emacs to refer to C-x but is displayed the same, though probably in a different color. If you replace the former two-letter-sequence with the latter character and evaluate the definition again, it should work.
You can insert the character with
C-x8RET #x18 RET.
PS: To display information about a specific character at point you can use
M-x desribe-char or what-cursor-position, which is bound to C-x = by default.
I agree with Simon Fromme.
To insert the C-x character, you may omit the #x prefix from his answer and type:
C-x8RET18RET
But you may also simply type C-qC-x in case you don't know the hexadecimal value of the ascii code of this or any other character!
Nevertheless, in your case, I would rather search for the function associated to the C-x 2 sequence. You'll easily find it is split-window-below using either:
C-h k C-x 2 RET
or M-x edit-last-kbd-macro RET
Then you can write some code easier to copy/paste/save like:
(fset 'psTest #'split-window-below)
or
(defun psTest ()
(interactive)
(split-window-below))
This might be a good way to start learning emacs-lisp!
Assume I have a text file with content
1
123
12
12345
If I want to add an 'a' in the beginning of each line I can simply use string-rectangle (C-x r t), but what if I want to append an 'a' to the end of each line, after which the file should become
1a
123a
12a
12345a
Thanks.
You could use replace-regexp for this purpose, with the $ regexp metacharacter that matches end-of-line. Go to the start of the buffer, and then do M-x replace-regexp, and answer $ and (your text) to the two prompts.
Or, in emacs-speak, for your specific example of adding a:
M-< M-x replace-regexp RET $ RET a RET
Emacs keyboard macros are your friend.
C-x ( C-e a C-n C-x )
Which just sets up the keyboard macro by: starting the keyboard macro (C-x (), go to the end of the line (C-e), insert an a, go to the next line (C-n), and then end the macro recording (C-x )).
Now you can either execute it (C-x e), and keep pressing e for each line you want to have it run on, or you can run it on a region with C-x C-k r.
If you do this a lot, you can save the macro, or you can write a function. This would be one such function:
(defun add-string-to-end-of-lines-in-region (str b e)
"prompt for string, add it to end of lines in the region"
(interactive "sWhat shall we append? \nr")
(goto-char e)
(forward-line -1)
(while (> (point) b)
(end-of-line)
(insert str)
(forward-line -1)))
In ESS when I am evaluating chunks of code in a .R file using C-c C-j or C-c C-r (to send the line or region to a running R process), how can I get the R buffer to scroll down automatically, such that after evaluating a region the cursor is at the bottom, at the prompt?
Thanks.
Probably a bunch of ways to do it. In my .emacs.d I have
(setq comint-prompt-read-only t)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
You might also be interested in this code, originally from Felipe Csaszar, which lets you do what you ask and a few other nice things besides.
Have a look at auto-scrolling in emacs doc. For my part:
customize-variable RET scroll-down-aggressively RET
scroll-down-aggressively set to 1 did the job.
DJJ's solution works perfectly for ESS and polymode in Spacemacs.
First, put (scroll-down-aggressively 1) within the defun dotspacemacs/user-config () section of the configuration file, e.g. ~/.spacemacs.
Then, evaluate the line using SPC , e r or restart configuration SPC f e d.
Emacs 27 complained about the above line because "Symbol's function definition is void" so I had to change it as follow to achieve the same result:
(setq scroll-down-aggressively 0.01)
Is there a way to convert an emacs macro into elisp, not like what M-x insert-kbd-macro does, the actual activity becoming elisp statements.
Thanks for your help.
Nope, sorry. There is no trivial way to convert an emacs macro into elisp.
Update: There's been some work on Emacs to start down this path. See this thread as a starting point. It's still not possible (June 2010), but there's activity.
The first reason I can think of is dealing with interactive commands and translating keystrokes into proper arguments for functions.
Think of the following sequence:
C-x b .em TAB RET
This begins the command to switch to a buffer, types three characters, uses TAB completion to complete it and RET to accept. The equivalent lisp for the end result (in an emacs session where the TAB completion is unique) is:
(switch-to-buffer ".emacs")
Thinking of completion, there are also interactions with expansion of all types (dabbrev, hippie-expand, etc.).
A starting point can be M-x edit-last-kbd-macro which (in my case) shows this:
;; Keyboard Macro Editor. Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-x b .em <tab> RET
Command: last-kbd-macro
Key: none
Macro:
C-x b ;; switch-to-buffer
.em ;; self-insert-command * 3
<tab> ;; pabbrev-expand-maybe
RET ;; newline-and-indent
Which at least gives you some of the function names. But you'll see that RET is labeled as 'newline-and-indent which is incorrect because at the time of the macro execution, the minibuffer is active and the binding is in fact 'minibuffer-complete-and-exit. Similarly, the proper binding for TAB is 'minibuffer-complete.
I made a package that allows pretty much exactly this at https://github.com/Silex/elmacro
It has some quirks but it works pretty well... for example, the following macro:
F3 C-e M-b M-u C-a C-n F4
Generates the following elisp:
(defun upcase-last-word ()
"Change me!"
(interactive)
(move-end-of-line 1)
(backward-word 1)
(upcase-word 1)
(move-beginning-of-line 1)
(next-line 1 1))
I run the following command in Emacs unsuccessfully
C-u
How can you clear the beginning of a line in Emacs?
You can also accomplish this by using a prefix arg for kill-ine (usually C-k). From the Emacs help (C-h k C-k):
With zero argument, kills the text before point on the current line.
So, you can do
C-u 0 C-k
or even better
C-0 C-k
You can set the mark, go to the beginning, kill till mark:
C-Spc C-a C-w
Try this:
M-0 C-k
Delete from beginning of line to point
C-u works in Bash "Emacs Mode", but not actually in Emacs. Here's what I usually do:
C-a C-k
But this is really only good if you want to kill the whole line. Svante's advice will clear from the beginning of the line to where your cursor was, as you asked for.
I don't know, where you read about C-u, but it is bound to the universal argument in Emacs.
If you want to kill the whole line, call kill-whole-line which is bound C-S-backspace. No matter at what column is the cursor, it will kill the whole line from beginning to end.
I have a small function bound to a key-chord:
(defun kill-start-of-line ()
"kill from point to start of line"
(interactive)
(kill-line 0))
(define-key global-map "\M-#" 'kill-start-of-line)
M-# is usually Alt-Shift-3, not a new DotNet language
I'm sure I saw this somewhere else, but didn't save the original reference.