Visual Studio editing tricks

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Visual Studio editing tricks

Post by albinopapa » January 7th, 2020, 4:53 am

Figured I just keep a running list here to make things easier than just looking through the entire thread if it goes multiple pages:

Text editing keyboard shortcuts
Home: Move cursor to beginning of line of text, press again to go to far left of window
End: Move cursor to end of text and white space, tabs or spaces
Ctrl + C ( With something highlighted ): Copies highlighted text to clipboard
Ctrl + C ( With nothing highlighted ): Copy current line to clipboard
Ctrl + X ( With something highlighted ): Cuts highlighted text
Ctro + X ( With nothing highlighted ): Cuts current line
Ctrl + V ( With something highlighted ): Paste copied text from clipboard
Shift + Delete ( with something highlighted ): Cut highlighted text
Shift + Delete ( with nothing highlighted ): Cut entire line of text
Shift + Insert: Paste any copied text from clipboard
Alt + Shift + Up/Down: Multiple cursors for multi-line edits
Alt + Shift + Arrows: Allows you to select regions instead of entire lines
Alt + Left Click: Same as above for KB and Mouse interaction
Alt + Up/Down: Swap line of text with line above or below the line the cursor is on
Ctrl + Backspace/Delete: - Deletes word to the right of cursor to the next punctuation or beginning of next word
Ctrl + Up/Down: Scrolls the window up or down without moving the cursor

Navigation shortcuts
Ctrl + T: Opens a search box to find something in your project
Ctrl + Tab: Brings up a list of open documents, keep pressing tab while holding Ctrl to select a different document, release Ctrl to go to that document
F1: Opens a browser window to msdn. If you have something Win32 or C++ related ( c++ keywords or Win32 api functions or types ), "helpful" information will show up like a help document for what 'const' is or how to use IDXGISwapChain.

Debug keyboard shortcuts
F5: Runs the project in it's current mode ( Debug or Release ).
Shift + F5: Runs the project without being attached to VS ( no performance utilities running )
F9: Set break point on same line as cursor
F10: Steps over next line skipping function calls
F11: Step into next line, steps into functions or just steps over normal code

Outlining keyboard shortcuts
Ctrl+M,O: Starts/Enables outlining which collapses functions down to their declarations
Ctrl+M,P: Stops/Disables outlining which expands functions
Ctrl+M,L: If outlining is enabled, toggles outlining of all scopes

As text editors go, Visual Studio isn't the most flexible and is probably why most use VIM or something similar. However, there are a few nice features I'd like to share that I use regularly.

Multi-line edits: Hold down ALT and press the UP or DOWN arrows. This will give you multiple cursors.

Box highlighting: Hold down ALT and SHIFT while moving the cursor around. This allows you to cut/copy aligned blocks of code.

Multi-line paste duplicates: Copy a section of code like normal, then use ALT plus SHIFT plus the UP or DOWN arrow keys for however many copies you want, then press CTRL+V to paste. This will paste whatever text was in the clipboard on each line you have selected for multi-line edit.

Move lines of code, without copy/paste: Place the cursor on any line in the editor window, hold down the ALT while pressing the UP or DOWN arrows. If you press UP then the current line moves up one line or swaps with the line above. If you press DOWN then the current line is swapped with the line below. This is great for quickly reordering the lines of code without having to cut and paste.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
AleksiyCODE
Posts: 16
Joined: September 21st, 2019, 8:47 pm

Re: Visual Studio editing tricks

Post by AleksiyCODE » January 7th, 2020, 4:41 pm

Yea, i also use these a lot. But i prefere selecting rectangular regions with mouse (holding alt and selecting). Also didn't know about swaping lines of code with 'alt' - seems neat.
I like ass

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Visual Studio editing tricks

Post by albinopapa » January 7th, 2020, 9:36 pm

I use the mouse for selecting regions on occasion, but I almost always seem to forget to place my cursor before pressing ALT, so it takes me a couple of tries. The only reason I ever use it though is if I already have my hand on the mouse. Even selecting large regions, I find it just as easy to use the keyboard shortcuts. CTRL+SHIFT+END to highlight the rest of the line, then while still holding CTRL+SHIFT I just LEFT_ARROW back to where I want to be, then hold ALT then tap the DOWN_ARROW however many times to highlight the lines I need. Yeah, it's a bit more involved ( more steps ), but I don't have to take my hands off the keyboard to cut/copy then if I need to paste somewhere that is more than a few lines up or down from there I might use the mouse, otherwise I just continue using the keyboard.

