OpenNextLine

Last edit

Summary: Removed confusing sentence.

Changed:

< The `o' and `O' commands of vi are closely equivalent to `C-a C-M-o' and `C-e C-j' in Emacs, respectively. Although, open-line and split-line do break a line in half that vi users are not accustomed. The `open-next-line' and `open-previous-line' functions above accept a `C-u' prefix, while `split-line' (`C-M-o') does not. This doesn't get you exactly vi-like behavior as everything you type after an `o' or `O' and before you hit ESC is inserted /prefix/ times in vi. Ultimately KeyboardMacros would be the answer there.

to

> The `o' and `O' commands of vi are closely equivalent to `C-a C-M-o' and `C-e C-j' in Emacs, respectively. The `open-next-line' and `open-previous-line' functions above accept a `C-u' prefix, while `split-line' (`C-M-o') does not. This doesn't get you exactly vi-like behavior as everything you type after an `o' or `O' and before you hit ESC is inserted /prefix/ times in vi. Ultimately KeyboardMacros would be the answer there.


Some people (such as JerameyCrawford and JeremyCowgar) don’t much care for how open-line works and prefer the ViKeys version. Here is how to make “C-o” act like vi’s “o” and “M-o” act like vi’s “O”:

    ;; Behave like vi's o command
    (defun open-next-line (arg)
      "Move to the next line and then opens a line.
    See also `newline-and-indent'."
      (interactive "p")
      (end-of-line)
      (open-line arg)
      (next-line 1)
      (when newline-and-indent
        (indent-according-to-mode)))
    (global-set-key (kbd "C-o") 'open-next-line)
    ;; Behave like vi's O command
    (defun open-previous-line (arg)
      "Open a new line before the current one. 
     See also `newline-and-indent'."
      (interactive "p")
      (beginning-of-line)
      (open-line arg)
      (when newline-and-indent
        (indent-according-to-mode)))
    (global-set-key (kbd "M-o") 'open-previous-line)
 
    ;; Autoindent open-*-lines
    (defvar newline-and-indent t
      "Modify the behavior of the open-*-line functions to cause them to autoindent.")

The ‘o’ and ‘O’ commands of vi are closely equivalent to ‘C-a C-M-o’ and ‘C-e C-j’ in Emacs, respectively. The ‘open-next-line’ and ‘open-previous-line’ functions above accept a ‘C-u’ prefix, while ‘split-line’ (‘C-M-o’) does not. This doesn’t get you exactly vi-like behavior as everything you type after an ‘o’ or ‘O’ and before you hit ESC is inserted prefix times in vi. Ultimately KeyboardMacros would be the answer there.

I think TextMate uses Shift+Enter for “open next line”. (Ctrl+Shift+Enter might be a good candidate for “open previous line”.) So if you’re into consistency,

    (global-set-key [S-return]   'open-next-line)
    (global-set-key [C-S-return] 'open-previous-line)

and comment out the lines above which begin with “(global-set-key …”. Also, this retains the original ‘C-o’, which still has its uses.


CategoryEditing CategoryEmulation