How to make $ as an alias for a specific keyboard macro in emacs - emacs

Suppose that I have defined a macro in emacs (24.2), say xyz.
I would like to associate the shortcut $ to this macro, i.e. to run the macro xyz whenever I type $.
How I can do it?
I tried all of the following without success:
(global-set-key [$] 'xyz)
(global-set-key ["$"] 'xyz)
(global-set-key [?$] 'xyz)
(global-set-key [s-4] 'xyz)
(global-set-key "$" 'xyz)
(global-set-key (kbd "$") 'xyz)
(The last three ones were suggested by bleeding-fingers, abo-abo and Chris).

From your comments, it is now clear that you've defined a macro that includes using the key $. If you do this, you can't then bind the macro to the $, because that makes it recursive - every time you get to the $ in your macro, you are in effect calling the macro again.
You could, however, define the actions you want performed as an elisp function, which you could then bind to $. If we knew what you were actually doing with your macro we might be able to show you how.
EDIT: how about this:
(global-set-key (kbd "$") #'(lambda () (interactive) (insert " $")))
That should work, but lambdas can be a bit confusing. A little clearer for elisp beginners might be:
(defun my-dollars ()
"Insert a dollar sign with a space in front."
(interactive)
(insert " $"))
(global-set-key (kbd "$") 'my-dollars)

Related

emacs how to bind "LEFT-POINTING DOUBLE ANGLE QUOTATION MARK" to a keystroke

I want to type a single keystroke to insert unicode brackets #xab and #xbb.
This works (for a different unicode bracket):
(global-set-key (kbd "C-c [") "⟨")
But this doesn't work:
(global-set-key (kbd "C-c [") "«")
How do I coax emacs into inserting "«" with a single keystroke?
(I happen to be on MacOS, but intend to use ubuntu in Parallels)
I`m not sure what is expected, double ⟨ or one «, but hope this can help
check function and loop
(defun my-insert ()
"test something"
(interactive)
(dotimes (i 2)
(insert "⟨"))
)
(defun my-insert2 ()
"test something"
(interactive)
(insert "«")
)
(global-set-key (kbd "C-c [") 'my-insert)
(global-set-key (kbd "C-c ]") 'my-insert2)
There is a more general solution which works everywhere, making it more convenient (as you may not want to open an emacs instance each time you need a particular character), which is the compose key. Compose key is specific to Linux, but there is a way to install an equivalent solution on MacOS, and you can even convert existing Compose Key bindings, which is useful because there are a lot of predefined sequences out there in the wild, for instance the default ones.
E.g. on Ubuntu you probably just need to enable it in the settings, and then with the default configuration typing Compose < < will produce «.

Emacs: custom function and keybinding for commenting out line

I have been trying out stuff and looking at other answers for several hours now and I cannot figure out how to make custom functions and keybindings work... and it is absolutely infuriating.
For the purposes of testing I wrote this function
(defun my/cmmt ()
""
(interactive)
(move-beginning-of-line 1)
(comment-region 1))
(global-set-key (kbd "\C-o")
'my/cmmt)
There are 2 issues, I want it bind this to C-m but then I get an error:
symbol's value as variable is void: C-m
What does that mean?
And also, all it ever does is move the cursor to the beginning of the line, but not comment it out. Why?
Edit
(defun my/cmmt ()
""
(interactive)
(comment-region
(line-beginning-position)
(line-end-position)
)
)
(global-set-key (kbd "C-o")
'my/cmmt)
Now the error is:
symbol's function definition is void: \,
(kbd "C-o") not (kbd "\C-o")
You're confusing two methods of specifying keys -- (kbd "C-o") and "\C-o" are equivalent.
I recommend using kbd, and simply typing C-hk keys to learn what to pass to kbd to specify the key sequence keys. e.g.: when you type C-hkC-o Emacs tells you that C-o is the representation of that key sequence, so "C-o" is what you must pass to kbd.
The reason the commenting doesn't work is because (comment-region 1) isn't valid. You should be seeing an error. It takes two required arguments. See C-hf comment-region for details.

emacs: assign single key-binding to multiple commands, using the universal argument

Here is my attempt:
(global-set-key [M-left] (key-binding (kbd "C-u C-#")))
After I evaluate the above expression, invoking alt + left gives me the message <M-left> is undefined. The following, however, works:
(global-set-key [M-left] (key-binding (kbd "C-u")))
But this is only the universal argument part of my command. How do I combine these two commands into one Emacs key-binding?
There are two ways to do this: define a Keyboard Macro interactively or write a function:
(define-key global-map [M-left]
(lambda ()
(interactive)
(set-mark-command t)))
sds has provided the solutions, but for clarification, if you evaluate (key-binding (kbd "C-u C-#")) you'll see that it returns nil -- because that is not a bound key sequence.
In fact C-u runs the command universal-argument, which takes care of reading a subsequent key sequence from the user (C-# in your case).

emacs map several keystrokes and command to one key

I'm trying to map c-u m-x indent-pp-sexp to a single key, like F5, so that working with Emacs doesnt erode my fingerprints.
I use (global-set-key (kbd "C-u M-x indent-pp-sexp") "<f5>") but i'm getting the following error:
global-set-key: Key sequence C-u M-x i n d e n t - p p - s e x p starts with non-prefix key C-u
EDIT
With this lambda function (global-set-key (kbd "<f5>") (lambda (interactive) (universal-argument) (indent-pp-sexp t)))
Getting error:
recursive-edit: Wrong type argument: commandp, (lambda (interactive) (universal-argument) (indent-pp-sexp t))
Weird, because univeral-argument takes no parameters, and indent-pp-sexp takes boolean
You have the arguments the wrong way around, and you bind keys to functions, not to other key sequences. Perhaps you are really looking for a named macro; or you can write some actual Lisp and bind that to F5:
(global-set-key (kbd "<f5>")
(function (lambda () (interactive) (indent-pp-sexp t) )) )
The presence of an argument in the call form appears to be sufficient to select the prefix argument functionality.
You're missing the argument list to the lambda. Additionally I think passing t to indent-pp-sexp negates the need to call universal-argument.
(global-set-key (kbd "<f5>") #'(lambda ()
(interactive)
(indent-pp-sexp t)))
I'm a noob like you, but I already happened to figure basic things like making macros. I don't really know what's wrong with your code, but here's walkthrough of how I do things at home. What you need to do first, is press F3. Then type your keystrokes, and when finished, press F4. Congratulations, you have defined an anonymous macro. You can replay it as many times you wish by pressing F4 again. When you have played enough, enter M-x name-last-keybord-macro, and name it eg. foobar. Go to your ~/.emacs.d/macros/ directory (make it if you don't have one) and visit a file that you will name foobar.el. In its buffer, M-x insert-kbd-macro. When asked about name, say foobar. You will see that emacs has entered the contents of your just recorded macro into the file. Save it. Open your .emacs file, and add lines:
(load (expand-file-name "~/.emacs.d/macros/foobar.el"))
(global-set-key (kbd "M-<f5>") 'foobar)
And things start working for me after restart, with M-F5 as the binding for foobar.el macro.

having two functions executed with one keyboard combo

I'm trying to have C-<return> map to move-end-of-line then newline-and-indent
In my .emacs I have been playing around with the following:
(global-set-key (kbd "C-<return>") '(progn (move-end-of-line) (newline-and-indent)))
And
(defun c-ret()
'(move-end-of-line newline-and-indent))
(global-set-key (kbd "C-<return>") 'c-ret)
but neither work.
Pointers?
You are quoting the commands.
That implies that they won't be executed. You also need the (interactive) to signify to emacs that it's callable from the keyboard.
Then, you need to have your parameters to your functions correct.
Further, I think your nomenclature for return is wrong.
Your basic misunderstanding here is knowing how eLisp works. That's okay, it's an arcane programming language.
' a.k.a QUOTE is pretty much a Lisp-specific special instruction. It says, "Don't evaluate what comes after me", and returns the unevaluated parameter.
So '(foo bar) is desugared into (QUOTE (FOO BAR)), which returns (FOO BAR).
Try this:
(defun c-ret()
(interactive)
(move-end-of-line nil)
(newline-and-indent))
(global-set-key (kbd "C-RET") 'c-ret)
You can do this without writing any code yourself. See http://www.emacswiki.org/emacs/KeyboardMacrosTricks for instructions on capturing a sequence of commands as a keyboard macro, naming it, and saving it in your .emacs. You can then give the new command a key binding of your choice with e.g. (global-set-key (kbd "C-c i") 'new-command-name).
If you want a one-line solution, this will work too:
(global-set-key (kbd "C-<return>") (lambda () (interactive) (move-end-of-line nil) (newline-and-indent)))