How Visual C# Helps You Code—Edit - Bookmarks
| CSharp-Online.NET:Articles |
| Visual Studio Articles |
| © 2006 Peter Wright |
Edit -> Bookmarks
If you’re working on a particularly large class, it’s sometimes handy to be able to quickly jump to and from specific places in the code. For example, if I have a bunch of instance variables declared at the top of the class, I find it handy to be able to quickly jump to them to refresh my memory as to what certain ones are named before I use them in code.
C# Express includes a bunch of menu options on the Edit -> Bookmarks menu, as well as shortcut keys to set and navigate around bookmarks in your code. You can see the menu in Figure 7-26.

Figure 7-26. The Bookmarks submenu lets you set and navigate around bookmarks in your code, and shows you the handy shortcut keys to use them.
| DOUBLE KEY SHORTCUTS |
| These shortcuts are a little strange, aren’t they. Ctrl+K then Ctrl+C to comment stuff, Ctrl+K twice to set a bookmark. The reason is that all the code-editing hot keys begin with Ctrl+K. So, you use Ctrl+K to go into an advanced editing mode, then Ctrl+C for comment code ("C" for comment), Ctrl+K again to set a bookmark
(bookmark ends in "K"), and so on. You soon get used to it—like anything, the more time you spend in the editor, the quicker things like this become second nature. |
| Try It Out: Working with Bookmarks |
Setting a bookmark is easy. Just click on, or move the cursor to, the line of code that you want to bookmark and then press Ctrl+K twice. You’ll see a small icon appear beside the line of code, as in Figure 7-27.

Figure 7-27. Setting a bookmark on a line of code causes a small icon to appear beside the
bookmarked line.
Go ahead and set a few more bookmarks into the code; don’t worry about where too much.
To move around the bookmarks, just use Ctrl+K, Ctrl+P to go to the previous one (remember, Ctrl+K to go into an advanced editing mode, then P for previous), and Ctrl+K, Ctrl+N to go to the next one. As always, you can get at these through the Edit -> Bookmarks menu as well.
You can clear out a single bookmark simply by moving to it and pressing Ctrl+K and then Ctrl+K again. Alternatively, you can clear out all the bookmarks in a file by using Ctrl+K, Ctrl+L. Do that now to clear out the bookmarks you set.
Now, let’s take a quick look at how you would use this stuff for real when working on code.
Move to the top of the MainForm source in the Movie collection sample and bookmark the namespace line, as in Figure 7-28.

Figure 7-28. Bookmark the namespace line.
You can pretend that this marks the point at which some really important instance variables are declared.
Now move down to the very end of the source code and you’ll find a method called ShowSearchOnlinePart(). Put the cursor somewhere in that method.
Imagine that you were working on this code and needed to glance back at the instance variables you bookmarked (pretended to) earlier in the source. All you have to do is press Ctrl+K, Ctrl+K to remember where you are (do it now), and then Ctrl+K, Ctrl+P to go back to the previous bookmark (go ahead and try it). When you’re finished, just hit Ctrl+K, Ctrl+N to return to the next bookmark, and then Ctrl+K, Ctrl+K clear the bookmark out.
I know it all sounds terribly awkward, but it really isn’t, and once you get used to it, bookmarks are a great way to navigate around complex source files.
Of course, you’ll also want to use bookmarks across more than one file. Perhaps you want a bookmark set in one class, and the ability to jump to it while you work on another. The Previous Bookmark in Folder, and Next Bookmark in Folder items on the Edit -> Bookmarks menu let you do just that.
Try it out. Set a bookmark somewhere in the code for MainForm, and then take a look at ListDetails.cs in the MovieCollection1 application (just click it once in the Solution Explorer and then press F7 to view the code).
Now press the rather ornate shortcut Ctrl+Shift+K followed by Ctrl+Shift+P. Notice how you instantly jump out of the ListDetails code and back into the MainForm code. Of course, to get back to the ListDetails code,
you’ll need to set a bookmark before you jump.
|

