简体   繁体   中英

Visual studio CTRL+SHIFT+T transpose - what does it do?

I wrote some code and tried the Ctrl + T to check transpose feature in visual studio.

Just to check if CTRL + Shift + T does the reverse for this Transpose... I tried pressing Ctrl + Shift + T . and it just messed up everything...

Can anyone tell me what exactly this Ctrl + Shift + T does (especially with a block) ?

For instance:

public string returnDateTimeToMyformat(DateTime dt)
{
    dt = dt.AddYears(-1);
    return dt.ToString("yyyy MM dd HH mm ss");
}

To:

string returnDateTimeToMyformat publicdtDateTime (dt
{
    dt = )1AddYears(-.return;
    dt ).ToString("yyyy MM dd HH mm ss");
}

(I started with my cursor right after 'public')

Since CTRL-T swaps the two characters on either side of the cursor, the opposite of it is ...

wait for it ...

CTRL-T

:-)

CTRL SHIFT T transposes the two words after the cursor.

What it's doing to your block seems rather bizarre. It appears to doing it to multiple parts of each line. My only advice would be (as the doctor said to the patient who complained it hurts when banging their head against a wall): Don't do that.

As others have pointed out, the two words following the cursor are transposed, and the cursor is placed after the words that have been transposed. However, Visual Studio 2010 at least appears to ignore commas and other punctuation when considering "words." One utility of this, then, is that you can reorder something like an enum. For instance,

typedef enum myEnum
{
  ThingOne,
  ThingThree,
  ThingTwo
};

Put the cursor somewhere near ThingThree and press Ctrl Shift T to get:

typedef enum myEnum
{
  ThingOne,
  ThingTwo,
  ThingThree
};

This could be a good thing if you decide that a different order for your enums is better. You can also use this to help idiot-proof comparisons and/or quickly and easily format them to a better coding standard.

if ( ptr == NULL ) { /* stuff */ }

is considered bad (never mind that having an "if" on its own line is also bad) since you could easily write (or read) "ptr = NULL" by accident. You're better off with

if ( NULL == ptr ) { /* stuff */ }

So, if you did it wrong the first time, just select the offending expression and... Ctrl Shift T to the rescue!

...Yeah, okay, so this thing isn't that useful.

Edit: Hmm, I should add that the behavior is a little weirder when your cursor is placed immediately before a punctuation symbol (such as a left-parenthesis), hence the weird result you got when you repeatedly hit Ctrl Shift T on your code snippet. It seems to just swap any whitespace-terminated string after the cursor with the next alphanumeric "word," skipping over any punctuation symbols in between. The result is often difficult to read, though, so I'm not going to claim that's the exact pattern.

According to this website:

Transposes the two words that follow the cursor. (For example, |End Sub would be changed to read Sub End|.)

The only question that remains is probably: WHY?? Well it might become handy when you have a block of code lines where variables are assigned values. (For example Load/Save) In the opposite function, you want to do the opposite assignment, maybe this shortcut can be used in such a situation...

With this Visual Studio Document Reopen cool extension CTRL+SHIFT+T you can reopen the last closed document(s). It works like in Web browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM