Paste a word at ansi-term on Emacs - 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)))

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 evil-mode how to change insert-state to emacs-state automatically

I don't like the insert-state, and so I want to replace it with emacs-state. But this setting does not work:
(add-hook 'evil-insert-state-entry-hook 'evil-emacs-state)
After press o or cw, I am still in insert-state.
How about this approach:
(setq evil-insert-state-map (make-sparse-keymap))
(define-key evil-insert-state-map (kbd "<escape>") 'evil-normal-state)
I use it and it seems to do the trick. And since you're not changing the state, you retain state-related configs like cursor-color, etc.
Surprised nobody posted this yet...
(defalias 'evil-insert-state 'evil-emacs-state)
Anything that tries to call evil-insert-state will just end up calling evil-emacs-state. Works for i, a, o, O, etc.
There is now a bulitin way for Evil to do this
(setq evil-disable-insert-state-bindings t)
before loading evil
Reference: https://github.com/noctuid/evil-guide#use-some-emacs-keybindings
Tell me how this works. It's a hack that basically replaces the function evil-insert-state with evil-emacs-state. The problem is figuring out how to exit emacs state with the escape key. For instance, this version works fine when I exit emacs state with the ESC key, but not when I try to do the same with C-[:
; redefine emacs state to intercept the escape key like insert-state does:
(evil-define-state emacs
"Emacs state that can be exited with the escape key."
:tag " <EE> "
:message "-- EMACS WITH ESCAPE --"
:input-method t
;; :intercept-esc nil)
)
(defadvice evil-insert-state (around emacs-state-instead-of-insert-state activate)
(evil-emacs-state))
If the point is that you want to use normal Emacs editing when doing the kind of tasks vi uses insert mode for, then wiping the insert mode dictionary accomplishes this. It is probably desirable that the ESC key gets you back into normal mode and have C-z get you into Emacs state; Leo Alekseyev posts a tiny bit of code that does this:
(setcdr evil-insert-state-map nil)
(define-key evil-insert-state-map
(read-kbd-macro evil-toggle-key) 'evil-emacs-state)
which I use and like. There are two potential disadvantages to being in insert mode rather than emacs mode:
You can't use the ESC key as another, prefixed way of ALT-keymapping; and
There is a risk (so I am told, though I haven't encountered this) if you are accessing Emacs through a tty, that Emacs will interpret ALT-modified keys as ESC followed by the character, which gives a difference in insert mode than in emacs mode.
I don't think either problem is serious.
How I became a unix chad:
;; unix chad setting
(defalias 'evil-insert-state 'evil-emacs-state)
(define-key evil-emacs-state-map (kbd "<escape>") 'evil-normal-state)
(setq evil-emacs-state-cursor '(bar . 1))
From the documentation about evil-emacs-state-entry-hook:
Hooks to run when entering Emacs state.
So the evil-emacs-state function is run when you enter emacs-state (with C-z).
You can, however, do this:
(define-key evil-normal-state-map (kbd "i") 'evil-emacs-state)
The problem now is exiting emacs state. I remember there were some problems binding ESC in emacs state, as ESC is used as META, and (IIRC) Evil uses some "special" code to intercept the ESC key.
EDIT: following your comment: this one should work:
(fset 'evil-insert-state 'evil-emacs-state)

Why doesn't my keymap for C-c C-c work in C++?

I'm new to emacs and using emacs 24 and trying to bind C-c C-c to a function to comment out a single line. I have the following in my init.el file but it doesn't seem to work in c++.
(defun toggle-comment-on-line ()
"comment or uncomment current line"
(interactive)
(comment-or-uncomment-region (line-beginning-position) (line-end-position))
(next-line))
(global-set-key (kbd "C-c C-c") 'toggle-comment-on-line)
When I'm playing around in the scratch page it works fine and when I check with C-h k C-c C-cit displays the right function but when I'm in C++ the same command displays the text:
C-c C-c runs the command comment-region, which is an interactive
compiled Lisp function in `newcomment.el'.
It is bound to C-c C-c, <menu-bar> <C++> <Comment Out Region>.
(comment-region BEG END &optional ARG)
Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.
The strings used as comment starts are built from `comment-start'
and `comment-padding'; the strings used as comment ends are built
from `comment-end' and `comment-padding'.
By default, the `comment-start' markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments). This can be
changed with `comment-style'.
I assume something else is overriding C++ keybindings but I don't know what or how to fix it? Does anyone have any ideas?
Yes, c++ mode has its own keymap, which overrides the global map. Use the following instead:
(define-key c++-mode-map (kbd "C-c C-c") 'toggle-comment-on-line)
I've improved your code a bit, and below there's also the
code to bind the key without an error
(it happens because you're trying to define a key in
c++-mode-map before it was defined)
(defun toggle-comment-on-line ()
"comment or uncomment current line"
(interactive)
(let ((beg (if (region-active-p)
(region-beginning)
(line-beginning-position)))
(end (if (region-active-p)
(region-end)
(line-end-position))))
(comment-or-uncomment-region beg end)
(next-line)))
(add-hook 'c++-mode-hook
(lambda()
(define-key c++-mode-map (kbd "C-c C-c") 'moo-complete)))
As a side note, I strongly recommend against binding C-c C-c,
as this is a very popular mode specific binding that's different in every mode,
but means generally confirm:
in org-mode it evaluates a babel block of code
in message-mode it sends the email
in python-mode it sends the buffer to the process
in wdired it confirms your edits to the file names
So you'll really have a headache if you bind it, unless you're
using Emacs just for c++-mode.
I've been using Emacs for 3 years now and I have comment-dwim
on C-.. I'm quite happy with it so far.
If you're willing to use a different key binding, you can use the following code:
;; Nothing to see here.
after which you can do C-a C-SPC C-n M-;.

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.

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