Reputation: 2811
In vim, I can do this in command mode by typing 'o', which will add a new line below the cursor, and enter the insert mode.
Is there an equivalent in emacs?
Upvotes: 45
Views: 26817
Reputation: 27
I am using Emacs 24.Insert the line to ".emacs" file.
;; Move cursor to end of current line
;; Insert new line below current line
;; it will also indent newline
(global-set-key (kbd "<C-return>") (lambda ()
(interactive)
(end-of-line)
(newline-and-indent)))
;; Move cursor to previous line
;; Go to end of the line
;; Insert new line below current line (So it actually insert new line above with indentation)
;; it will also indent newline
(global-set-key (kbd "<C-S-return>") (lambda ()
(interactive)
(previous-line)
(end-of-line)
(newline-and-indent)
))
<C + Return>
that means <Ctrl + Enter>
for new line belowand
<C + S + Return>
that means <Ctrl + Shift + Enter>
for new line above.
Both will indent also. I hope it will work.
Upvotes: 3
Reputation: 6049
I am using emacs 25
and I have something like this:
;; Insert new line below current line
;; and move cursor to new line
;; it will also indent newline
(global-set-key (kbd "<C-return>") (lambda ()
(interactive)
(end-of-line)
(newline-and-indent)))
;; Insert new line above current line
;; and move cursor to previous line (newly inserted line)
;; it will also indent newline
;; TODO: right now I am unable to goto previous line, FIXIT
(global-set-key (kbd "<C-S-return>") (lambda ()
(interactive)
(beginning-of-line)
(newline-and-indent)
(previous-line)))
Hope it will help :)
Upvotes: 3
Reputation: 31
I use following key bindings make it work similar to vim's o and O:
<pre>
;; vi-like line insertion
(global-set-key (kbd "C-o") (lambda () (interactive)(beginning-of-line)(open-line 1)))
(global-set-key (kbd "M-o") (lambda () (interactive)(end-of-line)(newline)))
</pre>
Upvotes: 2
Reputation: 91
This configuration could help:
(defun newline-without-break-of-line ()
"1. move to end of the line.
2. open new line and move to new line"
(interactive)
(end-of-line)
(open-line 1)
(right-char))
(global-set-key (kbd "<M-return>") 'newline-without-break-of-line)
Upvotes: 1
Reputation: 4676
have you solved your problem?
I just solved this problem. Feel free to use this code :)
You can bind to every key you like in the global-set-key
,also to replace newline-and-indent
with newline
in case you don't like the new line to be indented.
;; newline-without-break-of-line
(defun newline-without-break-of-line ()
"1. move to end of the line.
2. insert newline with index"
(interactive)
(let ((oldpos (point)))
(end-of-line)
(newline-and-indent)))
(global-set-key (kbd "<C-return>") 'newline-without-break-of-line)
Upvotes: 21
Reputation: 7471
I am using prelude, and S-RET is equivalent to vi's o and C-S-RET is equivalent to vi's O.
Upvotes: 10
Reputation: 3760
The command C-o open-line
that others have suggested is not quite the same as o in vi, because it splits the current line and lets the cursor remain in the current line.
You get the exact same effect as vi's o with two strokes: C-e RET, which moves the cursor to the end of the current line and then inserts a new line, which leaves the cursor at the beginning of that line.
You could bind that sequence to a key of its own (perhaps overriding the existing definition of C-o), but I doubt if it's worth the trouble.
(Incidentally, the symmetric sequence C-a RET gives you the effect of vi's capital O, inserting a line before the current line.)
Upvotes: 55