Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently playing around with emacs and happy with most of the concepts. But I really adored the convenience of the three vim commands: dd,o,O Hopefully you can tell me how to mirror them in emacs :)

dd - deletes whole line, including newline, no matter where the cursor is.

I found something similar to do the trick:

C-a C-k C-k

While C-a moves the cursor to the beginning of the line, the first C-k kills the text, the second one kills the newline. The only problem is that this is not working on empty lines where I only need to type C-k which is quite inconvenient as I have to use different commands for the same task: killing a line.

o / O - creates a new empty line below / above cursor and moves cursor to the new line, indented correctly

Well, C-a C-o is nearly like O, just the idention is missing. C-e C-o creates an empty line below the current but does not move the cursor.

Are there any better solutions to my problems or do I have to learn Lisp and define new commands to fulfill my needs?

share|improve this question

6 Answers 6

up vote 20 down vote accepted

For o and O, here are a few functions I wrote many years ago:

(defun vi-open-line-above ()
  "Insert a newline above the current line and put point at beginning."
  (interactive)
  (unless (bolp)
    (beginning-of-line))
  (newline)
  (forward-line -1)
  (indent-according-to-mode))

(defun vi-open-line-below ()
  "Insert a newline below the current line and put point at beginning."
  (interactive)
  (unless (eolp)
    (end-of-line))
  (newline-and-indent))

(defun vi-open-line (&optional abovep)
  "Insert a newline below the current line and put point at beginning.
With a prefix argument, insert a newline above the current line."
  (interactive "P")
  (if abovep
      (vi-open-line-above)
    (vi-open-line-below)))

You can bind vi-open-line to, say, M-insert as follows:

(define-key global-map [(meta insert)] 'vi-open-line)

For dd, if you want the killed line to make it onto the kill ring, you can use this function that wraps kill-line:

(defun kill-current-line (&optional n)
  (interactive "p")
  (save-excursion
    (beginning-of-line)
    (let ((kill-whole-line t))
      (kill-line n))))

For completeness, it accepts a prefix argument and applies it to kill-line, so that it can kill much more than the "current" line.

You might also look at the source for viper-mode to see how it implements the equivalent dd, o, and O commands.

share|improve this answer

For dd, use "kill-whole-line", which is bound to "C-S-backspace" by default in recent versions of Emacs.

I should add that I myself use whole-line-or-region.el more often, since C-w is easier to type than C-S-backspace.

share|improve this answer
C+e C+j

According to the emacs manual docs. That gets you a new line and indentation.

share|improve this answer

You could create a macro and bind it to a key sequence. No need to learn any emacslisp yet.

share|improve this answer

Here's how I addressed the issue of Emacs's lack of a vi-like "O" command:

(defadvice open-line (around vi-style-open-line activate)
  "Make open-line behave more like vi."
  (beginning-of-line)
  ad-do-it
  (indent-according-to-mode))

With this in place, I've never really felt the need for a corresponding version of vi's "o" command. C-n C-o does the trick.

As for the "dd" command, that grated a little at first, but I eventually came around to Emacs's way of doing things. Anyway, when I want to delete several lines at once, which is often the case, I just do it using the region (C-a C-SPC, go to the other end of the text I want to delete, C-w). Or if I can eyeball the number of lines I want to delete, I'll do eg. M-9 C-k to delete nine lines at once.

share|improve this answer

Just use Viper-mode, Vimpulse or Vim Mode, Emacs keybindings are just not as ergonomic.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.5.22.2603\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nos"], "copyright__copyright__copyright": ["o to the other end of the text I want to delete, C-w). Or if I can eyeball the number of lines I want to delete, I'll do eg. M-9 C-k to delete nine lines at once.\n\nshare|improve this answer\n\nJust use Viper-mode, Vimpulse or Vim Mode, Emacs keybindings are just not as ergonomic.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}