How to insert new line in Emacs editor?

How to insert new line in Emacs editor?

Several few ways are explained here on how to insert a new line before the current line while editing text with Emacs editor.

Let us say there are few line like following:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed sit amet▮ipsum mauris.

You can see the cursor above as ▮, now imagine your cursor is on that line after the word amet and you wish to insert new line. The way to go is to use C-a to go to the beginning of the line and C-o to open a new line. C-o invokes the function open-line.

Result will be as following:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
▮ <- cursor will be here
Sed sit amet ipsum mauris.

If you are already with the cursor at beginning of the line, all what you have to do is C-o to insert new line.

Command open-line or Emacs key binding C-o is similar to vi editor command O to insert new line above the current line.

The command open-line is in my opinion well described. If cursor is somewhere in the middle of the line or at the beginning of the line, it will make the line empty and move anything from cursor and after cursor down for one line.

In the following example, one can see the cursor’s position:

Maecenas congue ligula ▮ consectetur ante hendrerit.

then when C-o or M-x open-line is invoked this is what happens:

Maecenas congue ligula ▮
consectetur ante hendrerit.

then one may continue writing:

Maecenas congue ligula  ac quam viverra nec 
consectetur ante hendrerit.

Thus there are 3 ways to open a new line or insert new line starting from current line with cursors:

  1. If cursor is at the beginning of the line then do simple C-o and the new line is inserted, current line is pushed down.

  2. If cursor is not at the beginning of the line and you wish to insert new line above the current one, use first C-a to move to beginning of the line and then C-o to insert new line.

  3. When cursors is somewhere on the line and you wish to open line, to make it empty for writing, then just press C-o and continue writing. The rest of the current line after the cursor will be pushed down.

Command open-line in Emacs editor and the key binding C-o is very similar to O command in vi editor or vim and other vi-like editors. But it does not do the same.

If you wish to insert new line above the current line regardless of the cursor position and practically skip doing manually the key binding C-a to move to the beginning of the line, you may use the following function:

(defun my-C-o ()
  "Opens new line regardless where is cursor positioned."
  (interactive)
  (move-beginning-of-line nil)
  (open-line 1))

;; which you may configure to be globally evailable in Emacs with:
(global-set-key (kbd "C-o") #'my-C-o)

Related pages

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: