Visual Studio: How to insert tab instead of code snippet - visual-studio

Whenever I hit the key after a keyword that comes from the list of code snippets, Visual Studio always inserts the code snippet.
However, this is very disturbing when I want to add one-line comments to existing code. I can't, for example, add a TAB character after the else keyword.
What keyboard combination do I have to press in order to get a TAB character inserted here?
Your help is appreciated.

A simple workaround for your else scenario is to insert a space and then the tab character, then remove the space.
If you want to make changes to snippets, you can do this by going to Tools -> Code Snippets Manager (Ctrl-K, Ctrl-B) and select the snippet you wish to modify. The path to the .snippet file will be shown. You can just open the file in a text editor and modify it to do the action you want.

Related

Change variable name in multiple lines in visual studio

So, I have some code for a Rect variable mageSection:
And I'd like to copy the same code for a different variable warriorSection. To achieve this:
So I'd like to know if there is a visual studio shortcut for allow you to change variable name in multiple line in visual studio without refactoring the whole variable name.i.e. I don't have to manually change those names from mageSection to warriorSectionfor these 4 lines.
It is a question about shortcut in visual studio rather than writing functions, as I'd like to know the shortcut for this. Many thanks!
Copy and paste the section of code.
Now select the pasted code and hit Ctrl-H to bring up the Quick Replace dialog.
In the top box, type "mageSection".
In the bottom box, type "warriorSection".
Hit Enter and it will find the first occurrence and replace it.
Hit Enter three more times...done.
If I'm understanding your question correctly you should be able to hold down alt while clicking into multiple lines and change them all at once. Then hit ESC to exit multiline editing.

Does Visual Studio or Resharper have an auto-completion overwrite feature similar to Eclipse?

Sometimes I'll have classes/methods/variables with similar naming. For example, GetABar() and GetAFooWithABar() (assuming for the sake of example that these aren't horrible names to begin with).
I get into situations where I want to change one of the calls from the former to the latter, and when I start typing in the middle of the name, I'll get an auto-complete suggestion. If I select the suggestion, the following results:
GetAFooWithABarABar()
In Eclipse, there's a handy feature called completion overwrite which you can set as a default, or select on the fly by holding down Ctrl when you select the suggested term. Does Visual Studio or Resharper have a similar feature?
In ReSharper, you get a different result if you complete with Enter or Tab. If you hit Enter, it will insert the text, as you describe above. But if you hit Tab, it will replace the text to the right of the text caret, and should give you the result you're after.

Visual Studio 2013 custom toolbar to insert code snippets

Could anyone point me in the right direction here? I am adding a custom toolbar and I want buttons for lots of different code snippets. We use so many that it is impracticable to remember all the different shortcut commands for each one, and selecting from a menu takes longer than just clicking a button.
It doesn't really even need to be specifically for a code snippet as defined by Visual studio, just simply pasting some text to the editor window, however I was hoping to be able to have some that would work as surround snippets, which the normal code snippets do.
You should call this:
(DTE2)base.GetService(typeof(DTE))
in the Initialize() method of the Package and save the resulting dte-instance in a field. When your button is clicked, you can use ((TextDocument)_DTE.ActiveDocument).Selection.Text) to retrieve the currently selected text, modify it and write it back as new selected text. Maybe you will have to cast the return value of the Selection-Property to TextSelection.

Visual Studio 2010 Code snippet

i have managed to make a code snippet and its all good, the question is i dont want to use ctrl+k ctrl +S then click on the snippet , instead i want to use a combination of keys to just use the snippet right away , or at least pressing ctrl+k ctrl + S then another combination ?
is that possible ?
thanks in advance...
If you say you're pressing Ctrl+K, Ctrl+S is because the snippet you added was a Surround With snippet, isn't it?
I suggest you installing ReSharper for this purpose. You can easily add all kind of snippets (called templates in ReSharper), and of course, Surround-With snippets.
After adding a template, you can give it the number you want.
Then all you have to do is press: Ctrl+Alt+J and you will see the following:
And all you have to do is press the number of the snippet you want.
For example, if you want to surround some code with an if, you press: Ctrl+Alt+J, 1.
Note:
In this moment, I don't really remember if Ctrl+Alt+J is the keyboard shortcut by default, because I have changed many of them, but you can check it in ReSharper > Edit > Surround With Template...
Easier than typing a key-combination is just typing the name+[Tab]+[Tab]. Visual Studio is pretty good at remembering the last ones used to make it even easier.
"f+[tab]+[tab]" creates a for loop snippet.
In your code snippet file, you can set up a shortcut element. Your code snippets are located under(depending on language):
"\Documents\Visual Studio 2010\Code
Snippets\Visual C#\My Code Snippets"
Under the CodeSnippet\Header\Shortcut section in the code snippet file, type a unique short relevant series of characters.
<CodeSnippet>
<Header>
<Shortcut>spi</Shortcut>
</Header>
Typing these characters followed by tab in the code window will insert your code snippet.

Can you set Visual Studio's "smart indent" to not remove tabs in blank lines?

When Visual Studio (2005) has Options -> Text Editor -> C/C++ -> Tabs -> Indenting set to Smart it will automatically indent code blocks and line up squiggly brackets, {}, as expected. However, if you hit enter inside a code block, move the cursor to another line, and then move it back, the inserted tabs are gone and the cursor is positioned all the way to the left. Is there a way to set Visual Studio to keep these tabs?
As far as I know, the only way to do that is to enter something (anything) on that line, then delete it. Or hit space and you'll never see it there until you return to that line.
Once VS determines that you've edited a line of text, it won't automatically modify it for you (at least, not in that way that you've described).
This is an annoyance to myself as well. Anytime the code is reformatted the blank lines are de-tabbed.
You might look at this: http://visualstudiogallery.msdn.microsoft.com/ac4d4d6b-b017-4a42-8f72-55f0ffe850d7 it's not exactly a solution but a step in the right direction

Resources