In Spacemacs, how do I insert a newline, without entering insert mode? - emacs

In Vim, you can bind Enter key to insert a newline without entering insert mode. how can you do this in Spacemacs.
Or how to remap keys in general in Spacemacs?

In Spacemacs, it is also possible to insert a new line while remaining in normal state with the wildly useful command spacemacs/evil-insert-line-below.
It is bound to SPC i j in Vim mode and M-m i j in Emacs mode.
Preceding that command with a numerical argument will insert more than one new line, e.g. 4 SPC i j will insert four new lines.

You can use:
(define-key evil-normal-state-map (kbd "RET") 'spacemacs/evil-insert-line-below)
to insert a line below and stay at the same position, or
(define-key evil-normal-state-map (kbd "RET")
(lambda ()
(interactive)
(call-interactively 'spacemacs/evil-insert-line-below)
(evil-next-line)))
to insert a line below and go to the new line.

I usually use ]SPC to append a new line below. It is equivalent to SPCij.
Also you can add one line above by [SPC.

Related

How to insert new line (after cursor point) with indentation?

I works in programming mode such as javascript-mode and usually need to push some lines down for formatting purpose.
I find C-o convenient to insert a newline after cursor point. However the line pushed down loses its indentation.
I find RET convenient to insert newline. And the line pushed down is nicely indented. However, the inserted new-line comes after the current cursor point. (EDIT: ) I find it is convenient to keep the cursor position by inserting the newline after it, because sometimes I still need to modify the current line.
Probably something like this:
(defun open-line-and-indent ()
"Like `newline-and-indent', but do not move the point."
(interactive)
(save-excursion
(newline-and-indent)))
(global-set-key (kbd "C-o") #'open-line-and-indent)
;; (define-key javascript-mode-map (kbd "C-o") #'open-line-and-indent)

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

Setting arbitrary cursor positions with multiple cursors in Emacs?

I want to modify different parts of my file with the same string. So I installed multiple-cursors in emacs. But unfortunately, I cannot do the (simple?) thing of marking different parts of the text and start to edit.
Each command I have checked seems not useful to do what I want. For instance consider I need to edit the beginning of line 10 and a character in the middle of line 34. How do I do that?
It sounds like you are looking for the command mc/add-cursor-on-click, which I personally have bound to C-S-<mouse-1>. With this setup, I can hold Ctrl and Shift and click the beginning of line 10, and then click the middle of line 34, and I will have a cursor in both positions.
You can bind this command the way I have like this in your init:
(global-set-key (kbd "C-S-<mouse-1>") 'mc/add-cursor-on-click)
You can replace the kbd with another combination too if you want.
The following is derived from https://github.com/magnars/multiple-cursors.el/issues/44 and seems like a reasonable keyboard-driven solution.
(require 'multiple-cursors)
(defun mc/toggle-cursor-at-point ()
"Add or remove a cursor at point."
(interactive)
(if multiple-cursors-mode
(message "Cannot toggle cursor at point while `multiple-cursors-mode' is active.")
(let ((existing (mc/fake-cursor-at-point)))
(if existing
(mc/remove-fake-cursor existing)
(mc/create-fake-cursor-at-point)))))
(add-to-list 'mc/cmds-to-run-once 'mc/toggle-cursor-at-point)
(add-to-list 'mc/cmds-to-run-once 'multiple-cursors-mode)
(global-set-key (kbd "C-S-SPC") 'mc/toggle-cursor-at-point)
(global-set-key (kbd "<C-S-return>") 'multiple-cursors-mode)
Firstly use mc/toggle-cursor-at-point to mark each position where you want a cursor, and then invoke multiple-cursors-mode to activate them.

Key map for ex command in emacs evil-mode

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/")))

Reloading AucTex Labels and Defining Keybindings

When I add a label in emacs to a .tex file, I used to reload the file to get it to show up in RefTeX. i.e. C-c ) wouldn't have the new label unless I reloaded the file.
After some searching I found that C-u C-c ) will refresh RefTeX before trying to do the reference. This works as I would like, but I would like to use C-c r for this command instead of typing C-u C-c ) every time. How do I do this?
Thanks,
Jim
I don't use reftex but as far as I can understand you want just to define a binding:
(define-key reftex-mode-map (kbd "C-c r") 'reftex-reference)
Building on Oleg's answer:
Maybe it would be better to feed it the C-u argument already if that's what you are after:
(defun call-reftex-reference-directly ()
(interactive)
(let ((current-prefix-arg 4)) ;; emulate C-u
(call-interactively 'reftex-reference) ;; invoke reftex-reference
)
)
(define-key reftex-mode-map (kbd "C-c r") 'call-reftex-reference-directly)
Maybe try if this works for you?
You can type r in the label selection buffer to refresh it without reloading the file. The refresh is instantaneous (unless you have very large/very many linked files).