Key map for ex command in emacs evil-mode - emacs

In emacs evil-mode, how do I bind a key sequence so that it pre-populates the evil-mode ex command line and positions the cursor? In vim, I can do this:
nnoremap g/r :%s//g<left><left>
In emacs, I tried this (and several variations):
(define-key evil-normal-state-map "g/" nil)
(define-key evil-normal-state-map (kbd "g/r")
(lambda () (interactive) (kbd ":%s/")))
It has no effect, and I don't see any messages after trying the keymap.
It looks like emacs used to have a useful function evil-ex-read-command that sent input to the evil-mode command line:
https://github.com/magnars/.emacs.d/blob/master/site-lisp/evil/evil-ex.el#L554
But that function doesn't seem to be available anymore.

If you mean to bind the key combination
Press and release g
Press and release /
Press and release r
your string in kdb should be "g / r".
Emacs is not based on keystrokes as vim is, but keystrokes are just a means to execute functions. So pressing k in normal mode does not execute the function k (as in vim), but self-insert-char. That means that you do not bind the combination g / r to equal some other keystrokes, but rather to call an arbitrary function. And evil defines an evil-ex function, that does exactly what you want (Actually it's the exact function, that is called, when you press : in normal mode).
Untested but it should work
(define-key evil-normal-state-map (kbd "g / r") (lambda () (evil-ex "%s/")))

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

Keymapping in emacs using semantic with interactive functions and default parameters invocation

In semantic; to move around function declarations it is possible to use C-c , J to open declaration, and just C-u C-SPC to return where the function was called. However to map those functions to some other short keybindings like that M-right (meaning alt key in combination with right arrow), so in our .emacs we can have:
(define-key global-map [(M-right)] 'semantic-complete-jump).
This indeed works because C-c , J is mapped to invoke the semantic-complete-jump function.
So two questions:
How to map M-left to the C-u C-SPC? remembering that C-u is not a part of the command, it is just the argument passed to the invoked function.
Is there any way to invoke semantic-complete-jump via C-c , J without being interactive and using by default always the default value (that it is mainly the word under where is the cursor)? This will allow to avoid one additional keystroke moving much faster around the code.
This is possible to do with M-. (mapped to find-tag) and M-* (mapped to pop-tag-mark) playing with tags and etags with emacs, but using semantic it seems to be much more powerful and ideal for big projects with large amount of code.
S̲o̲ ̲t̲h̲e̲ ̲p̲r̲e̲v̲i̲o̲u̲s̲ ̲t̲w̲o̲ ̲q̲u̲e̲s̲t̲i̲o̲n̲s̲ ̲w̲h̲a̲t̲ ̲a̲r̲e̲ ̲a̲s̲k̲i̲n̲g̲ ̲i̲s̲: what configuration lines are needed just to use M-right to move inside the function declarations (without being asked) and M-left to go to the previous point were this function was called using semantic.
Here's what I've got:
(add-hook
'c-mode-common-hook
(lambda()
(define-key c-mode-base-map
(kbd "C-x C-h") 'semantic-ia-fast-jump)))
(global-set-key
(kbd "M-p")
(lambda()(interactive) (set-mark-command 4)))

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 can't reset Ctrl-d key behaviour

I wanted to change the behaviour of Ctrl-d key. So it will delete a word backward. I created a function:
(defun backward-delete-word (arg)
"Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (backward-word arg) (point))))
Then inserted this into emacs.d:
(global-set-key (kbd "\C-d") 'backward-delete-word)
It works in fundamental-mode, but in php-mode it just removes the next character. When I click
Ctrl-h k Ctrl-d
Emacs gives this:
C-d runs the command c-electric-delete-forward, which is an
interactive compiled Lisp function in `cc-cmds.el'.
It is bound to C-d.
(c-electric-delete-forward ARG)
Somehow, it was reset to another function. How to find out, where it was reset and make it work with my function instead?
I don't have php-mode so I can't say for sure, but the binding is likely overriden in php-mode-map (which, as a major mode map, has higher precedence than the global map).
You can check by using C-h b to list all available key bindings and look for C-d or c-electric-delete-forward in the output buffer to see in which keymap the binding is defined.
Assuming php-mode-map overrides the C-d binding, you can disable it using
(define-key php-mode-map (kbd "C-d") nil)

Emacs Ctrl modifiers don't work in console

I have 2 hotkeys for dired, that work in GUI mode in Emacs:
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<up>")
(lambda () (interactive) (find-alternate-file "..")))))
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<right>") 'diredp-find-file-reuse-dir-buffer)))
But when I click CTRL+→ or CTRL+↑ in console the cursor just moves as if the arrow was pressed.
When I try CTRL+H K and then CTRL+→, it gives me the right key docs as if CTRL wasn't pressed at all.
How to fix this strange behaviour in console?
I am using Linux Slackware 14, Emacs 24.2.1.
Here is the algorithm, how to make modifier keys work in Emacs in terminal.
1.Create a file funcskeys with content:
control keycode 105 = F100
string F100 = "\033[1;5D"
control keycode 106 = F101
string F101 = "\033[1;5C"
control keycode 103 = F102
string F102 = "\033[1;5E"
altgr keycode 105 = F103
string F103 = "\033[1;5F"
At the end must be an empty line!
2.Under root load the file:
#loadkeys funcskeys
3.Put this into the beginning of .emacs:
(unless (display-graphic-p)
(progn
(define-key input-decode-map "\e[1;5C" [(control right)])
(define-key input-decode-map "\e[1;5D" [(control left)])
(define-key input-decode-map "\e[1;5E" [(control up)])
(define-key input-decode-map "\e[1;5F" [(meta left)])))
End of algorythm
After this hotkeys will work:
(global-set-key (kbd "C-<right>") 'forward-word)
(global-set-key (kbd "C-<left>") 'backward-word)
Your terminal likely doesn't produce different escape sequences for CTRL-right‬ versus just right.
You can easily verify this by typing CTRL-v CTRL-right‬ and CTRL-v right‬. Here, CTRL-v tells the terminal to print the escape sequence for the key that follows. If these two produce the same sequences then your terminal sends the exact same information to Emacs whether you press CTRL or not.
For instance, if I type these shortcuts in a Gnome terminal, I get:
^[[C for CTRL-v right‬
^[[1;5C for CTRL-v CTRL-right‬
When I do the same on one of the Linux consoles, I get:
^[[C for CTRL-v right‬
^[[C for CTRL-v CTRL-right‬
As you can see, in the latter case it's exactly the same result for both key sequences, hence there's no way Emacs can distinguish the two.
The only way to fix this is to convince your terminal to send a different sequence when you hold down the CTRL key - see this question for more information.
An easier work-around would be to simply use different key bindings in Emacs.
Look out for loadkeys. At least in Debian/Ubuntu it's in the package kbd. With it, you can modify your keyboard layout and probably also some more "exotic" key combinations.