I switch windows with M-left and M-right. Also Tab, S-Tab and C-Tab are hardwired into my spine. Since I use markdown-mode my workspeed has halved.
How do I disable that markdown-mode re-assigns those keys on loading. The keys I describe are carefully handcrafted shortcuts from my .emacs file, set via global-set-key.
(global-set-key [S-iso-lefttab] 'dabbrev-expand)
(global-set-key [C-tab] 'ispell-word)
(global-set-key [M-up] 'windmove-up)
(global-set-key [M-down] 'windmove-down)
(global-set-key [M-left] 'windmove-left)
(global-set-key [M-right] 'windmove-right)
Set those keys also in markdown-mode, in its keymap (probably markdown-mode-map). For example:
(define-key markdown-mode-map [C-tab] 'ispell-word)
The problem you saw comes from the fact that a local binding overrides a global one. See the Elisp manual, node Active Keymaps.
I have an emacs shortcut set up like so:
;; Adding the key mappings to minor mode.
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
;; Cancel with one press of escape instead of three.
(define-key my-keys-minor-mode-map (kbd "<escape>") 'keyboard-quit)
(my-keys-minor-mode 1)
(defconst my-minor-mode-alist (list (cons 'my-keys-minor-mode
my-keys-minor-mode-map)))
(setf emulation-mode-map-alists '(my-minor-mode-alist))
This works great to override a shortcut in a truly global way, superseding all other minor modes. However I need a way to have a shortcut behave differently in different minor modes. For example I would like the escape key to run helm-keyboard-quit when helm is running bu run keyboard-quit otherwise. Doing so like:
(eval-after-load "helm"
'(progn
(define-key helm-map (kbd "<escape>") 'helm-keyboard-quit)))
won't work because the global escape shortcut will override it. Is there a way to do this?
Why not use a simple lambda. Something like this (untested):
(global-set-key (kbd "<escape>")
(lambda()
(interactive)
(if helm-mode (helm-keyboard-quit)
(keyboard-quit))))
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)
In Emacs, C-x o takes me to the next window.
What keyboard macro takes me to the previous window in Emacs?
You might also want to try using windmove which lets you navigate to the window of your choice based on geometry. I have the following in my .emacs file to change windows using C-x arrow-key.
(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <right>") 'windmove-right)
(global-set-key (kbd "C-x <left>") 'windmove-left)
That'd be C-- C-x o
In other words, C-x o with an argument of -1. You can specify how many windows to move by inserting a numeric argument between C-u and the command, as in C-u 2 C-x o. (C-- is a shortcut for C-u - 1)
Personally I prefer to use window-number.el
To select a different window, use Ctrl-x, Ctrl-j n
Where n is the number of the window, the modeline of each window shows it's number, as shown in the screenshot.
Just download window-number.el, place it in your emacs load-path and use the following in your .emacs
(autoload 'window-number-mode "window-number"
"A global minor mode that enables selection of windows according to
numbers with the C-x C-j prefix. Another mode,
`window-number-meta-mode' enables the use of the M- prefix."
t)
There's another similar mode called switch-window.el which gives you big numbers in the windows... (pressing the number switches the window and reverts the display.)
(source: tapoueh.org)
If you work with multiple emacs windows (>3) a lot and you will want to save some keystrokes add this to your init file and you'll be better off:
(defun frame-bck()
(interactive)
(other-window-or-frame -1)
)
(define-key (current-global-map) (kbd "M-o") 'other-window-or-frame)
(define-key (current-global-map) (kbd "M-O") 'frame-bck)
Now just cycle quickly thru the windows with M-o
There are some very good and complete answers here, but to answer the question in a minimalist fashion:
(defun prev-window ()
(interactive)
(other-window -1))
(define-key global-map (kbd "C-x p") 'prev-window)
Base on idea from #Nate but slightly modified to support backwards cycling between windows
;; Windows Cycling
(defun windmove-up-cycle()
(interactive)
(condition-case nil (windmove-up)
(error (condition-case nil (windmove-down)
(error (condition-case nil (windmove-right) (error (condition-case nil (windmove-left) (error (windmove-up))))))))))
(defun windmove-down-cycle()
(interactive)
(condition-case nil (windmove-down)
(error (condition-case nil (windmove-up)
(error (condition-case nil (windmove-left) (error (condition-case nil (windmove-right) (error (windmove-down))))))))))
(defun windmove-right-cycle()
(interactive)
(condition-case nil (windmove-right)
(error (condition-case nil (windmove-left)
(error (condition-case nil (windmove-up) (error (condition-case nil (windmove-down) (error (windmove-right))))))))))
(defun windmove-left-cycle()
(interactive)
(condition-case nil (windmove-left)
(error (condition-case nil (windmove-right)
(error (condition-case nil (windmove-down) (error (condition-case nil (windmove-up) (error (windmove-left))))))))))
(global-set-key (kbd "C-x <up>") 'windmove-up-cycle)
(global-set-key (kbd "C-x <down>") 'windmove-down-cycle)
(global-set-key (kbd "C-x <right>") 'windmove-right-cycle)
(global-set-key (kbd "C-x <left>") 'windmove-left-cycle)
Just to add to #Nate, #aspirin and #Troydm's answer I find this to be a very helpful addition if you decide to bind the windmove commands to whatever key combination you choose:
(setq windmove-wrap-around t)
With the default configuration you will get an error when you get to attempt to move to a window that doesn't exist which becomes kind of annoying after a while. However when windmove-wrap-around is set then attempting to move off the bottom of the frame for example will instead select the topmost window in the frame. This may be a more intuitive behaviour for you.
M-n and M-p makes the most sense to me, since they are analogous to C-n (next-line) and C-p (previous-line):
(define-key global-map (kbd "M-p") 'previous-multiframe-window)
(define-key global-map (kbd "M-n") 'other-window)
(inspired by to this and that)
In reference to Nate's answer, I replaced the arrow keys to use the traditional p for going up, n for going down, f for going right and b for going left. I also replaced the Ctrl with Super key as C-p, C-n, C-f and C-b are the default movement keys. This combination with M lets you jump characters and lines instead of going through just one by one after each keystroke. Thus Super key felt the best choice to keep it an easy key binding. Also, now you don't have to take your hand off the home row any more!
(global-set-key (kbd "s-p") `windmove-up)
(global-set-key (kbd "s-n") `windmove-down)
(global-set-key (kbd "s-f") `windmove-right)
(global-set-key (kbd "s-b") `windmove-left)
Hope it helps!
(global-unset-key (kbd "M-j"))
(global-unset-key (kbd "M-k"))
(global-set-key (kbd "M-j") (lambda () (interactive) (other-window 1)))
(global-set-key (kbd "M-k") (lambda () (interactive) (other-window -1)))
altj and altk will cycle through your visibles buffers. Forwards and backwards, to be exact.
There is already a package that lets you switch windows by using M-. check this website. Add this to your init file:
(require 'windmove)
(windmove-default-keybindings 'meta) ;; or use 'super to use windows key instead alt
(global-set-key (kbd "C-x a") 'ace-swap-window)
(global-set-key (kbd "C-x q") 'ace-select-window)
download ace-window from the melpa repo if you don't know how to do that
put this in your .emacs file if you don't have one create it
(package-initialize)
(require 'package)
(add-to-list 'package-archives '("melpa" , "http://melpa.org/packages/"))
(package-initialize)
then "m-x list-packages"
The fastest method I have found for switching to the previous window is to mash a couple keys together as a "key-chord". The following lets you use your left pinky+ring fingers together to go to previous window:
(key-chord-define-global "qw" 'prev-window)
(key-chord-define-global "'y" 'other-window) ; bonus for my colemak, adjust otherwise
(key-chord-define-global ";'" 'other-window) ; probably normal
(This is possible because Emacs key chords are order independent, meaning that qw is the same as wq.)