How to bind 'search' and 'search-repeat' to C-f in Emacs? - emacs

How can I to remap incremental search (C-s) to C-f in Emacs?
I try to do (global-set-key (kbd "C-f") 'isearch-forward) but the second C-f does not repeat the search and I need to use C-s.
I then tried (global-set-key (kbd "C-f") 'isearch-repeat-forward) but the first C-f didn't start the search.
And I even tried (global-set-key (kbd "C-f C-f") 'isearch-repeat-forward), but this causes an error.
I want to use C-f for search and search-repeat commands, how can I do this?
Thanks.

(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)

isearch-repeat-forward is defined in the isearch-mode-map
To resolve your problem do the following :
(global-set-key (kbd "C-f") 'isearch-forward)
(add-hook 'isearch-mode-hook
(lambda ()
(define-key isearch-mode-map (kbd "C-f") 'isearch-repeat-forward)
)
)
EDIT: actually, you don't need to add a hook. The accepted answer by Ross Patterson is correct.

Related

How to customize Emacs keyboard shortcut for deleting current line

I want the current line to be deleted when Alt, and the letter d is pressed twice.
How can I achieve this inside my Emacs configuration file?
Currently all I have is this in my .emacs:
(global-set-key (kbd "M-9") 'prev-window)
(global-set-key (kbd "M-0") 'other-window)
You can use your own keymap:
(defvar somename-map (make-sparse-keymap) "Keymap for M-d")
(define-key somename-map (kbd "M-d") 'kill-line)
(define-key somename-map (kbd "M-w") 'kill-word)
(global-set-key (kbd "M-d") somename-map)

How to keep traditional binding on M-x in emacs evil mode

I am trying to bind execute-extended-command to M-x in evil normal mode.
I currently have
;; evil mode
(require 'evil)
(evil-mode 1)
(define-key evil-normal-state-map "M-x" 'execute-extended-command)
in my .emacs file but the keybinding doesn't work. I tried replacing
"M-x"
with
"\M-x"
and
(kbd "M-x")
but neither works. I also tried adding it to evil.el and evil-maps.el.
I don't know what's wrong with your binding. You could use Emacs' own global-set-key for global stuff and if you plan something special for say, insert mode, you could override that later on, like this:
;; this works, just tested. My evil is 1.0-dev from github.
(global-set-key (kbd "M-x") 'smex)
(define-key evil-insert-state-map (kbd "M-x") 'execute-extended-command)
Use (kdb "") macro when you have modifier keys in your binding. But you can use the macro always, regardless of the content. These are for example usage. When in doubt, wrap the key in (kdb ).
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
;;(global-set-key (kbd "M-x") 'execute-extended-command)
(define-key evil-normal-state-map ",d" 'volatile-kill-buffer)
(define-key evil-normal-state-map ",b" 'ido-switch-buffer)
(define-key evil-normal-state-map ",s" 'ispell-word)
(define-key evil-normal-state-map (kbd "C-x g") 'magit-status)
(define-key evil-insert-state-map (kbd "C-f") 'my-expand-file-name-at-point)
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
(define-key minibuffer-local-map (kbd "C-w") 'backward-kill-word)
(define-key evil-normal-state-map (kbd ",ff") 'ido-find-file)
After long research and with help from #emacs and #evil-mode channels on irc, it turned out that my emacs was broken. It was a snapshot from http://emacs.naquadah.org/ I tried all this on another emacs version (from debian jessies repos) and it worked ok.

Disable Mail in emacs 23

I am wondering if its possible to disable mail in emacs 23. Basically, I occasionally press C-xm and it annoys me that it will create a Mail folder in my home directory. (I also want to remap this to 'execute-extended command).
I have tried
(global-unset-key "\C-x m")
(global-set-key (kbd "C-c m") 'execute-extended-command)
but it doesn't seem to affect anything.
This will set the binding of C-xm to the execute-extended-command function:
(global-set-key "\C-xm" 'execute-extended-command)
or
(global-set-key [?\C-x ?m] 'execute-extended-command)
or
(global-set-key (kbd "C-x m") 'execute-extended-command)

What are your favorite global key bindings in emacs ? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Mine are:
(global-set-key [f6] 'compile-buffer)
(global-set-key [f7] 'kmacro-start-macro-or-insert-counter)
(global-set-key [f8] 'kmacro-end-and-call-macro)
(global-set-key [f9] 'call-last-kbd-macro)
(global-set-key [f10] 'name-and-insert-last-kbd-macro)
(global-set-key [f12] 'menu-bar-open) ; originally bound to F10
(global-set-key "\C-cR" 'rename-current-file-or-buffer)
(global-set-key "\C-cD" 'Delete-current-file-or-buffer)
The name-and-insert-last-keyboard-macro is from another Stack Overflow question.
I happen to have quite a lot of these:
;; You know, like Readline.
(global-set-key (kbd "C-M-h") 'backward-kill-word)
;; Align your code in a pretty way.
(global-set-key (kbd "C-x \\") 'align-regexp)
;; Perform general cleanup.
(global-set-key (kbd "C-c n") 'cleanup-buffer)
;; Font size
(define-key global-map (kbd "C-+") 'text-scale-increase)
(define-key global-map (kbd "C--") 'text-scale-decrease)
;; Use regex searches by default.
(global-set-key (kbd "C-s") 'isearch-forward-regexp)
(global-set-key (kbd "\C-r") 'isearch-backward-regexp)
(global-set-key (kbd "C-M-s") 'isearch-forward)
(global-set-key (kbd "C-M-r") 'isearch-backward)
;; Jump to a definition in the current file. (This is awesome.)
(global-set-key (kbd "C-x C-i") 'ido-imenu)
;; File finding
(global-set-key (kbd "C-x M-f") 'ido-find-file-other-window)
(global-set-key (kbd "C-x C-M-f") 'find-file-in-project)
(global-set-key (kbd "C-x f") 'recentf-ido-find-file)
(global-set-key (kbd "C-c r") 'bury-buffer)
(global-set-key (kbd "M-`") 'file-cache-minibuffer-complete)
;; Window switching. (C-x o goes to the next window)
(global-set-key (kbd "C-x O") (lambda ()
(interactive)
(other-window -1))) ;; back one
(global-set-key (kbd "C-x C-o") (lambda ()
(interactive)
(other-window 2))) ;; forward two
;; Indentation help
(global-set-key (kbd "C-x ^") 'join-line)
(global-set-key (kbd "C-M-\\") 'indent-region-or-buffer)
;; Start proced in a similar manner to dired
(global-set-key (kbd "C-x p") 'proced)
;; Start eshell or switch to it if it's active.
(global-set-key (kbd "C-x m") 'eshell)
;; Start a new eshell even if one is active.
(global-set-key (kbd "C-x M") (lambda () (interactive) (eshell t)))
;; Start a regular shell if you prefer that.
(global-set-key (kbd "C-x M-m") 'shell)
;; If you want to be able to M-x without meta
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
;; Fetch the contents at a URL, display it raw.
(global-set-key (kbd "C-x C-h") 'view-url)
;; Help should search more than just commands
(global-set-key (kbd "C-h a") 'apropos)
;; Should be able to eval-and-replace anywhere.
(global-set-key (kbd "C-c e") 'eval-and-replace)
;; Magit rules!
(global-set-key (kbd "C-x g") 'magit-status)
;; This is a little hacky since VC doesn't support git add internally
(eval-after-load 'vc
(define-key vc-prefix-map "i" '(lambda () (interactive)
(if (not (eq 'Git (vc-backend buffer-file-name)))
(vc-register)
(shell-command (format "git add %s" buffer-file-name))
(message "Staged changes.")))))
;; Activate occur easily inside isearch
(define-key isearch-mode-map (kbd "C-o")
(lambda () (interactive)
(let ((case-fold-search isearch-case-fold-search))
(occur (if isearch-regexp isearch-string (regexp-quote isearch-string))))))
;; Org
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
;; program shortcuts - s stands for windows key(super)
(global-set-key (kbd "s-b") 'browse-url) ;; Browse (W3M)
(global-set-key (kbd "s-f") 'browse-url-firefox) ;; Firefox...
(global-set-key (kbd "s-l") 'linum-mode) ;; show line numbers in buffer
(global-set-key (kbd "s-r") 're-builder) ;; build regular expressions
;; Super + uppercase letter signifies a buffer/file
(global-set-key (kbd "s-S") ;; scratch
(lambda()(interactive)(switch-to-buffer "*scratch*")))
(global-set-key (kbd "s-E") ;; .emacs
(lambda()(interactive)(find-file "~/emacs/dot-emacs.el")))
;; cycle through buffers
(global-set-key (kbd "<C-tab>") 'bury-buffer)
;; use hippie-expand instead of dabbrev
(global-set-key (kbd "M-/") 'hippie-expand)
;; spell check Bulgarian text
(global-set-key (kbd "C-c B")
(lambda()(interactive)
(ispell-change-dictionary "bulgarian")
(flyspell-buffer)))
;; replace buffer-menu with ibuffer
(global-set-key (kbd "C-x C-b") 'ibuffer)
;; interactive text replacement
(global-set-key (kbd "C-c C-r") 'iedit-mode)
;; swap windows
(global-set-key (kbd "C-c s") 'swap-windows)
;; duplicate the current line or region
(global-set-key (kbd "C-c d") 'duplicate-current-line-or-region)
;; rename buffer & visited file
(global-set-key (kbd "C-c r") 'rename-file-and-buffer)
;; open an ansi-term buffer
(global-set-key (kbd "C-x t") 'visit-term-buffer)
;; macros
(global-set-key [f10] 'start-kbd-macro)
(global-set-key [f11] 'end-kbd-macro)
(global-set-key [f12] 'call-last-kbd-macro)
(provide 'bindings-config)
I actually have an entire Emacs Lisp file dedicated to global keybindings :-)
Some of my more unusual bindings:
(global-set-key [pause] 'emms-pause)
First good use for the pause key in a very long time!
(global-set-key [(super \\)] 'find-file-at-point)
Just all-around useful.
(global-set-key [(super s)] 'shell)
(global-set-key [(meta p)] 'shell)
With the second binding in place, I can quickly type M-p M-p RET to return to the shell buffer and repeat the last command I typed there.
Then there are the unbindings:
(global-unset-key "\C-x\C-n")
Never really had a use for set-goal-column, and always kept tripping over it.
(when window-system (global-unset-key "\C-z"))
I hate it when I accidentally type C-z and iconify my frame.
Now we get a little meta:
(defmacro global-set-key* (keys &rest body)
`(global-set-key ,keys (lambda () (interactive) ,#body)))
Just a little keystroke-saving device that lets me write stuff like:
(global-set-key* [(shift control n)] (next-line) (scroll-up 1))
(global-set-key* [(shift control p)] (previous-line) (scroll-down 1))
Credit for this goes to Steve Yegge in http://sites.google.com/site/steveyegge2/effective-emacs
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\C-c\C-k" 'kill-region)
It truly is wonderful to have Ctrl-w mean what I'm used to having it mean.
(Also check that article for more)
One of my favorites was something recommended by a coworker that I initially thought I'd hate:
The arrow keys (up/down/left/right) are remapped to scrolling the current window. To move the cursor, you've still got C-n/p/f/b (or isearch, or tags, or whatever).
(bind "C-t" (lookup-key global-map (kbd "C-x")))
I never transpose characters so I rebound it to mean C-x when used as a prefix key. I just can't stand reaching all the way for the "x" key on Dvorak.
I'm also using this macro since today:
(defmacro bind (key fn)
`(global-set-key (kbd ,key) ,(if (listp fn) fn `',fn)))
Breadcrumb:
(require 'breadcrumb)
(global-set-key [(control f2)] 'bc-set)
(global-set-key [(f2)] 'bc-previous)
(global-set-key [(shift f2)] 'bc-next)
(global-set-key [(meta f2)] 'bc-list)
(global-set-key [(control w)] 'kill-this-buffer)
I was already hitting Ctrl-W out of instinct to close a buffer -- customizing the binding made emacs much easier for me.
Yes, I realize this betrays my origin as a Windows guy. We all make mistakes...
I didn't list these in my first answer, but in retrospect I've always found them useful, and I often see questions from people which could be easily solved by use of the apropos functions, so I think anything which makes them more visible is a good thing! (I also spotted that apropos-library has appeared since I first added these, so writing this answer was useful to me :)
;; Make apropos searches also find unbound symbols, and
;; set new key-bindings for various other apropos commands.
(setq apropos-do-all t)
(global-set-key (kbd "C-h a") 'apropos-command)
(define-prefix-command 'Apropos-Prefix nil "Apropos (a,d,f,l,v,C-v)")
(global-set-key (kbd "C-h C-a") 'Apropos-Prefix)
(define-key Apropos-Prefix (kbd "a") 'apropos)
(define-key Apropos-Prefix (kbd "C-a") 'apropos)
(define-key Apropos-Prefix (kbd "d") 'apropos-documentation)
(define-key Apropos-Prefix (kbd "f") 'apropos-command)
(define-key Apropos-Prefix (kbd "l") 'apropos-library)
(define-key Apropos-Prefix (kbd "v") 'apropos-variable)
(define-key Apropos-Prefix (kbd "C-v") 'apropos-value)
With these bindings, I type C-h C-a whenever I want to search for something, followed by the appropriate character according to the specific kind of search I require (with a prompt to help me if I can't remember the possibilities). If I don't know quite what I'm looking for, then a second C-a (or plain a) at the prompt will run an all-encompassing apropos.
If I can't remember what the prompt characters actually mean, typing C-h again at the prompt (i.e. C-h C-a C-h) will list the bindings.
;; Generally useful
(global-set-key [(meta ?/)] 'hippie-expand)
(global-set-key [(super ?i)] 'imenu)
;; Emacs Lisp navigation
(global-set-key (kbd "C-c f") 'find-function)
(global-set-key [(super ?l)] 'find-library)
;; Compiling things, navigating to errors
(global-set-key [print] 'recompile)
(global-set-key [(shift print)] 'compile)
(global-set-key (kbd "M-p") 'previous-error)
(global-set-key (kbd "M-n") 'next-error)
(global-set-key (kbd "s-p") 'flymake-goto-prev-error)
(global-set-key (kbd "s-n") 'flymake-goto-next-error)
;; Open URLs in Firefox. Still not sure which binding I like most...
(global-set-key (kbd "s-<kp-5>") 'browse-url-firefox)
(global-set-key (kbd "s-<kp-begin>") 'browse-url-firefox)
(global-set-key (kbd "s-t") 'browse-url-firefox)
;; EMMS (music player)
(global-set-key [Scroll_Lock] 'emms-pause)
(global-set-key (kbd "<S-Scroll_Lock>") 'emms-next)
(global-set-key (kbd "<C-Scroll_Lock>") 'emms-show)
;; Navigation between and within buffers
(global-set-key (kbd "C-<backspace>") 'bury-buffer)
(defun scroll-down-one-line ()
"Scroll down one line."
(interactive)
(scroll-down 1))
(defun scroll-up-one-line ()
"Scroll up one line."
(interactive)
(scroll-up 1))
(global-set-key [(super up)] 'scroll-down-one-line)
(global-set-key [(super down)] 'scroll-up-one-line)
(global-set-key [(super right)] 'next-buffer)
(global-set-key [(super left)] 'previous-buffer)
(defun other-window-backwards ()
(interactive)
(other-window -1))
(global-set-key [(control super down)] 'other-window)
(global-set-key [(control super up)] 'other-window-backwards)
(global-set-key "\M-n" 'next-buffer)
(global-set-key "\M-p" 'previous-buffer)
(global-set-key (kbd "C-c w") (quote copy-word))
(global-set-key (kbd "C-c l") (quote copy-line))
(global-set-key (kbd "C-c p") (quote copy-paragraph))
(global-set-key (kbd "C-c s") (quote thing-copy-string-to-mark))
(global-set-key (kbd "C-c a") (quote thing-copy-parenthesis-to-mark))
(global-set-key "\C-o" 'other-window)
(global-set-key "\M-o" 'other-window)
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-key my-keys-minor-mode-map (kbd "C-,") 'beginning-of-buffer)
(define-key my-keys-minor-mode-map (kbd "C-.") 'end-of-buffer)
(define-minor-mode my-keys-minor-mode
"A minor mode so that my key settings override annoying major modes."
t " my-keys" 'my-keys-minor-mode-map)
(my-keys-minor-mode 1)
(global-set-key (kbd "C-c k") 'browse-kill-ring)
(global-set-key (kbd "M-.") 'etags-select-find-tag)
(global-set-key (kbd "M-s l") 'loccur)
(global-set-key (kbd "M-s /") 'multi-occur-in-matching-buffers)
(global-set-key (kbd "C-x M-b") 'bury-buffer)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "M-/") 'hippie-expand)
;; Toggle show-trailing-whitespace.
(global-set-key (kbd "C-c M-w") (function (lambda () (interactive) (setq show-trailing-whitespace (not show-trailing-whitespace)))))
;; Use framemove, integrated with windmove.
(windmove-default-keybindings) ;default modifier is <SHIFT>
(when (require 'framemove nil :noerror)
(setq framemove-hook-into-windmove t))
;Nice list. Here's my block.
(global-set-key [f1] 'revert-buffer)
(global-set-key [f2] 'emacs-wiki-find-file) ; moved to xemacsinit
(global-set-key [f3] 'insert-current-time)
(global-set-key [f4] 'replace-regexp)
(global-set-key [f5] 'replace-string)
(global-set-key [f6] 'goto-line)
(global-set-key [f10] 'linum-mode)
(global-set-key [f11] 'my-shell-command-on-region)
(global-set-key [f12] 'eval-region)
(global-set-key [home] 'beginning-of-buffer)
(global-set-key [end] 'end-of-buffer)
(global-set-key "\C-x\C-n" 'other-window)
(global-set-key "\C-x\C-p" 'other-window-backward)
(global-set-key "\C-z" 'scroll-one-line-ahead)
(global-set-key "\C-q" 'scroll-one-line-behind)
(global-set-key "\C-x\C-q" 'quoted-insert)
(global-set-key "\M-," 'point-to-top)
(global-set-key "\M-." 'point-to-bottom)
(global-set-key "\C-x," 'tags-loop-continue)
(global-set-key "\C-x." 'find-tag)
(global-set-key [(control tab)] 'other-window)
(global-set-key [(control shift right)] 'other-window-backward)
Mine have a few Windows or Eclipse keybindings:
(global-set-key (kbd "<C-tab>") 'helm-mini)
(global-set-key [M-f4] 'kill-emacs)
(global-set-key (kbd "C-w") 'kill-this-buffer)
(global-set-key (kbd "C-o") 'imenu)

Useful keyboard shortcuts and tips for ESS/R

I would like to ask regular ESS/R users what key bindings do they use frequently and tips on using ESS/R.
I have set several shortcuts in my .emacs file. The most useful are:
C-tab to switch between the R command line and the file (similar to josh answer, but much faster):
(global-set-key [C-tab] 'other-window)
Control and up/down arrow keys to search history with matching what you've already typed:
(define-key comint-mode-map [C-up] 'comint-previous-matching-input-from-input)
(define-key comint-mode-map [C-down] 'comint-next-matching-input-from-input)
Comment-uncomment a selected region with C-d or C-maj-d
(defun uncomment-region (beg end)
"Like `comment-region' invoked with a C-u prefix arg."
(interactive "r")
(comment-region beg end -1))
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
Also I've also enabled CUA mode (from options menu) and reconfigured quite a lot of shortcuts to require only two keystrokes (instead of four in standard mode):
;; Delete selection when pressing [delete] key
(delete-selection-mode t)
;; ESS Mode (.R file)
(define-key ess-mode-map "\C-l" 'ess-eval-line-and-step)
(define-key ess-mode-map "\C-p" 'ess-eval-function-or-paragraph-and-step)
(define-key ess-mode-map "\C-r" 'ess-eval-region)
;; iESS Mode (R console)
(define-key inferior-ess-mode-map "\C-u" 'comint-kill-input)
(define-key inferior-ess-mode-map "\C-w" 'backward-kill-word)
(define-key inferior-ess-mode-map "\C-a" 'comint-bol)
(define-key inferior-ess-mode-map [home] 'comint-bol)
;; Comint Mode (R console as well)
(define-key comint-mode-map "\C-e" 'comint-show-maximum-output)
(define-key comint-mode-map "\C-r" 'comint-show-output)
(define-key comint-mode-map "\C-o" 'comint-kill-output)
;; Search with C-f / C-F (control-maj-F for backware search)
(global-set-key "\C-f" 'isearch-forward)
(global-set-key (kbd "C-S-f") 'isearch-backward)
(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
(define-key isearch-mode-map (kbd "C-S-f") 'isearch-repeat-backward)
;; Save with C-s / C-S
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'write-file)
;; need to redefine them for isearch mode (don't know why)
(define-key isearch-mode-map (kbd "C-s") 'save-buffer)
(define-key isearch-mode-map (kbd "C-S-s") 'write-file)
;; Pause = dedicate window.
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
;; delete = delete
(global-set-key [delete] 'delete-char)
;; C-b = list buffers
(global-set-key (kbd "C-b") 'bs-show)
You will find many more useful shortcuts in ESS documentation.
C-c C-z ess-switch-to-end-of-ESS
is nice to jump from your source file that you are editing foo.R to the R console
I found this link to be extremely helpful. It provides elisp code to make Shift+Enter do many common tasks in a context dependent fashion.
http://kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
Great stuff, have been using it for ages. Unfortunately as of 15-11-2013 the uncomment key binding may not work due to EMACS changes (I think, at least it was working before I loaded the latest version). This is because the default uncomment function has 3 arguments but the one defined above has 2. The best way to fix this is to simply delete the uncomment function from the code and retain the keybinding, so it uses the default uncomment function. Or in other words just use this:
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
M-n and M-p in the ESS R console for next/previous command.