Joe Webb Blog

Musing and observations about SQL Server, other technogies, and sometimes just life in general

Copying and pasting rectangular blocks of code in Visual Studio and Management Studio

One of the things I really like about teaching technical classes is getting to know and talk with students, learning of the challenges they face and how they've addressed them. Frequently, I can learn from their experiences, too. So, it works out well for both student and instructor.

In a custom Visual Studio/SQL Server class last week, I made a passing comment about Visual Studio being a very good text editor, but that it lacked one feature that I really like in other editors - block copy and paste. I lamented that the Visual Studio editor doesn't allow you to select a rectangular area of code and then copy and paste it elsewhere in the document. Other editors that I've used over the years - emacs, vim, Notepad++, UltraEdit - can do that. I would have thought that Microsoft would have put that in Visual Studio.

It turns out that they did. One of my students showed me an old Microsoft Word trick. You can depress the Alt key while using the left mouse button to select rectangular areas of code. Pressing the Alt key tells the editor not to select the entire line, but to follow the mouse pointer wherever it goes. Then you can use normal techniques to copy the text and paste it wherever you'd like.

blockcopy20071106

Why is this useful? Consider declaring a bunch of variables as show above. At a later point in your coding, you need to set the value of all of these variable. By copying the block you can quickly and easily paste only the relevant text (the variable names) into the procedure like so.

blockcopyb20071106

Pretty nifty! Thanks, Ellen, for the tip!

By the way, this technique works in the SQL Server Management Studio editor, too.

blockcopyc20071106

Cheers!

Joe

kick it on DotNetKicks.com

Legacy Comments


Mladen
2007-11-06
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
very sweet!!
wasn't there a keyboard short cut in an old visual studio that would enable/disable that kind od selection?

Casual reader
2007-11-06
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
From the Visual Studio Help:
============================
To select a rectangle of text
Hold down the ALT key, press down on the left-click button of your mouse, and then drag the cursor over the text to select.

—or—

Hold down the SHIFT+ALT keys and press any Arrow key.

Joe Webb
2007-11-08
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
Hey - thanks for the documentation reference.

You know, now that I know that VS has the capability, it doesn't really surprise me that it is documented in help. But I, along with every other student in the class, was unaware that VS had that capability. That's why I thought I'd mentioned it here.

Cheers!

Jesse Gavin
2007-11-08
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
That just changed my life. Thank you!

Roger Dickey
2007-11-08
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
That's right, I forgot about that.

That would have come in handy the other day.

Thanks for the reminder!

Lucio
2007-11-08
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
In VI: Ctrl-Q :)

Lucio
2007-11-08
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
In VI: Ctrl-Q :)

tokioBelle
2007-11-11
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
For the benefit of this blog's owner, and all those who strayed here from DotNetKicks and seem to stare in awe, Sara Ford of Microsoft has all sorts of Visual Studio tricks in her weblog:

http://blogs.msdn.com/saraford/



Great!
2009-05-13
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
wooo hooooooo!!!!

No more emacs for me!!!!!!!!!!!!

columbia jackets
2010-10-15
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
By the way, this technique works in the SQL Server Management Studio editor, too.

snow boots for women
2010-10-15
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
By copying the block you can quickly and easily paste only the relevant text (the variable names) into the procedure like so.

fur boots
2010-10-19
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
Pressing the Alt key tells the editor not to select the entire line, but to follow the mouse pointer wherever it goes. Then you can use normal techniques to copy the text and paste it wherever you'd like.

furry boots | womens snow boots | columbia outlet | columbia sportswear jackets | the north face jackets | north face jacket | cheap mac makeup | discount makeup

hanly
2010-10-26
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
This super video converter for mac is developed by Emicsoft Studio, it is currently the best video converter running under Mac os x, comparied by isqunite, Visualhub and other Video Converter for Mac Free vide under "format" option choose the output video formats by your needs, such as to mp4 for ipod, iphone, zune, psp, creative zen, flv, app

electriac
2011-06-25
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
Many TNX. One of my main reasons for retaining XP is so that I could use EMACs for block copy and paste. Now I have one less thing I have to keep XP around for.

electriac
2011-06-25
re: Delete a line
One other feature I wish I had that was present in most older text editors like NYedit was a single key command to "Delete A Line". Is there something like that in Vis2008 that I am missing?

Ryan S.
2012-02-17
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
Visual Studio is the program I use the most when editing the Indianapolis Jiu Jitsu website I work on. I have a fairly basic understanding of it and am learning more techniques every day. Copying and pasting these code blocks is a great time saver and is one technique I love! I also wish there was a single key command to delete an entire line of code. This would help speed up the website editing process for sure.

Nuno Monteiro
2012-10-10
re: Copying and pasting rectangular blocks of code in Visual Studio and Management Studio
Great!
Thanks for sharing this.