6 ms·
Emacs - the editor that STILL doesn't have a simple way to duplicate the current line?
by commandlinefan 7y ago
Emacs - the editor that STILL doesn't have a simple way to duplicate the current line?
- kcbanner 7y agohttps://www.emacswiki.org/emacs/CopyingWholeLines https://www.emacswiki.org/emacs/CopyingWholeLines
- mwexler 7y agoSimple, given the Emacs defn of "simple" :-)
- TeMPOraL 7y agoIt is simple. What other editor lets you take anything from here: https://www.emacswiki.org/emacs/CopyingWholeLines#toc3 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.
- jordigh 7y agoC-S-backspace C-y C-y That's simple by Emacs standards. :-) If you need it simpler, write and save a keyboard macro.
- yxhuvud 7y agoDifferent editors have different patterns of usage. I find I roughly speaking never duplicate lines, so why would this be a problem?
- cold_fact 7y agoI 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?
- mickeyp 7y agoWell, 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.
- OskarS 7y agoThe 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.
- TeMPOraL 7y ago> "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.
- simtel20 7y agoI 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
- balnaphone 7y agoFor 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.
- benreesman 7y agoI 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.
- bachmeier 7y agoI 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.
- jdormit 7y agohttps://github.com/emacs-evil/evil https://github.com/emacs-evil/evil ;)
- bachmeier 7y agoI have that on my list.
- globular-toast 7y agoWhy? I can't think of any time when I've wanted to duplicate a line.
- bachmeier 7y agoWell, 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.
- dmortin 7y agoI don't hate or like lisp. It's just another programming language which does the job.
- alexhutcheson 7y agoIf that's something you do a lot, you could write a ~3-line function and bind it to a key.
- wiredfool 7y agoTriple 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?
- w0m 7y agoduplicate 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.
- wiredfool 7y agoI 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.
- globular-toast 7y agoMy 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.
- BeetleB 7y agoIf 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.
- michael-ax 7y ago(list-packages) and install move-dup.el to gain four variations on that theme..
- beizhia 7y agoI 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.