Defining step size of Emacs Macro Counter - emacs

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).

Related

In evil-mode how to just press J to move the cursor down 3 lines

When in evil-mode, I can make the cursor go down 3 lines by command 'C-u 3 M-x evil-next-line' , but how can I make the cursor go down 3 lines by just pressing J?
1. Short answer
Add this to your .emacs file:
(evil-define-motion evil-next-line-3 (count)
"Move the cursor 3 * COUNT lines down."
:type line
(let (line-move-visual)
(evil-line-move (* 3 (or count 1)))))
Note that J is currently bound to evil-join.
If you are ok to rebind it, just add:
(define-key evil-normal-state-map "J" 'evil-next-line-3)
Now, to join lines, you may either type M-x evil-join RET or bind something else to evil-join (see below).
Note that, if you type 2J, you'll move twice 3 lines down, which is more or less 6 lines down... and C-u J will move 4*3 = 12 lines down.
2. Not so short answer (what I actually did)
I never used evil-mode... so I tried to add this package and enabled the evil-mode (which is ok for me since I know the main vi commands and I know how to disable it with C-- M-x evil-mode RET).
When evil-mode is enabled and I type C-h k J, the Help buffer says:
J runs the command evil-join (found in evil-normal-state-map),
which is an interactive compiled Lisp function in ‘evil-commands.el’.
It is bound to J.
(evil-join BEG END)
Join the selected lines.
Then I typed C-h k j, clicked the evil-commands.el link from the Help buffer and copied and adapted the definition of the evil-next-line function to the evil-next-line-3 function and bound it to J.
In case you want to go back to the previous binding of J (or want to bind it to something else), you can evaluate something like:
(define-key evil-normal-state-map "J" 'evil-join)

How do I bind a key to "the function represented by the following key sequence"?

I'm just starting to learn emacs (woohoo!) and I've been mucking around in my .emacs quite happily. Unfortunately, I don't know Lisp yet, so I'm having issues with the basics.
I've already remapped a few keys until I fix my muscle memory:
(global-set-key (kbd "<f9>") 'recompile)
That's fine. But how can I tell a key to 'simulate pressing several keys'? For instance, I don't know, make <f1> do the same as C-u 2 C-x } (widen buffer by two chars).
One way is to look up that C-x } calls shrink-window-horizontally, and do some sort of lambda thing. This is of course the neat and elegant way (how do you do this?). But surely there's a way to define <f1> to send the keystrokes C-u 2 C-x }?
Sure there is, and it's the obvious way:
(global-set-key (kbd "<f1>") (kbd "C-u 2 C-x }"))
For anything long-term, I would recommend the approach shown by seh, as that will naturally be more robust in most situations. It requires a little more work and know-how, of course, but it's all worthwhile :)
angus' approach is like a cut-down version of the keyboard macros feature that gives Emacs its name (and slightly simpler to use than macros for the example in question). You should definitely be aware of macros, however -- they can be exceedingly useful, and for anything more complicated it quickly becomes far easier to record one dynamically than to write out all the individual keys manually.
Here's the summary I wrote myself of the most important bits:
;;;; * Keyboard macros
;; C-x ( or F3 Begin recording.
;; F3 Insert counter (if recording has already commenced).
;; C-u <n> C-x ( or F3 Begin recording with an initial counter value <n>.
;; C-x ) or F4 End recording.
;; C-u <n> C-x ) or F4 End recording, then execute the macro <n>-1 times.
;; C-x e or F4 Execute the last recorded keyboard macro.
;; e or F4 Additional e or F4 presses repeat the macro.
;; C-u <n> C-x e or F4 Execute the last recorded keyboard macro <n> times.
;; C-x C-k r Apply the last macro to each line of the region.
;; C-x C-k e Edit a keyboard macro (RET for most recent).
;; C-x C-k b Set a key-binding.
;;
;; If you find yourself using lots of macros, you can even name them
;; for later use, and save them to your init file.
;; M-x name-last-kbd-macro RET (name) RET
;; M-x insert-kbd-macro RET (name) RET
;;
;; For more documentation:
;; C-h k C-x (
;; M-: (info "(emacs) Keyboard Macros") RET
If we play with the example from the question, you'll see how some of these things tie together...
To begin with, you can define the macro with F3C-u2C-x}F4
You could then bind it temporarily to F1 with C-xC-kbF1 (actually that's not true if F1 is currently a prefix key for an existing keymap, as typing it interactively will simply prompt for the remainder. You can circumvent this in code with (global-set-key (kbd "<f1>") ...), but I would suggest sticking to the reserved bindings).
If you then use describe-key (C-hk) to examine what is bound to that key, Emacs will show you a (lambda) expression which you could copy to your init file if you so wished.
Alternatively, you could name the macro and ask Emacs to insert the code into the current buffer:
M-x name-last-kbd-macro RET (name) RET
M-x insert-kbd-macro RETRET
This code will look different to the lambda expression shown by describe-key, but if you evaluate the inserted macro, you'll see the equivalence. You can likewise show that the (kbd "...") expression also evaluates to the same value, and therefore these are all just alternative ways of doing the same thing.
(You can use the *scratch* buffer to evaluate the code by moving point after the end of the expression, and either typing C-xC-e to show the value in the minibuffer, or C-j to insert the value into the buffer).
Note that the 'inserted' code uses fset to assign the macro to a symbol. You could bind the macro to a key either by executing the (fset) and then assigning that symbol to a key with (global-set-key), or you could ignore the (fset) and simply assign the macro value directly. This, of course, is directly equivalent to angus' answer.
Edit: I've just noticed that there's a kmacro-name-last-macro function bound to C-xC-kn which is nearly identical in form to name-last-kbd-macro, but which generates the lambda expression form seen when using kmacro-bind-to-key (C-xC-kb) and describe-key.
I'll use shrink-window-horizontally as the example function, but you can generalize the idea to any bindings you'd like to define.
If you want to use two as the default amount to shrink the window, rather than one, try the following:
(global-set-key [f9]
(lambda (&optional n)
(interactive "P")
(shrink-window-horizontally (or n 2))))
That binds the F9 key to an interactive function accepting a prefix argument. If you just press F9, you'll pass no argument, which summons the default value of 2, as the parameter n will receive nil as an argument. However, if you press, say, C-u 10 F9, you'll pass ten as the argument for n. This allows you to use your binding more flexibly.
general-simulate-key from general.el works better (in my case a sequence with popups and changing keymaps that I couldn't get to work with macros): https://github.com/noctuid/general.el#simulating-keypresses

How to invoke the buffer list in Emacs

I usually type M-x buffer-menu to switch buffers in Emacs. How can I do this with a shorter command? Its quite a long string to type.
Thanks!
You can use C-x b to change buffers. You have to enter the first few letters of the buffer name, and of course you can use completion. If you press TAB (the most useful key in Emacs), a list of (matching) buffers appears. You can click in this list to switch to a buffer.
You can bind buffer-menu to a key. Pick a key that's not used for another command — let's say f12 — and add the following line to the file ~/.emacs:
(global-set-key (kbd "<f12>") 'buffer-menu)
There are many other interfaces to changing buffers in Emacs, and they can be significantly more efficient than C-x b and C-x C-b. Since this tends to be a very personal choice, I recommend you experiment with a few and keep the one(s) you feel most comfortable with.
C-x C-b
As stated here
I'd highly recommend switching to a mode designed for efficient buffer switching.
If your version of Emacs is recent enough (22+):
M-x ido-mode
and then:
C-x b
to switch buffers, with incremental substring matching, C-s and C-r rotate forward and backwards through the matches.
If you have an older version of Emacs, it should have:
M-x iswitchb-mode
and then, as with ido-mode:
C-x b
opens up the minibuffer to let you choose the buffer to switch to.
Bind C-x C-b to buffer-menu. There is no sense leaving it bound to list-buffers. list-buffers is just a eunuch version of buffer-menu. ;-)
And you might want to try this: http://www.emacswiki.org/emacs/BufferMenuPlus
Try bs-show (in my opinion a way better than C-x C-b). You can bind it to F9 by adding this to .emacs:
(global-set-key (kbd "<f9>") 'bs-show)

Convert Emacs macro into Elisp

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))

In Emacs, what is the opposite function of other-window (C-x o)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Emacs, switch to previous window
other-window advances me to the next window in the current frame, but I also want a way to move back to the previous window.
Emacs has next-buffer and previous-buffer, but no analogous interactive functions for window navigation. Just other-window.
Provide a negative argument with C-u - ("Control+U" then "minus"), or even more simply C-- ("Control minus").
Move to previous window: C-- C-x o
Move to previous frame: C-- C-x 5 o
From code, (other-window -1) or (other-frame -1) will do the same thing.
Check out the help for the key you want to reverse (e.g. C-h k C-x o to show help for C-x o) and if it says "A negative argument..." you know you can use C--.
This is an old post, but I just wondered the same. It seems there now is a function for this in Emacs: previous-multiframe-window.
I have it bound to C-x O, as in uppercase letter o. Now I just throw in shift when I want to go backwards.
(global-set-key (kbd "C-x O") 'previous-multiframe-window)
Put this in your .emacs, and bind it to whatever key you like
(defun back-window ()
(interactive)
(other-window -1))
A slightly less annoying shortcut available by default isC-- C-x o. That way you don't have to switch between Meta and Control while typing the prefixes.
Different from what you asked for, but the windmove package lets you move between windows according to their relative screen locations, which can be much easier than repeatedly doing C-x o.
Instead of C-u -, you can also give a negative prefix argument with just M-- (Meta-Minus) , i.e. switch to previous window with M-- C-x o.