Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Emacs - the editor that STILL doesn't have a simple way to duplicate the current line?


I don't think it's that hard to do C-a, C-k then C-y as many times as you want.

If you want to bind it to a key you could record that as a macro for e.g., but I find that sequence is simple enough that I don't think hard about it


For the equivalent of vi's 'yyp', you must do C-a C-k C-k C-y C-y, or more likely 'C-akkyy'.

The first C-k only kills to end-of-line, to capture the line ending (\n) you need another C-k. The first C-y only puts back what you killed, so that needs to be doubled also to actually change the buffer in the desired way.

Subsequent C-y commands will add more copies of the line, but I find that 99% of the time it's only a single duplicate of a line that I want.

C-S-Backspace C-yy does the same thing, btw.


I think the germane point about Emacs here is that some people like writing Lisp and some people hate it. If you hate it Emacs is likely not going to be your best environment, and that’s fine.

I really like writing Lisp so Emacs has a simple way of doing anything I want, but it’s not for everyone.


I love writing Lisp and do it for a living, but I can’t even remember the last time I had to write a single line of elisp to use Emacs (unless you count things like adding or removing a layer in my .spacemacs file as writing Lisp).


Really? Emacs is the opposite for me, I've used it for 15 years and really only recommend it to those for whom writing a little bit of emacs lisp sounds like fun. You write lisp for a living, sounds like in Emacs, so I assume you use paredit-mode. Don't you have to do little things like ensure that paredit-mode is turned on in your mode hook for clojure or common lisp or whatever?


I have changed the values of dotspacemacs-smartparens-strict-mode and dotspacemacs-smart-closing-parenthesis from the default nil to t in my .spacemacs file. As far as I can remember, that's the only paredit-ish customisations I have done. And by the way, I use smartparens everywhere, not just in Lisp modes.

Generally, the Spacemacs defaults work really well for me, and for many others. Elisp hacking was more relevant in the past. These days, with Spacemacs and other similar community efforts that deliver sane and well thought out settings, not much tinkering is required.


OK, I feel like I'm missing something with smartparens. I do the same as you -- but I use paredit everywhere, not just in lisp modes. When I've tried smartparens it seemed much less attractive (powerful?) than paredit.


I think this is a larger problem with Emacs. Duplicating a line is something that should be easy to do out of the box.

And if it's a situation where you really need to make the user define a keyboard shortcut, don't make them do C-c followed by the actual shortcut, let them define single keystroke shortcuts to do what they want. Multiple keystroke shortcuts are so damn annoying.


Why? I can't think of any time when I've wanted to duplicate a line.


Well, there are a lot of things I don't want to do, but that doesn't mean others don't want to do them. As these things go, it's a fairly common operation.



I have that on my list.


I don't hate or like lisp. It's just another programming language which does the job.


My emacs would have that feature in a few seconds if I thought it would be useful. But I don't happen to think it is so it (probably) doesn't.



Simple, given the Emacs defn of "simple" :-)


It is simple. What other editor lets you take anything from here:

https://www.emacswiki.org/emacs/CopyingWholeLines#toc3

and then just eval it to get new functionality? And the various implementations I linked to here are trivial to figure out and write yourself with even the most passing familiarity with Emacs Lisp. Even if you're a complete newcomer to the language, Emacs is self-documenting and can teach you everything from inside the editor.


C-S-backspace C-y C-y

That's simple by Emacs standards. :-)

If you need it simpler, write and save a keyboard macro.


Different editors have different patterns of usage. I find I roughly speaking never duplicate lines, so why would this be a problem?


I constantly find myself duplicate lines in vim (shift+y). You've never had to edit a line that's really similar to another?

Or when debugging, I don't want to change the old code, so I copy the line, comment it out, and work with the duplicated line.

You've never had to do that?


Well, everyone edits things in different ways.

The Emacs way of solving a workflow problem is instructing Emacs how to solve it for you. For instance, you mention:

1. Copy the line the cursor (point) is on;

2. Insert the copied line below/above point;

3. Commenting out the line you copied;

