Duplicate Line

By Deane Barker on March 13, 2006

Here’s something that I have in EditPlus, but I haven’t found in other editors: a “duplicate line” shortcut.

In EditPlus, you can have your cursor in a line, hit CTRL-J, and the line will be copied immediately underneath the current line. Hit CTRL-J four or five times and you get four or five identical lines.

This is really handy — more than you’d think. For instance, tonight when adding “Import” statements at the top of a .Net Web form in Visual Studio, I had to manually copy the last line, then paste it in. Tedious.

There are lots of instances in programming where you have multiple, consecutive lines that differ very slightly, so a quick way to copy and paste really saves time. Think of table rows in HTML, for example.

So why isn’t “Duplicate Line” found in more editors? When I work in any editor besides EditPlus, this is the command I miss right away and get frustrated when I can’t find a replacement for it.

Gadgetopia
What This Links To

Comments

  1. I’ve always loved Vi for its handling of invaluable shortcuts like this. Duplicate line in Vi: ‘yyp’.

  2. i’m a vi fan also.

    that being said – i’m leary of features that facilitate copying and pasting. i realize that for html development it’s useful, but i’ve slogged my way through enough unfactored code to think that maybe pasting is something we should prohibit editors from doing all together.

    fortunately, vi also has good global search and replace facilities.

  3. Alex beat me to the ‘yyp’. In vi, you can also say something like ‘yy 3p’ (copy line and paste it 3 times) or ‘4y 3p’ (copy the next 4 lines, then paste them 3 times)

  4. I’m pretty sure you can do it in VS.NET with Resharper, though I don’t remember the actual shortcut key (as the duplicate line isn’t a feature I personally use). If you’re not using Resharper… you’re missing out! – it can do a helluva lot more than just duplicate lines ;-)

  5. I guess most of the editors have similar stuff… Don’t they?

    I do remember my very old MSDOS days with TDE.

    And for anybody using Windows nowadays, Duplicate line is: Ctrl-C Ctrl-V

    Yup!!

  6. Ok, here’s a free one for you from another EditPlus user, this works in VisualStudio 2005 (8.0) and probably 7.1 and 7.0:

    Sub DuplicateLine()
        Dim line As String
        DTE.ActiveDocument.Selection.StartOfLine(0)
        DTE.ActiveDocument.Selection.EndOfLine(True)
        line = DTE.ActiveDocument.Selection.Text
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.StartOfLine(0)
        DTE.ActiveDocument.Selection.Text = line
    End Sub
    

    If you assign the macro to CTRL-j it works just like EditPlus except a little bit slower, but then again, everything is slower in VS2005.

    All you have to do is paste it in one of the existing modules in the Macro IDE. To assign to a key, searxh for “Duplicate” in the keyboard customizing dialog.

    -Mike

  7. Thank Mike!!! you rule;

    if you want to prefect your macro, you need to add the event were some text is already selected, and copy this selected text only!

    I used to work in Zend (PHP) and this feature was one of my favourites

    Noam

  8. Wow Mike Thank you so much, I just found your macro today and it worked perfectly. I am also used to Control+D from Notepad++ and it is a petty that duplicate line doesnt exist in VS, but now its all good! ;)

    Many thanks!

  9. Note that some of these cdomanms may not work if you are accessing bash through a telnet/ssh session, or depending on how you have your keys mapped.

Comments are closed. If you have something you really want to say, email editors@gadgetopia.com and we‘ll get it added for you.