Oh, another neat trick.
Scroll the screen up/down without moving cursor: Hold down CTRL then press the UP_ARROW/DOWN_ARROW keys. As long as the line the cursor is on stays on screen, the cursor will stay put otherwise the cursor will move depending on if you are scrolling up or down. I use this if I need to see some code further up on the screen that's just out of view and I don't want to move the cursor and lose intellisense or just flat out lose my place.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Slidy
Posts: 40
Joined: September 9th, 2017, 1:19 pm

Re: Visual Studio editing tricks

Post by Slidy » January 10th, 2020, 7:22 am

Couple more shortcuts I like:

Editing:
Ctrl + Left/Right - Moves cursor one word in that direction
Ctrl + Backspace/Delete - Deletes one word
Home - Goes to beginning of line
End - Goes to end of line

Code navigation:
Ctrl + T - Opens a textbox where you can type the name of anything you want to look for, e.g. filenames, type names, variable names and it'll show you the results. Very handy for traversing through a big project without having to dig through files.
Ctrl + Click - Takes you to the declaration or definition of a symbol (depending on which one you're currently at), or takes you to the file if its an include

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Visual Studio editing tricks

Post by albinopapa » January 10th, 2020, 9:22 am

The Ctrl+Click doesn't work for me since I turned that off, I prefer to have it highlight/select the word. I don't remember where that option is. ( probably in options somewhere )

Shift + Delete with nothing selected: Cuts the entire line the cursor is on.

I haven't used this because until just now I thought it just deleted the line, but turns out to Cut the line, I might be adding this one.

The Home/End and Ctrl + Left/Right I decided not to include because they are usually pretty standard across text editors. Definitely handy and I use them quite a bit along with holding down Shift to highlight words quickly.

Ctrl + Shift + Left/Right Highlight to beginning of next word or punctuation. Get's a little wonky when going over double quotes though and sometimes / or \\ can't remember. I believe it's the \\ as the cursor seems to skip over quite a bit if they are there in string literals.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Slidy
Posts: 40
Joined: September 9th, 2017, 1:19 pm

Re: Visual Studio editing tricks

Post by Slidy » January 11th, 2020, 1:49 am

Reminds me of another shortcut I use often:

Ctrl + X (with nothing highlighted/selected) - Cuts current line
Ctrl + C (with nothing highlighted/selected) - Copys current line

Whenever I want to duplicate a line multiple times I use that second one.

Something like this:
Ctrl + C
Ctrl + V (x5)
will copy the line and duplicate it 5 times

Slidy
Posts: 40
Joined: September 9th, 2017, 1:19 pm

Re: Visual Studio editing tricks

Post by Slidy » January 11th, 2020, 1:54 am

And a couple more code navigation shortcuts:

Ctrl + K, O - Switches between .h/.cpp
Ctrl + - - Goes "back" (could be to where your cursor was before in current file or to the previous file you were at)

User avatar
AleksiyCODE
Posts: 16
Joined: September 21st, 2019, 8:47 pm

Re: Visual Studio editing tricks

Post by AleksiyCODE » January 12th, 2020, 10:59 am

I also find Ctrl + M, O (for collapsing) and Ctrl + M, P (for stopping collapsing) quite useful.
By the way, I often accidentally put bookmarks (Ctrl + K, K). Does anyone actually use those for anything?
I like ass

albinopapa
Posts: 3982
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Visual Studio editing tricks

Post by albinopapa » January 12th, 2020, 4:17 pm

AleksiyCODE wrote:
January 12th, 2020, 10:59 am
I also find Ctrl + M, O (for collapsing) and Ctrl + M, P (for stopping collapsing) quite useful.
By the way, I often accidentally put bookmarks (Ctrl + K, K). Does anyone actually use those for anything?
If you are referring to the bookmarks, I do not.

I do however use two of the outlining shortcuts. Ctrl + M,O to collapse scopes and functions and Ctrl+M,L for toggling outlining; collapsing/expanding all scopes, functions, classes, namespaces and #pragma regions. I forgot about the Ctrl+M,P. Just found out that if you do Ctrl+M,P the Ctrl+M,L is not available until you press Ctrl+M,O.

Thanks for sharing, almost forgot the Outlining shortcuts.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Slidy
Posts: 40
Joined: September 9th, 2017, 1:19 pm

Re: Visual Studio editing tricks

Post by Slidy » January 13th, 2020, 1:44 pm

What does outlining do exactly? Just tried it out and looks to me just like un-collapsing everything (same as Ctrl+M,P does).

Post Reply