By the sound of things you do 1 and 2 with a command in Vim and the 3rd option with another command. In Emacs, I would simply program a function to do all 3 and bind it to a key.


The point is that "duplicating a line" is a very fundamental building block to other operations, and it should be supported natively. For instance, lets say you have to do several different function calls with only minor differences, you might write it out once and then duplicate a few times, then go in and edit them.

"Writing a function for the full change" is simply not a tenable thing to do. "Duplicate a line" is fundamental in the same way that "go to start of line" or "indent this line one more tabstop" is.

As someone who has spent years using both Emacs in "Emacs mode", and vim/evil-mode, there's no question to me that the modal vim-style editing is superior to Emacs's. That's not to say that "Emacs mode" is bad: it's far superior to most editing styles. Just not evil/vim :)

Don't get me wrong: I adore Emacs, and I use it for hours and hours every day. But if it weren't for evil-mode, I would go back to Vim in a heartbeat.


> "Writing a function for the full change" is simply not a tenable thing to do. "Duplicate a line" is fundamental in the same way that "go to start of line" or "indent this line one more tabstop" is.

So write a function for "duplicate line" and use it from now on. It's very easy to do. Just as it's easy to write function for a full edit.

A general algorithm to get one started:

1) Execute the edit you want to reuse.

2) Press C-h l, or M-x view-lossage, to see all the keypresses you made recently, along with elisp commands they executed.

3) You now know all the operations you did. Most of the time, you can recreate the edit by just wrapping them in parens, and then wrapping the whole block with (progn).

4) You're now ready to turn it into a function. Read up on 'defun and 'interactive forms. For best results, check out the documentation for each command you used during edit, via C-h f - some commands are meant for direct usage, and have a (faster, better) variant meant for use from within code; this is usually documented.

5) Tweak your function to make it more general along the dimensions you need, bind it to a key, and now you've just implemented what elsewhere would take a large plugin.


I love view-lossage. How often do You use C-x <esc> <esc> which shows elisp for what ya just did? Eg, switch buffer then try it.


Oh, I didn't even know about that! That's great!

Also, it reads straight from command-history variable (another thing I didn't know about). You can preview and browse it via C-h v command-history. Or, write your own elisp operating on it (perhaps interactively, by IELM - the elisp REPL).


You do it because it's so easy :)

I say that as a Vim user that duplicates lines constantly. Vim definitely changes your coding patterns; for better or worse because it can be soo frictionless.


millions of times. but with magit and a complete disregard for commit messages you can cure yourself of the crud-making habit in an afternoon!


If that's something you do a lot, you could write a ~3-line function and bind it to a key.


If you care about such things then don't use vanilla emacs. Use spacemacs instead where I suspect you'll have this out of the box. It's not an obscure port. Indeed in the long run it wouldn't surprise me if we end up with more spacemacs users than vanilla emacs. I've even run across some pages with emacs tips where they take it as a given that you're using spacemacs (the tips only work on spacemacs and the page never qualifies this).

And even with vanilla emacs it's probably a one liner in your config to get vi bindings.


I know this isn't as simple as what you're looking for, but this is what I go to macros for. Kill the line, paste it twice, move to the first one, comment it out, move down one.

Once you create a macro like that you can run it, name it, save it to your config, bind it to a key, whatever suits your workflow.

I never really thought of how much I do this manually, so I just set it up for myself right now. I'll probably rewrite it as an elisp function though.


Triple click the line, then middle click somewhere.

Or ctrl a ctrl k ctrl k ctrl y (move) ctrl y.

Is it really something that’s so common that it needs a command?


duplicate function declaration or code chunk; comment out old and modify new; leave old inline until you're happy with new and verified existing functionality isn't lost. Super useful working pattern I think that I use constantly. If it was more than `V p` Maybe i wouldn't work like that; but I do because it is so frictionless.

The more I use Git the more I've been committing/diffing for similar effect; but it's still much more overhead for smaller checks.


I find git a much better use case for that sort of thing, especially since it's about as many keystrokes.

If you've got a vim setup that works for you, great.


(list-packages) and install move-dup.el to gain four variations on that theme..




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: