How to define emacs keybinding 'C-c C-c'? - emacs

I want to bind the 'C-c C-c' to a custom command, don't know how to.
I tried (global-set-key (kbd "C-c C-c") 'suspend-emacs), but it seams not work.
Any idea will be appreciated.
Thanks.

It is very likely that the local binding of C-c C-c in the current buffer is shadowing the global binding that you make with global-set-key. Conventionally, key sequences consisting of C-c followed by a control character are reserved for major modes. For instance, CC Mode gives C-c C-c a local binding as comment-region. Key sequences consisting of C-c and a letter (either upper or lower case) are set aside for users:
(global-set-key (kbd "C-c c") 'suspend-emacs)
And you may not want to bind suspend-emacs to a new key sequece. suspend-frame, which is bound to C-z and C-x C-z by default, calls suspend-emacs for us when it is invoked from the (controlling) tty device.

Related

emacs cider clear REPL buffer

I simply want to clear the repl buffer so that a single prompt eg (user>) is left on the first line.
I have a keybinding:
(put 'erase-buffer 'disabled nil)
(global-set-key (kbd "C-x C-<backspace>") 'erase-buffer)
But this gives the message :
text is read only
There is the option C-c C-o but this only clears the last return value.
When using python, and run-python the following command C-x M-o which i believe is comint-clear-buffer
cider-repl.el provides a function cider-repl-clear-buffer which by default is bound to:
M-x c-r--bu RET
as C-c M-b is not used by cider-repl as far as I am aware:
(add-hook 'cider-repl-mode-hook
'(lambda () (define-key cider-repl-mode-map (kbd "C-c M-b")
'cider-repl-clear-buffer)))
cider-repl.el also provides cider-repl-handle-shortcut which is bound to ,.
Which will prompt you to many commands, such as clear (which you want), ns (to change namespace), refresh, reload and many others
I find pressing , followd by enter (to choose clear, faster/more convenient than the other answer.)
Note: you need to type , into the repl while the line is empty, it works for both evil and normal emacs keybinds

emacs key bind search buffer to occur

I like the option to do C-s C-w and then show all in a separate buffer by using M-s o, but I would really like to keybind the M-s o ('occur) such that I can type C-s C-w C-, or similar-
I have tried the normal keybind:
(global-set-key (kbd "C-,") 'occur)
But it just do the normal occur, not the search buffer occur.
The command that is bound to M-s o during an isearch isn't the default occur command, but a special version called isearch-occur, that automatically invoke occur on isearch hits.
You can bind this to the C-o (or C-, if you prefer) shortcut without overriding other commands using the define-key command with the isearch-mode-map:
(define-key isearch-mode-map (kbd "C-o") 'isearch-occur)
In this way you can use the sequence C-sC-wC-o.

redefined keys doesn't work correctly

I've redefined key bindings for some basic movement functions in my init.el file:
(global-set-key "\C-j" 'backward-char)
(global-set-key "\C-k" 'next-line)
(global-set-key "\C-l" 'forward-char)
(keyboard-translate ?\C-i ?\H-i)
(global-set-key [?\H-i] 'previous-line)
(global-set-key "\M-j" 'backward-word)
(global-set-key "\M-l" 'forward-word)
And in general (text editing) it perfectly works, but in some modes it executes multiple commands, e.g. in Buffer mode when I press C-k aside from moving the cursor down Emacs marks the listed buffer for deletion. Also, when I call helm-prelude with C-c p h and press one of these key bindings Emacs either doesn't react at all or, in case of C-k, clears the search bar. I thought that the purpose of global-set-key was to bind commands to specific keys everywhere, am I wrong?
Local (e.g., major-mode) keymap bindings trump global keymap (global-map) bindings. And minor-mode keymap bindings trump both of these.
There is a hierarchy of several keymap types that determines which maps take precedence. See the Elisp manual, node Controlling Active Maps (and nearby nodes about keymaps). The full hierarchy is a bit complicated, but most of the time what you need to be aware of is what I stated in the preceding paragraph.
Yes, the global keymap is only used when there is no binding for the key being pressed in a local keymap. For example, the buffer menu mode uses Buffer-menu-mode-map, where C-k is bound to Buffer-menu-delete.
You may have better luck using keyboard-translate to translate these keys to the "normal" Emacs bindings for those commands, i.e. C-p, C-n etc.

Is there a quick way to unbind keys in Emacs?

I did a ctrl h b to view all my bindings in emacs. Now I want to unbind a lot of keys, simply because I never use those functions of Emacs and I don't want to perform them when I accidently press the bound keys! This also frees up a lot of keys for other tasks (for use with Cedet for example). So apart from global-unset-key, is there any method to remove bindings in bulk?
C-a move-beginning-of-line
C-b backward-char
C-c mode-specific-command-prefix
C-d delete-char
C-e move-end-of-line
C-f forward-char
C-g keyboard-quit
C-h help-command
C-k kill-line
C-l recenter-top-bottom
C-n next-line
C-o open-line
C-p previous-line
C-q quoted-insert
C-t transpose-chars
C-u universal-argument
C-v scroll-up
C-x Control-X-prefix
C-z suspend-frame
ESC ESC-prefix
I want to remove most of these bindings which are absolutely useless for me.
There's no built-in way to unset a lot of keys, because it's easy to do it yourself:
(Edited for strict correctness:)
(dolist (key '("\C-a" "\C-b" "\C-c" "\C-d" "\C-e" "\C-f" "\C-g"
"\C-h" "\C-k" "\C-l" "\C-n" "\C-o" "\C-p" "\C-q"
"\C-t" "\C-u" "\C-v" "\C-x" "\C-z" "\e"))
(global-unset-key key))
Although I have to say that most of the commands you call "useless" I would call "essential."
(Edited to add:)
As for freeing up keys for other tasks, there's plenty of unused key real estate:
Key sequences consisting of C-c followed by a letter are by convention reserved for users.
If you have an extra modifier available, like Option on the Mac or the Windows key on a PC, you can associate it with an Emacs modifier like super. I have super-b bound to browse-url-at-point, for example.
If you're not on a plain terminal, the shift key becomes available to distinguish key sequences. For example, I have shift-meta-b bound to bury-buffer.
For commands that are useful but not run often enough to warrant a dedicated key sequence, you can use defalias to provide a shorter name. In my .emacs file, I have (defalias 'ru 'rename-uniquely) and (defalias 'c 'calendar) (among many others).
global-unset-key and local-unset-key are useful, but it's worth having an answer to this question that points out that the general way to unbind a key (for any keymap) is to define a binding of nil:
(define-key KEYMAP KEY nil)
If you follow the code for either of those other functions, you'll notice that this is exactly what they do.

Rebinding C-c to C-c

I'm using Viper, and I want to change its C-c and C-g to the original emacs functions. I can rebind C-g with (define-key viper-vi-global-user-map "C-g" 'keyboard-quit), but how can I rebind C-c, since it's a prefix key?
Thanks!
It may make sense for you to run M-x viper-set-expert-level with an argument of 2 ("Master"). As the viper-mode documentation explains:
2 -- MASTER: C-c now has its standard
Emacs meaning in Vi command state, so
most Emacs commands can be used when
Viper is in Vi state.
As you master viper-mode, you're meant to increase your expert-level setting gradually over time, making more Emacs features available to you (or, as the Viper documentation puts it, "To use Emacs productively, you must reach level 3 or higher").
The original binding for C-c can be set with the following:
(define-key viper-vi-global-user-map (kbd "C-c") 'mode-specific-command-prefix)
The info page for this is Prefix Keys.