How do I configure emacs to bind something to meta-up? - emacs

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

Related

bind key to negative-argument + command in emacs

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

wrong type argument commandp with a custom function in emacs

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)

Emacs: how to get the global shortcut value

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

emacs: toggle binding of number row to <shift>-ed equivalent

In emacs, I would like to rebind the top row of my keyboard [1...0] so that hitting an unmodified key results in its shifted equivalent. That is, typing 1234567890 would result in !"£$%^&*() being inserted in the buffer.
I am using emacs 24.1.1 in Windows Vista, with viper-mode enabled. I am doing some Common Lisp programming using slime. I use viper so that I can avoid using Ctrl and Shift too often as I can get a bit of emacs pinkie (RSI). Having started programming in lisp, I have found that hitting S-9 and S-0 to open and close parentheses is starting to take its toll.
By including the following in my start-up file, I can bind 9 to ( and vice-versa.
(defvar my-keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "9") '(lambda () (interactive) (insert "(")))
(define-key map (kbd "(") '(lambda () (interactive) (insert "9")))
map))
(viper-modify-major-mode
'lisp-mode
'insert-state
my-key-map)
This works well enough and is easily extended to the rest of the row, except that I would like to be able to toggle between the two modes without having to hold down shift (say, by toggling Caps Lock).
Is there any way to do this, or am I approaching it all wrong?
Here's an example I quickly hacked together, tested it in Emacs24 on Linux:
(setq viper-mode-key-mapping "custom")
(defvar custom-viper-keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "9") '(lambda () (interactive) (insert "(")))
(define-key map (kbd "(") '(lambda () (interactive) (insert "9")))
map))
(defvar default-viper-keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "9") '(lambda () (interactive) (insert "9")))
(define-key map (kbd "(") '(lambda () (interactive) (insert "(")))
map))
(defun switch-viper-mode-custom-keymap ()
(interactive)
(if (string= viper-mode-key-mapping "default")
(progn (setq viper-mode-key-mapping "custom")
(viper-modify-major-mode 'lisp-mode 'insert-state custom-viper-keymap))
(progn (setq viper-mode-key-mapping "default")
(viper-modify-major-mode 'lisp-mode 'insert-state default-viper-keymap))))
(global-set-key [(control f1)] 'switch-viper-mode-custom-keymap)
When I have viper-mode activated, pressing CTRL-F1 switches the keyboard mapping from custom to normal.

Emacs: scroll buffer not point

Is it possible to scroll the entire visible portion of the buffer in Emacs, but leave point where it is. Example: point is towards the bottom of the window and I want to see some text which has scrolled off the top of the window without moving point.
Edit: I suppose C-l C-l sort of does what I wanted.
try these. Change M-n and M-p key bindings according to your taste
;;; scrollers
(global-set-key "\M-n" "\C-u1\C-v")
(global-set-key "\M-p" "\C-u1\M-v")
;;;_*======================================================================
;;;_* define a function to scroll with the cursor in place, moving the
;;;_* page instead
;; Navigation Functions
(defun scroll-down-in-place (n)
(interactive "p")
(previous-line n)
(unless (eq (window-start) (point-min))
(scroll-down n)))
(defun scroll-up-in-place (n)
(interactive "p")
(next-line n)
(unless (eq (window-end) (point-max))
(scroll-up n)))
(global-set-key "\M-n" 'scroll-up-in-place)
(global-set-key "\M-p" 'scroll-down-in-place)
This might be of use. According to the EmacsWiki page on Scrolling;
The variable scroll-preserve-screen-position may be useful to some.
When you scroll down, and up again, point should end up at the same
position you started out with. The value can be toggled by the built
in mode M-x scroll-lock-mode.
I think this is better:
(defun gcm-scroll-down ()
(interactive)
(scroll-up 1))
(defun gcm-scroll-up ()
(interactive)
(scroll-down 1))
(global-set-key [(control down)] 'gcm-scroll-down)
(global-set-key [(control up)] 'gcm-scroll-up)
reference : emacs wiki
;; Preserve the cursor position relative to the screen when scrolling
(setq scroll-preserve-screen-position 'always)
;; Scroll buffer under the point
;; 'scroll-preserve-screen-position' must be set to a non-nil, non-t value for
;; these to work as intended.
(global-set-key (kbd "M-p") #'scroll-down-line)
(global-set-key (kbd "M-n") #'scroll-up-line)
Based on Bilal's answer:
(global-set-key [(meta down)] (lambda () (interactive) (scroll-down 1)))
(global-set-key [(meta up)] (lambda () (interactive) (scroll-up 1)))