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!!!!!!!!!!!!

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?

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