The key bindings I've defined in my .emacs file aren't working. Here's the file:
;init modes
(menu-bar-mode 0)
(tool-bar-mode 0)
(cua-mode)
(column-number-mode)
(fset 'perl-mode 'cperl-mode)
(cperl-set-style PerlStyle)
;keymappings
(global-set-key [f12] 'save-buffer)
(global-set-key [S-f12] 'write-file)
(global-set-key [f7] 'ispell)
(global-set-key [up] 'scroll-one-line-up)
(global-set-key [down] 'scroll-one-line-down)
;functions
(defun scroll-one-line-up (&optional arg)
(interactive "p")
(scroll-up (or arg 1)))
(defun scroll-one-line-down (&optional arg)
(interactive "p")
(scroll-down (or arg 1)))
I know Emacs parses the file since everything else seems to work. It's just that the keys are not being bound.
How can I make it work?
You have an error in your .emacs at line:
(cperl-set-style PerlStyle)
It should be written as:
(cperl-set-style 'PerlStyle)
Since it raises an error that stops parsing .emacs at that point, your key bindings won't be evaluated.
To follow up to my previous answer, you would have to change the binding in the local keymap using a hook variable. Here's an example that I use with java-mode:
(defun java-setup ()
(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92)
indent-tabs-mode nil
tab-width 4
fill-column 96
c-comment-start-regexp "\\(#\\|/\\(/\\|[*][*]?\\)\\)"))
(add-hook 'java-mode-hook 'java-setup)
In your case you would use something like:
(defun mysetup ()
(define-key local-map [f12] 'func))
(add-hook 'your-mode-hook 'mysetup)
Also, fwiw, I do the following to define my global keys:
(defun function-key-help ()
(interactive)
(switch-to-buffer "*Help*")
(erase-buffer)
(insert-file (expand-file-name "~/lib/fkeys.help"))
(message "Type C-x b <nl> to remove help window."))
(define-key global-map [f12] 'function-key-help)
And it works perfectly in my Emacs 23 setup.
It is hard to say what your problem might be without more information, like is it all your keybindings or just one or two that do not work. I will hazard a guess that it is the last two ([up] and [down]). In those cases the on-line documentation below seems to indicate that you might be shadowing the global definitions with local ones defined by the mode.
global-set-key is an interactive
compiled Lisp function in `subr.el'.
(global-set-key key command)
Give key a global binding as command.
command is the command definition to
use; usually it is a symbol naming an
interactively-callable function. key
is a key sequence; noninteractively,
it is a string or vector of characters
or event types, and non-ASCII
characters with codes above 127 (such
as ISO Latin-1) can be included if you
use a vector.
Note that if key has a local binding
in the current buffer, that local
binding will continue to shadow any
global binding that you make with this
function.
Related
This works in emacs 25:
(setq custom-keymap (copy-keymap global-map))
(defun custom-def (keys func &optional file &optional global-p)
(define-key custom-keymap keys func)
(if global-p (global-set-key keys func))
(if file (autoload func file "[custom autoload]" t)))
(custom-def [delete] 'delete-char)
But when I call custom-def in emacs 26 I get an invalid-function error. I isolated it to the &optional parameters. I removed those two args and custom-def works.
So what changed between 25 and 26? What am I missing here? I want the flexible ARGLIST that works in emacs 25.
Use optional just once in the parameter list- the following parameters will all be optional [other keywords can follow as well - see info(elisp) Functions].
(defun custom-def (keys func &optional file global-p)
;; ...
)
[I'm not sure what the change was - perhaps duplicated parameter checking was added?]
I am using the following.
(global-set-key [f9] 'helm-do-grep-1)
But when I press f9, It complains wrong type argument. I just want it behavior like "C-u C-c h g" to grep recursively. But type so many keys is boring.
update:
I need to grep recursively. helm-do-grep run in non-recursive mode.
You can use
(global-set-key [f9]
(lambda ()
(interactive)
(let ((current-prefix-arg 't))
(call-interactively 'helm-do-grep))))
Upd. If you're interested: the version with kbd sequence
(global-set-key [f9]
(lambda ()
(interactive)
(let ((minibuffer-message-timeout 0))
(execute-kbd-macro (read-kbd-macro "C-u C-c h g C-x Q"))))
See the definition of C-x Q here https://stackoverflow.com/a/28435402/1937596
As the error message already points out, the function helm-do-grep-1 has one argument: https://github.com/emacs-helm/helm/blob/master/helm-grep.el#L810
Probably what you wanted is binding f9 to helm-do-grep which calls helm-do-grep-1 in return with the correct parameters (
https://github.com/emacs-helm/helm/blob/master/helm-grep.el#L1129)
(global-set-key [f9] 'helm-do-grep)
Update:
You can find several solutions to your question here: http://www.reddit.com/r/emacs/comments/2dxj69/how_do_make_helmdogrep_to_do_recursive_always/
To show another possibility you could also do the following:
(global-set-key [f5]
(lambda ()
(interactive)
(call-interactively (key-binding (kbd "C-c h g")))))
In that case, you call helm-do-grep using <f5> and the recursive approach with C-u <f5>. However, this approach will depend on your key bindings.
when i type C-u F9 I want anything.el to pop up a buffer of choices as if I had typed
M-x anything -shell* manually.
In other words, I often invoke anything and look for all my shell buffers and so I would like to simplify this process.
1. You can define a simple function calling anything with a pre-filled -shell* input, and bind it to a key (for example F9):
(defun my/anything-shell ()
(interactive)
(anything :input "-shell*"))
(global-set-key (kbd "<f9>") 'my/anything-shell)
2. If your F9 key is already bound to usual-f9-command and you want anything-shell to be called only when you specify a prefix argument (with C-u F9), then your key binding must be a bit more complex:
(global-set-key (kbd "<f9>") (lambda (&optional arg)
(interactive "P")
(if arg
(my/anything-shell)
(usual-f9-command))))
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
In my .emacs i have the following function that transposes a line
(defun move-line (n)
"Move the current line up or down by N lines."
(interactive "p")
(let ((col (current-column))
start
end)
(beginning-of-line)
(setq start (point))
(end-of-line)
(forward-char)
(setq end (point))
(let ((line-text (delete-and-extract-region start end)))
(forward-line n)
(insert line-text)
;; restore point to original column in moved line
(forward-line -1)
(forward-char col))))
And I bind a key to it like this
(global-set-key (kbd "M-<down>") 'move-line)
;; this is the same as M-x global-set-key <return>
However, I want to bind M-up to move-line (-1) But I cant seem to be able to do it correctly:
;; M-- M-1 M-x global-set-key <return>
How do I define the above using global-set-key to call move-line -1?
Not minutes after asking the question I figured it out by copy+pasting code. However I have no clue how it works.
(global-set-key (kbd "M-<up>") (lambda () (interactive) (move-line -1)))
global-set-key only takes 2 arguments: the key sequence and the command you want to bind to it. So
(global-set-key (kbd "M-<down>") 'move-line)
works fine. But if you want to use move-line with an argument you need to wrap it in an anonymous (aka lamba) function so that it presents itself to global-set-key as one value.
You can simply ask for the number of lines you want and convert the input string into an integer:
(global-set-key (kbd "M-<up>")
(lambda ()
(interactive)
(move-line (string-to-int (read-string "Lines: ")))))
I found this when I had the same problem, but I ended up solving it in another way.
(global-set-key (kbd "M-<down>") 'move-line)
(global-set-key (kbd "M-<up>") (kbd "C-u -1 M-<down>"))
Definitely not a perfect solution, since M-<down> could be reassigned and C-u -1 might not make sense on it, but since it's just my local init file, it should be no problem.
Also this obvious only works well for keyboard commands that you want to have reversed.
You might want to check out the "transpose-lines" built-in function.