Understanding Virtual Space

Menu:  Tools -> Options -> Text Editor -> All Languages -> General -> Settings
Versions:  2008,2010
Published:  2/28/2010
Code:  vstipEdit0023




Okay.  Virtual space is a little funky to understand if you aren't from the Old School.  You see, a loooooong time ago in this galaxy we used to have (and some people still have) editors that treat everywhere as editable space.  Let me explain:  Without virtual space on the line ends where the code ends.  Now you can have some really long lines of code but, until you press Enter, the line just keeps on going.  Like this:



If I move my cursor to the end of any of these lines and hit my Right Arrow key then it will go to the next line.  So, for the short line the cursor will jump down to the next line at column  44.  For the longer line it will jump down the next line at column 68.  And for the super duper long line it will jump to the next line at column 176.  This is the way editors have been for a while now and isn't really new to folks.


However, this way wasn't always the case.  There was a time when you could type anywhere you wanted any time you wanted without restriction.  Some text editors still do this today.  Virtual space allows you to go back to the old style of editing which is preferred by some coders.  Go to Tools -> Options -> Text Editor -> All Languages -> General -> Settings and check the "Enable virtual space" checkbox to turn this feature on.  When you do, you will be able to type anywhere on a line regardless of whether the code ends or not:


Comments (9)

  1. Leo Davidson says:

    I think the description is a bit misleading.

    So long as word wrap is off, code lines can continue for as long as you want until you hit return; the virtual space option doesn’t need to be on for that.

    When virtual space is off the text cursor is restricted to the areas where text exists but there’s no limit on the length of lines.

    When virtual space is on you can use the arrow keys, or mouse clicks, to position the cursor off the end of lines, past their carriage returns and then start typing in that "space".

  2. GregM says:

    Leo is correct, virtual space and word wrap are really not related, except that they are generally not used together because word wrap can make viritual space a bit odd.

    One of the main behaviors that virtual space changes is that it allows you to use the arrow keys to navigate up and down through the file while keeping your cursor in the same column, for doing things like aligning text.  If virtual space is off, moving the cursor to a different line will also move the cursor to a different column if the line you’re moving to is shorter than the column where the cursor is positioned.

  3. zainnab says:

    Good points Leo and Greg.  I’ll incorporate the info into the description to make it less misleading.  I did notice that you can’t have both virtual space and word wrap as the IDE won’t allow it.  I get the reason for word wrap but, to be honest, haven’t (or don’t) see a compelling reason for virtual space.  Does anyone use it?


  4. GregM says:

    Yes, I use it all the time, and have for many years.  A lot of the places where it is useful are the same kinds of places where column select, and the VS2010 enhancements to it, are useful.

  5. Wilson says:

    I use it regularly too (though since I recently switched back to VS from Notepad++ – which doesn't support it – I'm not as used to it as I once was, and I may end up turning it off – the end of an era).

    One thing that I wish hadn't changed with VS 2008 (probably 2005, actually) was the way the 'smart indent' feature worked. It used to be that when you had Virtual Spaces enabled, and you hit enter, the editor would put your cursor at the same column as the line you just edited … but it <i>wouldn't automatically add the blank spaces before your cursor location</i>. It would politely wait and automagically add them once you started typing.

    Now, it puts them in when it creates the new line and I end up with 'blank' lines filled with spaces all over the place. It's probably my own (mild, I hope) OCD talking, but it bothers me.

  6. gwideman says:

    @Wilson — Actually, Notepad++ DOES have virtual space behavior as an option. See "Notepad++: cursor past end of line / virtual spaces?" at Superuser dot com.

    Couple of other points.  Editors often have an option to get rid of superfluous whitespace at the ends of lines. This is handy whether or not you are using virtual space, but auto repairs any cases where you've inadvertently entered something in virtual space and changed your mind.

    Finally, I would not say that virtual space behavior is the "old" way of editing. It's in a number of current editors and IDEs.  It's just that there have been a number of editors that leave it out that have become popular with newcomers who've never known they had a choice :-).

  7. Paul says:

    To answer an earlier question: one potential use for this is for easily lining up code or comments in a tabular or multi-column format.  So if you had something like this:

    for (string str in strings) {                //This is the start of the outer loop

       for (int i in integers) {                  //This is the start of the inner loop

           Console.WriteLine(str + i);     //This is the body of the loop

       }                                                 //This is the end of the inner loop

    }                                                     //This is the end of the outer loop

    Whether this is your coding style or not, this is much easier to do if virtual space is turned on.

  8. George says:

    What's the benefit of not using "virtual space"? It confuses me when the cursor jumps when I move it up/down and I am loosing the track of the column it was before I started to move. And what do I get in exchange?

  9. Dirk says:

    Much better explanation than I got on MSDN.  Thanks!

Skip to main content