Did you know… You can Right-drag code to do a Move or a Copy? – #170


Today’s tip was submitted by a blog reader.  I had no clue that you could do this. 

Select a line of code, and then right-drag that line to anywhere within your editor (or into another editor window).  Then you’ll get this little menu popup with the options of "Move here, Copy here, Cancel." 

RighClickDrag

I love little tricks like these, especially when i never knew it existed.  We should play a game called "Stump the Sara" and send me your most obscure IDE tricks.  Since i only worked on VS Core, it can only be generic IDE features not tied to any specific language.

Technorati Tags: VS2005Tip,VS2008Tip

Comments (19)

  1. VS Enthusiast says:

    It’s always nice to learn something new.  I always use cut-and-paste, but drag-and-drop seems just a  bit more efficient.

    Thanks for the tip!

  2. ccatto says:

    Hey Now Sara,

    This one is realy neat, I never knew about this one. It’s quite useful too.

    Also I saw a post recently about your sessions on CodePlex & Tip of the day. Hope they go or have gone well. CodePlex is a really great site, I’ve been using it more & more lately.

    as always thx 4 the info,

    Catto

  3. Mark says:

    This tip works for almost any windows application as far as I know.

  4. Jimbo says:

    Nope…didn’t know that one either…although it makes sense.  It’s the same behaviour as when you drag an icon with right button…only contextually correct.  One thing I love about Windows…it’s consistent if nothing else.  That and the ability to do anything four different ways!! 🙂

  5. JMD says:

    Maybe this is too elementary, but the same dragging operation can be done without incurring the usability "penalty" of dealing with a redundant context menu.  *Left*-dragging a selection of code performs a "move" — unless you hold the Ctrl button while dragging, in which case the operation changes to a "copy".  A [+] is added to the the mouse pointer to indicate such.

    Dragging to copy a selection actually works between disparate editors in many cases.  E.g. I can copy code from the IDE over into this comment editor in Firefox:

    using System;

    using System.Collections.Generic;

    using System.Management;

  6. Justin says:

    Normally I don’t use the drag drop except to avoid over writing what is in the clipboard. (Don’t like taking hands off the keyboard to go to the mouse… It’s SOOO time consuming! The clipboard benefit is the point that I find really useful! Using the left button (with control for copy) is really neat!

    "Stump the Sara": Hope your not running out of ideas 😉

  7. Shtong says:

    I didn’t know that right click one but I’ve been using the ctrl-drag (with left button) to copy and just drag to move for a long time, it’s a great shortcut for copy-pasting !

    For quick replacement of several single words like a variable name I also like the fact that ctrl-click selects an entire word, so if I want to replace every occurence of a var name in a function, I hold the ctrl key down, and do a click-and-v on every var name I want to replace. It’s pretty fast once you get used to it.

  8. Thanks for reminding me of this feature…I’ll start using it again immediately!  Also, works for Visual Studio 2005.

  9. Bruno Brant says:

    JDM beat me to it. You can use CTRL while dragging to avoid displaying the context menu.

    A tip: This doesn’t use the clipboard, so, if you need to copy some text but doesn’t want to lose whatever you have in the clipboard, you can drag-copy.

  10. Kyralessa says:

    Oogh.  A lot of the programmers I know would be better off if copying code were made harder, not easier.  🙂

  11. Søren Pedersen says:

    Many text-editing "places" in Windows does this, and you can even do it with items in folders or on the desktop.

    I discovered this one myself by accident; a book fell on my mouse’s right button and caused it to slide slightly, revealing this little popup menu…!

  12. e34m5 says:

    Is there a way to highlight multiple lines of code and then indenting or unindent all at the same time.

  13. Rwg says:

    Due to the simplicity of the answer I’m not sure if this is what you wanted to hear?

    1) Select multiple lines of text with the usual mouse drag, or the keyboard using SHIFT+up or down arrow keys

    2) Press TAB to indent or shift+TAB to unindent the selection

    Incidentally, you can also select a block (square) region of text in Visual studio or Word by holding the ALT key down then dragging over the area with the mouse.

  14. Anthony Wykle says:

    I believe that with a little research one would find that both the <ctrl> / <alt> – left drag and the right drag tricks are actually a part of the windows common controls edit box functionality. I don’t know about the right drag, but I know the modified left drag tricks have been a part of the standard windows UI since at least Win95.

    Side note: the right-drag trick doesn’t work in the textarea implementation provided by MSIE 8.0. Maybe Microsoft should consider making the right-drag standard if it isn’t?

  15. I’m inclined to think this is OS level functionality rather than VS or Word specific – I’ve tried it successfully on third party apps.

    Does that nullify it from the Stump Sara game since it’s below VS core?

    Also:

    Clicking and holding the left mouse button for a second or two activates the same feature.

    And thanks for the ALT+click for selecting a rectangular area – that’s nice!  I’m sure to find that useful in SQL Server, my bread and butter.

  16. Cp6uja says:

    WOW! That’s what i call a genious feature!!

    This blowed my socks off my feet!

    You stupid americans…

  17. joe brownie says:

    iam trying to hack on warrock (a computer game) but i cant find out how

  18. feiyim says:

    hey sara,

    thats a nice one you got for us.i knew it existed but never gave a damn about it nor knew its efficacy.Now that you have alerted me and its preety good, am gonna try it.thanks

  19. Matthew says:

    Here’s a fewthat I use every day…

    These are chord combinations, so hold the ctrl key down for both…

    CTRL+K, C – applies comments to a selected block of code.

    CTRL+K, U – remove (un-comments) a selected block of code.

    CTRL+K, K – toggles a bookmark for the line the cursor is on.

    CTRL+K, N – moves the cursor to the next bookmark, I cannot remember what moves to a previous bookmark right now.

    CTRL+K, S – brings up the "surround with" list for the selected block of code.  It’s useful for selecting a block of code and putting it inside of a try/catch block, or conditional block, etc.

    — Matthew