Monday, October 26, 2009

Hack 20. Navigate Your Source Code











 < Day Day Up > 





Hack 20. Navigate Your Source Code





Visual Studio lets you go way beyond moving

around with the arrow, Page Up, and Page Down keys. Learn some

keyboard tricks that let you move around in different

ways
.







Visual Studio provides a

number of ways to navigate through your source code. This hack will

cover how to use a number of helpful shortcuts as well as how to use

bookmarks to quickly move around in your code.







3.6.1. Navigation Shortcuts





The normal method of moving around in your

source code with the mouse is perfectly acceptable, but it is also

slow. When you are busy typing, it takes time to reach for the mouse,

orient the cursor, and then move to another section of code by

clicking on a line. Visual Studio contains a number of navigational

shortcuts that can keep your hands on the keyboard and help you write

code faster.







3.6.1.1 Navigation history




The first two commands that you can use to navigate are the

View.NavigateBackward (Ctrl and

-)

and

View. NavigateForward (Ctrl-Shift

and -) commands. These keystrokes can best be described as undo and

redo commands, but instead of undoing or redoing actions, these

keystrokes undo and redo navigation. For instance, if you are working

in one method, then use the mouse to move over to another method, you

can jump back to the previous method using the NavigateBackward

keystroke. After changing something in that method, you could then

jump forward again using the NavigationForward keystroke. These

shortcuts also work fine across files and projects. If you are

working in one file and switch to another, you can quickly jump back

using the NavigateBackward shortcut keystroke.











3.6.1.2 Mouseless scrolling




Usually

when you want to scroll, you end up

reaching for the mouse. The nice thing about scrolling is that it

does not move the cursor, whereas using the regular up and down

arrows does move the cursor. Thankfully, Visual Studio includes

commands to scroll the file without moving the cursor. The

Edit.ScrollLineUp and

Edit.ScrollLineDown commands can be accessed by simply

pressing Ctrl while also pressing either the up or down arrow.











3.6.1.3 Go-to brace




Visual Studio includes a number of

commands that will jump to the next occurrence of a code element.

Possibly the most useful is Edit.GoToBrace

(Ctrl-]),

which will jump to the corresponding brace, parenthesis, or bracket

in the document. If the cursor is on a closing brace, it will jump

back to the opening brace; if the cursor is on the opening brace, it

will jump to the closing brace. This keystroke will work only when

the cursor is on a line that includes a brace and only with languages

that use braces (C#, C++, etc.).











3.6.1.4 End and Home




Visual Studio also supports the End and Home

keys through the commands Edit.LineEnd and

Edit.LineStart . Pressing the End or Home key will move

you to the end of the current line or the start of the current line,

respectively. Visual Studio also includes two commands called







Edit.DocumentEnd and

Edit.DocumentStart (Ctrl-End and Ctrl-Home), which will move you to

the end or beginning of the current document.











3.6.2. Bookmarks







Bookmarks

are one of the few computing analogies that make perfect sense.

Bookmarks allow you to mark a place in your code that you want to

come back to, just like a regular bookmark you would use in a paper

book. Creating and using bookmarks is incredibly easy and when used

properly can save time that would otherwise be wasted scrolling and

searching with the mouse.





You can set a bookmark on a specific line in a file using the command



Edit.ToggleBookmark (the

keystroke chord [Hack #24] Ctrl-K,

Ctrl-K). After setting a bookmark, you will see a small blue

indicator next to that line where a breakpoint would normally appear.

You can remove the bookmark by simply using the Edit.ToggleBookmark

command again. You can also quickly move between bookmarks using two

other commands, Edit.NextBookmark (Ctrl-K,

Ctrl-N) and Edit.PreviousBookmark (Ctrl-K,

Ctrl-P) . Using these two commands,

you can quickly jump through all of your bookmarks much faster than

manually searching for that last line you were on using the mouse.





Bookmarks are removed when you close a file, but if you leave files

open and close the solution, they are saved in your user options file

[Hack #4] and will still be

there next time you open the solution. You can also manually clear

all bookmarks by calling the Edit.ClearBookmarks (Ctrl-K,

Ctrl-L) command.





Bookmarks can also be used with the mouse through the Edit

Bookmarks menu.



















     < Day Day Up > 



    No comments: