emacs cider clear REPL buffer - emacs

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

Related

Emacs, bind "convert buffer to dos format" to f11 key

I'm attempting to bind this series of commands
C-x RET f undecided-dos
to my keyboard f11 key. So far I've tried many things such as
\C-x RET \f undecided-dos
in my .emacs file but no success. Please show me the correct syntax.
If you can complete a command interactively, you can then query Emacs for what the function you performed is called. Try M-x repat-complex-command and press the up arrow once (or more times if you have completed other commands in the interim) or ask for key binding help:
C-h k C-x RET f
=> set-buffer-file-coding-system
Unfortunately, you can't bind this directly to a keystroke:
;;;; BROKEN
(global-set-key (kbd "<f11>") '(set-buffer-file-coding-system 'dos-undecided))
... because when you try to run that, you run into
Wrong type argument: commandp, (set-buffer-file-coding-system (quote dos-undecided))
You can work around that by specifying an interactive form around it:
(global-set-key (kbd "<f11>")
(lambda ()
(interactive "*")
(set-buffer-file-coding-system 'undecided-dos)))
The "*" argument to interactive says it is only allowed in buffers that you have permission to modify.

Bind command to C-RET in Emacs

Say I have some interactive function in Emacs my-function, how can I bind it to Ctrl + RET?
I have tried with:
(global-set-key (kbd "C-RET") 'my-function)
and
(global-set-key (kbd "C-return") 'my-function)
but none of them seem to work. is this at all possible?
Always remember that kbd very conveniently accepts the exact same syntax that Emacs gives you when you ask it about a key sequence, so you never ever have to guess.
C-hkC-RET tells me:
<C-return>
therefore I would use (kbd "<C-return>")
OTOH, when running Emacs in my terminal, C-hkC-RET tells me:
C-j
because C-RET isn't a valid control character in a terminal, and therefore Emacs isn't receiving the same input that it gets in GUI mode (so I wouldn't be able to use that binding in my terminal).
This should work:
(global-set-key [(control return)] 'my-function)
It works for me, but may not in a terminal as per #phils's answer.

Is there a way to do a history search in nrepl?

You know how when you hit the up arrow in bash it will fill in the last command you typed in? Is there any way to do this in nrepl?
So far I've been doing a reverse search (C-r), typing the first few characters of the line in question, killing the line(s) (C-k), jumping to the end of the buffer (M->) and yanking the killed line (C-y). Is there an easier way to do this?
You can use M-p and M-n to navigate up and down in the input history. Also, the current input can be used as a search pattern, i.e. type the start of the command you want to match, then M-p will take you to the next match. This uses the functions nrepl-previous-input and nrepl-next-input. If you don't like those keybindings, you can also rebind to <up> and <down>:
(define-key nrepl-mode-map (kbd "<up>") 'nrepl-previous-input)
(define-key nrepl-mode-map (kbd "<down>") 'nrepl-next-input)
Just add this to your .emacs (and evaluate C-x C-e after each line if you don't want to restart your Emacs). Also, note that M-n and M-p are likely to be bound to similar functionality in other REPL and comint like modes.
If you're using Cider, you can add the following to your user config:
(define-key cider-repl-mode-map (kbd "<up>") 'cider-repl-previous-input)
(define-key cider-repl-mode-map (kbd "<down>") 'cider-repl-next-input)
To persist the history for the next time you open a repl, you also have the following options:
(setq cider-repl-wrap-history t)
(setq cider-repl-history-size 1000)
(setq cider-repl-history-file "~/.cider-repl-history")
cider-repl-history-file is required if you want a persistent history. If you use a relative path, the history will be local to the current project.

Paste a word at ansi-term on Emacs

I use ansi-term on Emacs and need to paste some words there. "Paste" only works with mouse mid-button. I know that with C-x C-j and C-c C-k, we can switch between char run and line run, but it is inconvenient. I prefer to use C-y or C-c y to do the job. Searched online but the solutions didn't work with my emacs23.
There's two options here: use the inferior process or Emacs.
To use the inferior process (probably something that uses readline), just send raw C-y characters.
(define-key term-raw-map (kbd "C-k") 'term-send-raw)
(define-key term-raw-map (kbd "C-y") 'term-send-raw)
Then C-k and C-y get sent directly to the terminal, where they function like they would in any other terminal (e.g. kill to end of line and yank, respectively). Since the inferior process is receiving and interpreting the keypresses, Emacs will have nothing to do with the kills and yanks.
To use Emacs's kill ring, use term-paste.
(define-key term-raw-map (kbd "C-c C-y") 'term-paste)
Personally, I like to treat term-mode buffers like regular terminals, so I usually use the mouse to copy/paste and C-k/C-y when I'm editing a command line.
FWIW, I use multiterm, and I do
(with-eval-after-load "multi-term"
(dolist
(bind '(("C-k" . term-send-raw)
("C-y" . term-send-raw)
("C-c C-y" . term-paste)
))
(add-to-list 'term-bind-key-alist bind)))

emacs -- keybind questions

I have successfully used Ctrl+Shift+Up ' Ctrl+Shift+down '
Ctrl+Shift+left' Ctrl+Shift+Right to different commands. But when I
tried to use Ctrl+s to the command save-buffer and Ctrl+Shift+s, which
is equivalent to Ctrl+S, to another command, it has some problem.
save-buffer works fine, but when I type Ctrl+Shift+s, it excute
the command save-buffer. I used Ctrl+q to find the control sequences of
Ctrl+s and Ctrl+Shift+S, I get the same result, which is ^S.
I expect that I will get ^s for Ctrl+s, but it doesn't.
Anyone knows the reason?
Another queston is: I use Ctrl+c for the command killing-ring-save. In this
case, all commands (which are of large number) begin with Ctrl+c don't work now.
Is there a way to replace the prefix Ctrl+c by another customized prefix?
I may pose my question in the wrong direction. I use ctrl+c as
killing-ring-save. It works fine in emacs (no mode). But if I open a .c file (C-mode), then
when I type Ctrl+c, it waits me to type another key. I think in this case,
ctrl+c is regarded as a prefix. In this case, I need the following modifications:
Using a custom defined prefix, say Ctrl+a, as Ctrl+c ; Remove the
prefix Ctrl+c ; Using Ctrl+c as killing-ring-save.
I add the following to my ~/.emacs :
(global-set-key (kbd "C-a") mode-specific-map)
(global-set-key (kbd "C-c") 'kill-ring-save)
(global-set-key (kbd "C-f") 'isearch-forward)
(global-set-key (kbd "C-v") 'yank)
(global-set-key (kbd "C-s") 'save-buffer)
(defun my-c-initialization-hook ()
(define-key c-mode-base-map (kbd "C-a") mode-specific-map)
(define-key c-mode-base-map (kbd "C-c") 'kill-ring-save))
(add-hook 'c-initialization-hook 'my-c-initialization-hook)
But this doesn't work. Ctrl+c is still regarded as a prefix, so I can't use it
as kill-ring-save. Furthermore, if I type Ctrl+a Ctrl+c, it said it's not
defined. (I thought it will have the same result as I type Ctrl+c Ctrl+c)
The C-c binding is tricky, CUA mode solves it well, by only making it do kill-ring-save when you have a region marked.
First, Control-S is an ASCII control character -- ^s and ^S are the same character.
Keys are something different from characters, however, and if you are using Emacs with a window manager then you can distinguish the keys C-s and C-S-s. The latter is Control-Shift-s.
The problem you are hitting is that if you do not explicitly bind the shifted version of a letter key, then the shifted letter key uses the binding of the unshifted key. This is a "feature".
So you need to bind both C-s and C-S-s.
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'another-command)
If you're running emacs in a terminal, then the reason for the shift-ctl-c issue could be the terminal driver. In that case, give the command stty stop undef, then run emacs again, and see if it affects the problem. Also, see if you get same problem with shift-ctl-other letters