Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs equivalents of Vim's dd,o,O

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?

like image 684
ericteubert Avatar asked Jan 31 '10 21:01

ericteubert


People also ask

Is Emacs harder than Vim?

Emacs tends to be relatively straightforward, similar to commonly used text editors like Notepad. On the other hand, Vim is a power-user's tool, using keyboard shortcuts to speed up tasks. Vim is known to have a much steeper learning curve than Emacs.

Which is more powerful Vim or Emacs?

Productivity and Editing Speed Vim enthusiasts argue that once a user becomes familiar with the editing modes and commands of Vim, it enables far greater productivity and efficiency. File editing is usually faster with Vim than with Emacs because of Vim's purposely speed-driven interface.

Which is faster Emacs or Vim?

Vim was always faster to start up than Emacs. I'm saying that on any machine, out-of-the-box installs of Vim will start up faster than out-of-the-box installs of Emacs. And I tend to think that after a moderate amount of customisation of either one, Vim will still start up faster than Emacs.

Is Emacs slower than Vim?

Speed. Both VIM and Emacs are super fast, especially when compared to modern-day Electron GUI text editors (like Brackets and Atom). There are arguments against Emacs being slower than Vim but the differences are very insignificant. Emacs takes around 1-2 seconds at startup compared to the instant startup of Vim.


2 Answers

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.

like image 76
seh Avatar answered Sep 18 '22 15:09

seh


C+e C+j 

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

like image 23
pjammer Avatar answered Sep 17 '22 15:09

pjammer