I don't know elisp, but I'm trying to do something like the following:
(add-hook
'scala-mode-hook
(lambda ()
(define-key scala-mode-map (kbd "RET") (lambda ()
(scala-newline)
(scala-indent-line)))))
Goal is to call the two functions each time I hit the ENTER key. How do I actually do this?
I do essentially this in so many modes that I've squashed them all together:
(mapcar (lambda (hooksym)
(add-hook hooksym
(lambda ()
(local-set-key (kbd "C-m") 'newline-and-indent)
)))
'(
clojure-mode-hook
emacs-lisp-mode-hook
erlang-mode-hook
java-mode-hook
js-mode-hook
lisp-interaction-mode-hook
lisp-mode-hook
makefile-mode-hook
nxml-mode-hook
python-mode-hook
ruby-mode-hook
scheme-mode-hook
sh-mode-hook
))
Just stick scala-mode-hook in there somewhere and it'll work for you too :)
You need an (interactive) form after the lambda in your define-key.
EDIT:
To be clear, the inner form should look like:
(lambda ()
(interactive)
(scala-newline)
(scala-indent-line))
In hook you can use local-set-key, for example
(add-hook 'scala-mode-hook
(lambda ()
(local-set-key [return]
(lambda ()
(scala-newline)
(scala-indent-line)))))
although, maybe it will be easier to use something like standard newline-and-indent?
(add-hook 'scala-mode-hook
(lambda ()
(local-set-key [return] 'newline-and-indent)))
Just type C-j it will call the newline-and-indent command and do exactly what you ask.
Related
I am trying to bind to M-b a command preceded by a negative argument. I post my code.
working
(global-unset-key (kbd "M-f"))
(global-set-key (kbd "M-f") 'forward-whitespace)
not working
(global-unset-key (kbd "M-b"))
(global-set-key (kbd "M-b") (lambda () (interactive) (negative-argument(forward-whitespace))))
How can I make it working?
The specific issue you have is solved easily:
(global-set-key (kbd "M-b") (lambda () (interactive) (forward-whitespace -1)))
You might wonder if you can write a macro that would call an arbitrary command interactively with negative argument.
It is not really hard:
(defmacro call-with-negative-argument (command)
`(lambda ()
(interactive)
(prefix-command-preserve-state)
(setq prefix-arg '-)
(universal-argument--mode)
(call-interactively ,command)))
(global-set-key (kbd "...") (call-with-negative-argument forward-whitespace))
but, really, any compliant function would also work fine like this:
(defmacro call-with-negative-argument (command)
`(lambda ()
(interactive)
(,command -1)))
I know this type of question have been asked by many people,
but I have read many similar posts and still have no idea
what to do. So here is the elisp code in .emacs:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(progn
(py-execute-line)
(evil-next-line)))
(add-hook 'python-mode-hook
(lambda () (define-key python-mode-map (kbd "C-c C-j") 'py-execute-line-down)))
I also tried to add (interactive) into the function, it didn't work.
Just to keep the record here, this seemed to do the trick, not sure if it's optimal though:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(interactive)
(py-execute-line)
(evil-next-line 1))
(defun kaiyin-pykeys ()
"python mode custome keys"
(local-set-key (kbd "C-c j") 'py-execute-line-down)
)
(add-hook 'python-mode-hook 'kaiyin-pykeys)
Taking Dan's advice, I changed the above into:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(interactive)
(py-execute-line)
(forward-line 1))
(define-key python-mode-map (kbd "C-c j") 'py-execute-line-down)
I have the following global keyboard shortcut in Emacs:
(global-set-key (kbd "C-<right>") 'forward-word)
For the org-mode I decided to redefine this shortcut. If the cursor stands on a link, then go to the link location. Otherwise - use forward-word function.
(defun is-link-p ()
(if (org-in-regexp org-bracket-link-regexp)
t))
(defun follow-link-or-next-word ()
(interactive)
(if (is-link-p)
(org-open-at-point)
(forward-word)))
(add-hook 'org-mode-hook (lambda ()
(define-key org-mode-map (kbd "C-<right>") 'follow-link-or-next-word)))
Is it possible to change org-mode shortcut in the following manner: instead of calling (forward-word), find what function is globally bound to "C-<right>" and call it instead.
Thus I won't need to change (forward-word) twice in case I decide to change the global shortcut.
I think you're looking for the function (lookup-key keymap key &optional accept-defaults)
This function returns the definition of key in keymap. All the other
functions described in this chapter that look up keys use lookup-key.
Here are examples:
(lookup-key (current-global-map) "\C-x\C-f")
⇒ find-file
(lookup-key (current-global-map) (kbd "C-x C-f"))
⇒ find-file
You could extend your functions:
(defun is-link-p ()
(if (org-in-regexp org-bracket-link-regexp)
t))
(defun follow-link-or-default-action()
(interactive)
(let ((global-default (lookup-key (current-global-map) (kbd "C-<right>"))))
(if (is-link-p)
(org-open-at-point)
(funcall global-default))))
(add-hook 'org-mode-hook (lambda ()
(define-key org-mode-map (kbd "C-<right>") 'follow-link-or-default-action)))
There is no need to use C-x C-s to save buffer in term mode. So I want to disable it. I tried to define a new binding in term mode to replace it, but failed.
(add-hook 'term-mode-hook
#'(lambda ()
(define-key term-mode-map "\C-x\C-s"
#'(lambda ()
(interactive)
(message "NO!")))))
Any help is appreciated. Thanks
Update: I use multi-term which based on term mode.
As wvxvw said, I tried this and it works. Add below to term-bind-key-alist. Thanks everyone.
("\C-x\C-s" . (lambda ()
(interactive)
(message "NO!")))
Take off the #' from your code. It should work. But you will still be able to save the buffer with "M-x save-buffer"
UPDATE:
This code is working for me
(add-hook 'term-mode-hook
(lambda ()
(define-key term-mode-map (kbd "C-x C-s")
(lambda ()
(interactive)
(message "hello world")))))
You will need to reload your term buffer after you setup the hook. It will not work for existing buffers
This binds an operation to Control-p
(global-set-key (kbd "C-p") (λ () (interactive) (previous-line 5)))
I would like to instead bind it to Meta-UpArrow.
Thanks!
(global-set-key (kbd "M-<up>") (λ () (interactive) (previous-line 5)))
I figured this out by typing "C-h k" and then pressing meta + uparrow.
If use global set key, that mapping will be shadowed by a mode-specific mapping. So, while your mapping works in markdown-mode where there is no mode specific mapping for "M-<up>", it won't work in org-mode where <M-up> maps to org-metaup.
So, if you really want to map "M-<up>" even if it may conflict with some modes that you use, you can do the following:
(global-set-key (kbd "M-<up>") (lambda () (interactive) (previous-line 5)))
(require 'org)
(define-key org-mode-map (kbd "M-<up>") (lambda () (interactive) (previous-line 5)))
However, you need to do this (define-key) for every mode that you use that already has a mapping for <M-up>.