How to insert a new line and jump to it, in emacs?

12

3

It's exactly the same as eclipse Shift+Enter.

E.g. I have a some text:

Hello, *everyone.
I'm Freewind.

The * in the first line is the cursor. Then I press some key shortcut, it becomes:

Hello, everyone.
*
I'm Freewind.

Notice there is a new line in the second line, and the cursor is in the new line.

What's the key shortcut can I use?

Freewind

Posted 2011-09-02T18:23:54.633

Reputation: 1 547

3I always use C-e and then enter. – None – 2011-09-02T18:38:34.813

Answers

16

C-e C-m

or

C-e C-j

Both will move to end of the line and add newline. The second will also indent.

Ray Vega

Posted 2011-09-02T18:23:54.633

Reputation: 652

Can I map a key to do this job, so I just need to press once? – Freewind – 2011-09-02T18:42:36.200

That is a key mapping. Emacs allows binding commands to key sequences and most commands are two keys. It's generally a bad idea to bind to one key since most of the one key bindings are taken by emacs fundamentals. – Ross Patterson – 2011-09-02T18:46:20.130

C-e invokes move-end-of-line and C-m invokes newline – Dror – 2014-06-15T19:47:05.473

10

For completeness here is a function:

(defun end-of-line-and-indented-new-line ()
  (interactive)
  (end-of-line)
  (newline-and-indent))

(global-set-key (kbd "<S-return>") 'end-of-line-and-indented-new-line)

phimuemue

Posted 2011-09-02T18:23:54.633

Reputation: 211

Thank you, but how do I use it? Just map a key to this function? Can I map "Shift-Enter" to it? – Freewind – 2011-09-02T18:57:40.717

@Freewind answer updated – Trey Jackson – 2011-09-02T19:54:53.327

3If you use comment-indent-new-line instead, it will also insert the appropriate comment characters if you're currently inside a comment. – phils – 2011-09-03T00:19:17.310

7

You can make something akin to a keyboard macro like this.

(global-set-key (kbd "<S-return>") "\C-e\C-m")

or indeed:

(global-set-key (kbd "<S-return>") (kbd "C-e C-m"))

to avoid using two different kinds of syntax for keys.

kindahero

Posted 2011-09-02T18:23:54.633

Reputation: 1 088