I have (global-set-key (kbd "RET") 'newline-and-indent) in my .emacs which works fine in all modes but scala-mode(the newest, revision 19295 from svn).
What do I need to change to get it working?
(add-hook 'scala-mode-hook
(lambda () (local-set-key (kbd "RET") 'reindent-then-newline-and-indent)))
The above somewhat fixes the problem. It now indents the line correctly after pressing Enter once, but still doesn't work if there is a blank line above the newline.
In scala-mode, "RET" is bound to scala-newline by default, and this overrides the global key binding set with global-set-key, hence the need for the hook specific to scala-mode. Consider using:
(add-hook 'scala-mode-hook
(lambda () (local-set-key (kbd "RET") 'newline-and-indent)))
instead of:
(add-hook 'scala-mode-hook
(lambda () (local-set-key (kbd "RET") 'reindent-then-newline-and-indent)))
If you don't want Emacs to change your indentation after leaving a line.
Related
I want to change the key binding M-x slime-fuzzy-complete-symbol to M-TAB in both slime-mode and slime-repl.
I looked up the common lisp manual, and tried the following sentences:
(eval-after-load 'slime
`(define-key slime-prefix-map (kbd "M-TAB") 'slime-fuzzy-complete-symbol))
(add-hook 'slime-load-hook
#'(lambda ()
(define-key slime-prefix-map (kbd "M-TAB") 'slime-fuzzy-complete-symbol)))
(define-key slime-repl-mode-map (kbd "M-TAB")
'slime-fuzzy-complete-symbol)
None of them is useful. The third sentence even have an error:
Symbol's value as variable is void: slime-repl-mode-map
For reference, the following the is my init.el relate to slime:
(let ((default-directory "/usr/local/share/emacs/site-lisp/"))
(normal-top-level-add-subdirs-to-load-path))
;; Setup load-path, autoloads and your lisp system
;; Not needed if you install SLIME via MELPA
(add-to-list 'load-path "~/default-directory/slime")
(require 'slime-autoloads)
(setq inferior-lisp-program "/usr/local/opt/clozure-cl/bin/ccl64")
;; Setup slime-repl
(setq slime-contribs '(slime-scratch slime-editing-commands))
;;(setq slime-contribs '(slime-repl)) ; repl only
(setq slime-contribs '(slime-fancy)) ; almost everything
;;Setup suto-complete
(add-to-list 'load-path "~/default-directory/auto-complete/")
(require 'auto-complete-config)
;;(add-to-list 'ac-dictionary-directories "~/default-directory/auto-complete/ac-dict")
(ac-config-default)
I know this is an old question, but a few weeks ago I was looking for an answer to a similar one and I hope additional info may be useful for someone...
Here's what I've found out:
using (eval-after-load 'slime) in your .emacs (dot-emacs) file is not a good idea until you have (autoload 'slime) in it - and this is OK - without that (eval-after-load "...") may wait till the end of time for loading slime
the previous answer is OK if global-key-binding is appropriate for you
if you require or prefere local binding you may define your own function and bind it locally, e.g.:
(defun my-slime-mode-keybindings ()
"For use in `slime-mode-hook' and 'slime-repl-mode-hook."
(local-set-key (kbd "<C-f1>") 'slime-describe-symbol)
(local-set-key (kbd "<M-f1>") 'slime-apropos-all)
(local-set-key (kbd "C-c C-p") nil) ;; when you want to remove a key/sequence
(local-set-key (kbd "C-<tab>") 'ace-window)
(local-set-key (kbd "M-<tab>") 'slime-fuzzy-complete-symbol) ;; your case :)
) ;; end of defun my-slime-mode-keybindings()
;; tell emacs to use your function only in required mode(s)
(add-hook 'slime-mode-hook #'my-slime-mode-keybindings)
(add-hook 'slime-repl-mode-hook #'my-slime-mode-keybindings)
best regards
It will be better to follow the emacs documentation for this case:
Key binding commands
In your case with a global key bindoing should work:
(global-set-key (kbd "M-TAB") 'slime-fuzzy-complete-symbol)
When using ergo emacs, for some reason M-l and M-j (forward-char and backward-char respectively) don't work properly in the minibuffer (with ido mode).
I've tried setting the ido-completion-map with the following:
(add-hook 'ido-setup-hook
(lambda ()
(define-key ido-completion-map (kbd "M-k") 'ido-next-match)
(define-key ido-completion-map (kbd "M-i") 'ido-prev-match)
(define-key ido-completion-map (kbd "M-l") 'ido-next-match)
(define-key ido-completion-map (kbd "M-j") 'ido-prev-match)))
but these don't seem to stick.
I seem to be having a similar problem to this person: ido-mode binding masked by global-set-key but none of the solutions seems to work for me
Any help would be very appreciated
Kind regards
Nimai
Although the instructions at the outset of ido.el suggest using:
;; Customization
;; -------------
;;
;; Customize the Ido group to change the Ido functionality.
;;
;; To modify the keybindings, use the ido-setup-hook. For example:
;;(add-hook 'ido-setup-hook 'ido-my-keys)
;;
;;(defun ido-my-keys ()
;; "Add my keybindings for ido."
;; (define-key ido-completion-map " " 'ido-next-match)
;; )
I recently found that using the ido-common-completion-map had better luck when using a frame-switch function -- the original poster can substitute his / her own preferred keyboard shortcuts instead of m-tab and/or m-S-tab:
(add-hook 'ido-setup-hook 'ido-my-keys)
(defun ido-my-keys ()
"Add my keybindings for ido."
(define-key ido-common-completion-map (kbd "<M-tab>") 'ido-next-match)
(define-key ido-common-completion-map (kbd "<M-S-tab>") 'ido-prev-match) )
I have met the save problem, i'm using Emacs 24.4 with ergoemacs-mode-5.14.7.3 (i don't use the latest version of ergoemacs because it has the speed issue. See: github issue). After a lot of searching, i finally find this github commit, and get it work by adding below code to my emacs init file:
after enable ergoemacs-mode:
(when ido-mode
(global-set-key [remap ido-magic-forward-char] 'ido-next-match)
(global-set-key [remap ido-magic-backward-char] 'ido-prev-match))
Hope it helps, thanks!
I am using thhs code in the .emacs file to permanently enable the hs-minor-mode and to change the shortcut:
(setq-default hs-minor-mode t)
(global-set-key (kbd "C-c C-h") (kbd "C-c # C-h")) ;;hiding block of code
(global-set-key (kbd "C-c C-r") (kbd "C-c # C-s")) ;;revealing block of code
But the mode is not activated automatically. what should i do?
You can turn on hs-minor-mode for a specific mode like C, C++ mode using c-mode-common-hook.
(add-hook 'c-mode-common-hook #'hs-minor-mode)
In Emacs 24 or later, you can turn it on in all programming modes using prog-mode-hook.
(add-hook 'prog-mode-hook #'hs-minor-mode)
If you want it to be truly global, this does the trick:
(define-globalized-minor-mode global-hs-minor-mode
hs-minor-mode hs-minor-mode)
(global-hs-minor-mode 1)
If you want to enable it everywhere, and start the buffer with the code folded by hs-hide-all, do
(defun my-hide-all()
(interactive)
(hs-minor-mode)
(hs-hide-all))
(add-hook 'prog-mode-hook 'my-hide-all)
I'm trying to redefine the "M-." in the ENSIME mode so that it runs auto-complete instead of ensime-edit-definition. Which is the default binding. I have the following code in the .emacs:
(defun my-scala-mode()
(ensime-mode)
(local-set-key [return] 'newline-and-indent)
(local-unset-key (kbd "M-."))
(local-set-key (kbd "M-.") 'auto-complete)
(global-unset-key (kbd "M-."))
(global-set-key (kbd "M-.") 'auto-complete)
;(scala-electric-mode)
(yas/minor-mode-on))
(add-hook 'scala-mode-hook 'my-scala-mode)
However, once ensime mode loads, and somehow redefines the keys back to the default. If I comment out "(ensime-mode)" then it maps correctly.
What should I do here? Is there another mode hook I'm missing? Or should the order be different?
Thank you
Apparently ensime-mode is a minor-mode, so its bindings take precedence over the major-mode's bindings. And local-set-key affects the major mode's bindings. You might want to do something like the following (guarantedd 100% untested) instead:
(require 'ensime)
(define-key ensime-mode-map (kbd "M-.") 'auto-complete)
or
(add-hook 'ensime-mode-hook (lambda () (define-key ensime-mode-map (kbd "M-.") nil)))
In particular, when I load dired-x, it sets M-o to toggle the omit minor mode. I use M-o for other-window, so I would like to change the key that dired-x binds to something else. I've attempted setting the key after the mode loads like this:
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)
(global-set-key (kbd "M-o") 'other-window)
))
but to no avail.
Slightly better than adding another copy of your custom global binding to the local mode map, would be removing the local binding so that it no longer shadows the global binding. You might also give that function a new key before you do this.
(eval-after-load "dired-x"
'(progn
;; Add an alternative local binding for the command
;; bound to M-o
(define-key dired-mode-map (kbd "C-c o")
(lookup-key dired-mode-map (kbd "M-o")))
;; Unbind M-o from the local keymap
(define-key dired-mode-map (kbd "M-o") nil)))
The dired-mode bindings "shadow" the global ones so your "global-set-key" isn't helping. What you want to do is override the dired-mode binding:
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)
(define-key dired-mode-map (kbd "M-o") 'other-window)
))