Using code snippets in Visual Studio

人盡茶涼 提交于 2020-01-13 09:13:11

问题


Right now when I want to use the cw snippet (Code snippet for Console.WriteLn). I am typing cw, Tab, Tab.

Is this the correct (fastest!) way of doing it? If there wouldd be some way of only having to hit the tab key once or any other key I would be glad to know it.


回答1:


That is the fastest way. Note that the first tab is just to get rid of the IntelliSense tooltip. The second tab is the one that actually does the work. In other words, you can do cw + esc/tab/enter + tab or even c + esc + w + tab. Or you can even type cw, go somewhere else, click right after the cw then tab and it will auto complete.

That could be annoying if you do something like var cw tab.

Edit: Once you have used "cw", you can then do it a bit faster by just typing c + tab + tab. Since you last used "cw", the c will select "cw" from IntelliSense, the first tab will insert it, and the second tab will auto complete.




回答2:


AFAIK there's no faster way of doing this (but there's a slower one :) ):

http://msdn.microsoft.com/en-us/library/z4c5cc9b(VS.80).aspx




回答3:


With ReSharper it is cw,tab




回答4:


Like Nelson mentioned the 1st tab is really just an escape action in this case. But I do know a way to cut down 1 keystroke. Save the following as "c.snippet" and drop it in your "..\< myDocs >\< VS20XX >\Code Snippets\Visual C#\My Code Snippets" directory:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>c</Title>
            <Shortcut>c</Shortcut>
            <Description>Code snippet for Console.WriteLine</Description>           
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>SystemConsole</ID>
                    <Function>SimpleTypeName(global::System.Console)</Function>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[$SystemConsole$.WriteLine($end$);]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Now you just have to type c, tab, tab !!




回答5:


There IS a faster way:

The fastest way is q + Tab. I'm using it for my logging.

3 steps:

  • Create a new snippet file for q or use c+w (and skip this step)
    • C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1031\Visual C#
  • Go to options → Text-Editors → C# → IntelliSense and disable completion after 1 character
  • Enjoy!

Hope it works for you. Definitely works for me with VS 2010 for C#.



来源:https://stackoverflow.com/questions/2811904/using-code-snippets-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!