I have a text file with some sample content as shown here:
Sno = 1p
Sno = 2p
Sno = 3p
What i want is to remove the p from each of the columns.
With this intention i write a macro:
M-x //go to buffer
C-x (//start the macro
C-s = // search for equalto sign
RET C-f C-f // reach to te alphabet 'p'
DEL // Delete
C-n C-x )//go to new line and Close the macro definition
C-x e
Pressing e twice will remove p, but in case i want to do the same stuff till the end of file, how can i do it i can't keep pressing e if i have 20000 such lines. What should be done??
Please donot suggest regex, as this is a sample example, not the actual case.
Please donot suggest any elisp, i am comfortable with remembering shortcutf for emacs.
M-0 C-x e to repeat last macro until an error happens (after the final line, either C-s = or C-n will be an error).
You may hear an annoying beep.
You can use the "apply-macro-to-region-lines" function to apply the last defined keyboard macro to all lines in a region. So, the steps you follow are:
Define your keyboard macro (which you had already done but I've added another C-f to the 3rd line):
C-x (
C-s =
RET C-f C-f C-f
DEL
C-n C-x )
Select the whole buffer with the "mark-whole-buffer" command (by default, it's bound to C-x h). Alternatively, if you just want to change a certain number of lines, just select the lines you want changed.
Run the "apply-macro-to-region-lines" function (by default, it's bound to C-x C-k r).
All the p's will be removed.
I usually give a name to the macro after defining it with M-x name-last-kbd-macro, so I can call it with M-x conveniently. And then I call it with a numeric prefix. E.g.
M-1000 M-x macroname
The files I work on usually don't have 1000 places where the macro can act, so 1000 is large enough and it will stop automatically at the end of the file. You can use, say, 1000000 as a prefix if you have larger files, and if it's still not enough then you can call it again with 1000000 as prefix.
you may try: M-20000 C-x e so as to do the stuff for 20000 times, after you define the macro.
Related
I love the Emacs keyboard-macro functionality and I am using it a lot.
Sometimes, I don't want to just statically enter certain keyboard macros, but there should be a value there that will get changed in between. There is the feature of Emacs Macro counters (Macro counters in Emacs Manual).
The problem is that this counter always just counts up by one. Is there a way to specify the stepping size (i.e. move forward by 4 in each step)?
Thanks in advance for your help!
You can use kmacro-add-counter, bound to C-x C-k C-a.
For example to add 3 to the counter, use M-3 C-x C-k C-a.
Small full example: <f3> <f3> RET M-3 C-x C-k C-a <f4> <f4> <f4> <f4> will produce:
0
4
8
12
Alternative to kmacro
Sometimes, you can use tiny to do
what kmacro does in fewer keystrokes and with better undo context.
The above example can generated by entering:
m\n3*x4
and pressing the shortcut for tiny-expand. I bind it like this:
(global-set-key (kbd "C-;") 'tiny-expand)
Here, m\n3 basically means 4 repetitions (index starts from 0) joined by the newline character (\n). And *x4 is a shorthand for Elisp (* x 4).
I have a list of lines like this:
a+
b+
c+
d+
e+
f+
... you get the idea...
I want to end up with a+b+c+d+e etc
I was trying with emacs but couldn't work out how to do such a thing. anyone any ideas?
One thing that does work is
c-m-% [paste in selected after + on one line to beginning of next row] [nothing]
There must be something to insert for carriage return?
How about simply replacing EOLs by nothing?
M-%C-q C-jRETRET
Explanation:
M-% : query-replace
C-q : quote the following character
C-j : end-of-line character
first RET : validate the search string
second RET : validate the (empty) replacement string
Do you have a buffer with those lines in it? In that case, you could create a simple macro:
F3 ;; record macro
C-e ;; end of line
C-d ;; delete newline
F4 ;; save macro
Then either press F4 repeatedly until you're done, or do C-0 F4 to do it all in one swoop.
Have you tried just `M-q' ? The spacing is different, and it will use several lines if you have many of those thingies, but otherwise, it seems like a funny alternative.
M-x
replace-regexp
RET
C-q C-j
RET
RET
You can assume that I'm in repl using the slime mode.
How can I make a function key (for example, f4), to do this:
kill the last history item (the ones that you get with C-up or C-down);
move to the upper buffer;
yank, Save buffer to file;
move back to the repl.
Please, make it a step by step guide, because I'm a complete beginner to Emacs and Lisp.
The easiest way to make what you ask would be using emacs macros.
Why?
Because you have just said exactly what you want to do.
And macros save the sequence of keys you typed.
You can do it in emacs for one time, and save the sequence of pressed keys.
So, start recording a macro (when you are in the repl buffer) using F3 or C-x (, then make something like M-p C-a C-k C-u - C-x o C-y C-x o(i just translated your request to key sequence), then type F4 or C-x ). To execute macro, press F4 again, or C-x e.
You can interrupt recording a macro if you made a mess with C-g. The reverse is applied, if you made a mess and error message is send, your macro recording(sometimes frustrating) or evaluating(and this is feature, since you can make macro that will work good by just holding F4) would be interrupted.
If you want to use this macro later, you can name it with M-x name-last-kbd-macro. This will allow you to use as a command, typing M-x <your macro name> (<your macro name> - name of your macro). This will not save it for future sessions, just name it.
To save your named macro, use M-x insert-kbd-macro when you are in your .emacs file. This will insert elisp code at current point, executing which you will get your macro binded to your command name(and it will be executed every time you start emacs).
To bind it to some key, rather start it every time from M-x, insert this in your .emacs file: (global-set-key [f12] '<your-macro-name>). You can read more about setting comands to keys there and there.
The bad thing about macro is that you will undo every step, not the whole macro in one time(but someone may bring solution here, if he have one). If you want to make something more serious, using conditions or iterations, you have to forward your path to elisp. Just C-h k everything around. Help keys like C-h f, C-h a, C-h b will also come in use.
I'm looking for key binding / function which delete from current cursor position to first non blank character
example:
function f() {|
test();
| - cursor position
I want delete everything to "t" letter
M-z t t (substitute t as needed :).
Lots of good options have been presented. In Emacs 24 (currently in pretest), you can specify M-- to just-one-space or M-SPC to do exactly what you ask.
In 'c-mode' there is 'M-x c-hungry-delete-forward', which is also bound to C-c C-d. So, you can create that binding in whatever programming mode you're using.
(define-key <whatever>-mode-map (kbd "C-c C-d" 'c-hungry-delete-forward)
Alternatively, you can grab the package 'hungry-delete' and use that to override the deletion commands to delete all the whitespace (as opposed to a single space).
To do this, I use a custom macro :
(fset 'jline
[?\C- ?\C-\M-n ?\C-a ?\C-w return])
(just put this small macro in your .emacs config)
Of course, you can rename the macro like you want.
So, the following state :
function f() {|
test();
}
Becomes :
function f() {
|test();
}
Use M-x delete-blank-lines (which is also bound to C-x C-o).
From the help-docs (C-h k C-x C-o):
On blank line, delete all surrounding blank lines, leaving just one.
On isolated blank line, delete that one.
On nonblank line, delete any immediately following blank lines.
I need to repeatedly copy text from a fixed size rectangular region and I'd like to be able to save the shape of that rectangular region in a register so I don't have to keep re-creating the same size.
cua-set-rectangle-mark (<C-return>)
Move point to create a region 8x16 (note: this is the step I want to remove)
piture-clear-rectangle (C-c C-k)
Move point to new location.
picture-yank-rectangle (C-c C-y)
I'd like to replace steps 1 and 2 with a single 'paste rectangular region from register' command. Is this possible?
Wouldn't it be easier to simply use a keyboard macro for this?
E.g.:-
C-x C-( [start recording kbd macro]
steps 1-2
C-x C-) [end recording kbd macro]
Then
C-x e [execute kbd macro]
You probably want to use copy-rectangle-to-register and insert-register:
C-x r r runs the command copy-rectangle-to-register, which is an
interactive compiled Lisp function in `register.el'.
It is bound to C-x r r.
(copy-rectangle-to-register REGISTER START END &optional DELETE-FLAG)
Copy rectangular region into register REGISTER.
With prefix arg, delete as well.
To insert this register in the buffer, use C-x r g.
Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
START and END are buffer positions giving two corners of rectangle.
insert-register:
C-x r g runs the command insert-register, which is an interactive
compiled Lisp function in `register.el'.
It is bound to C-x r g, C-x r i.
(insert-register REGISTER &optional ARG)
Insert contents of register REGISTER. (REGISTER is a character.)
Normally puts point before and mark after the inserted text.
If optional second arg is non-nil, puts mark before and point after.
Interactively, second arg is non-nil if prefix arg is supplied.
See also:
C-xrc: clear-rectangle
C-xrd: delete-rectangle
C-xrk: kill-rectangle
C-xro: open-rectangle
C-xrr: copy-rectangle-to-register
C-xrt: string-rectangle
C-xry: yank-rectangle
EDIT:
Right, I completely misunderstood the question.
If you still particularly want to use registers, you could save and re-execute point movements with something like this:
(set-register ?a [right right right down down])
(command-execute (get-register ?a))