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)))
Related
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.
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).
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)
I want to make one keystroke, say C-F12, to do delete-other-windows or winner-undo. I think it's easy if I already learning Emacs Lisp programming, and set a boolean flag. That is, if previously it run delete-other-window, now it'll run winner-undo.
How do you do that in Emacs Lisp?
Thanks
Try something like this
(setq c-f12-winner-undo t)
(define-key (current-global-map) [C-f12]
(lambda()
(interactive)
(if c-f12-winner-undo
(winner-undo)
(delete-other-windows))
(setq c-f12-winner-undo (not c-f12-winner-undo))))
(defun swdev-toggle-sole-window ()
(interactive)
(if (cdr (window-list))
(delete-other-windows)
(winner-undo)))
(global-set-key (kbd "<C-f12>") 'swdev-toggle-sole-window)
The first line starts the declaration of a function called swdev-toggle-sole-window, taking no argument.
This function is declared as interactive, i.e. it can be called with M-x or through a key binding.
If the window list contains more than one element, i.e. if there is more than one window, …
… then delete other windows …
… else undo the window deletion.
Bind the function to the key C-f12.
Here's a solution using the approach taken by Emacs' recenter-top-bottom function:
(defun delete-other-window-or-winner-undo ()
"call delete-other-window on first invocation and winner-undo on subsequent invocations"
(interactive)
(if (eq this-command last-command)
(winner-undo)
(delete-other-windows)))
(global-set-key (kbd "<C-f12>") 'delete-other-window-or-winner-undo)
I can move my pointer up and down one line with my arrow key just fine in Emacs, so I'd like to redefine C-n and C-p to move up and down 5 lines at a time.
I'm just beginning to learn how to use Emacs, and elisp is very alien to me. I tried using the GNU Emacs lisp reference, but I couldn't find how to bind a keystroke to multiple commands.
Here's what I have so far (concentrating on the moving up definition):
(global-set-key "\C-p" '(loop for i in '(1 2 3 4 5) do ('previous-line)))
But, this brings up an error message when I hit C-p, "Wrong type argument."
Any suggestions?
Thanks!
Those function (I believe next-line and previous-line) accept an optionnal argument with C-u, so i think that (next-line 5) would do what you want.
Edit: so I just tried and that would be
(global-set-key (kbd "C-n")
(lambda () (interactive) (next-line 5)))
And the same with C-p and previous-line.
(Fiew not simple to write code in a textarea with a phone keyboard ^^)
according the warning in my emacs config, and the comment from Peter Ajtai, I propose the version that uses forward-line from my init.el
(global-set-key (kbd "C-n")
(lambda () (interactive) (forward-line 5)))
(global-set-key (kbd "C-p")
(lambda () (interactive) (forward-line -5)))
Of course there is also forward-char, works the same, just in a different direction.
The only thing that's missing is a complex-forward that takes a complex number as an argument, and can be used to navigate in all 4 directions :P