Assign code snippet to keyboard shortcut in Visual Studio
Asked Answered
C

4

25

Anyone knows how to assign key shortcut to specific code snippet?

I would like to assign for instance CTRL+K,CTRL+J to a #region snippet.

Therefore by able to:

  1. select text,
  2. press CTRL+K,CTRL+J =>
  3. selected text would be surrounded with #region .. #endregion.

I hate when I have to take my hands out of keyboard for more time than necessary:).

EDIT: For more understanding, I am asking how to bind key shortcut directly to SPECIFIC snippet. Opening snippet selector wont work for me. If I have to search through all my snippets to get to a the #region one, its worse then writting all its code by myself.

Thx for any suggestions.

Capo answered 25/4, 2012 at 9:4 Comment(5)
ctrl+k +s pops up surround with selection, then you need to select #regionRomaromagna
thank you, but i want to bind shortcut directly to #region not just open snippet selectorCapo
There is no VS 2011. I've edited your title.Satyriasis
This would be an extremely useful feature, but have not found a solution either.Greenery
I couldn't find a solution either. The only way is CTRL + "k" + "s" and then navigate the snippet selector with the mouse or typing the snippet name/directory then press ENTER. The <shortcut> field in the snippet code appears to be completely useless in this case.Ferd
R
4

Type a < and then type your snippet shorcut(your XML file Name) and then press tab.

you can get more info from this link https://learn.microsoft.com/en-us/visualstudio/xml-tools/how-to-use-xml-snippets?view=vs-2017

for example i have a code snippet which i've set xml file name and its shortcut to flog

when i want to use it directly to my code i do like this:

<flog and press tab it Works.

Renshaw answered 11/3, 2019 at 9:9 Comment(2)
if you skip IntelliSence by esc and type the shortcut it would be work in VS2017 too.Renshaw
The < key works when you are editing a xml file, not code file.Postobit
L
0

The closest solution I've found for this is to copy the intended snippet to your local snippets directory.

e.g. For VS-2015: %Userprofile%\Documents\Visual Studio 2015\Code Snippets\Visual C#\My Code Snippets

Then open it in a text editor and change the shortcut to something quick and simple such as '1'.

Now to use it, all you need to do is press Ctrl+K+S > Enter > 1 > Enter

Additionally, you can make it even quicker by changing the hotkey for the surround-with command. (Tools > Options > Keyboard > Edit.SurroundWith)

Lilalilac answered 10/11, 2015 at 2:52 Comment(0)
I
0

For me the fastest solution was to Import a Folder named "1", then Add the snippet (my name was "block") and mark it in the 1-Folder.

Then i can insert the snippet by <Ctrl+K,X> + <Enter> + <Enter>

First Enter chooses 1 (the Folder with MY snippets) and the second Enter chooses my most used snippet (must be alphabetically first)

Interject answered 17/11, 2020 at 13:10 Comment(0)
A
-1

I think you may want to try adding a <Shortcut> tag inside your MySnippet.snippet file

<Header>
   <Title>Square Root</Title>
   <Author>Myself</Author>
   <Description>Calculates the square root of 16.</Description>
   <Shortcut>sqrt</Shortcut>
 </Header>

For details check out : https://learn.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet?view=vs-2019#description-and-shortcut-fields

To add a shortcut, add a Shortcut element within the Header element

Assurgent answered 4/3, 2020 at 7:40 Comment(2)
This is the easiest and most versatile solutionHalftimbered
To use this shortcut you just start to type it out and then select it from the dropdown (title and description will show to the side), hit Tab twice to insert the snippet. I'm using VS 2022, presume it works the same on earlier versions.Ikey

© 2022 - 2024 — McMap. All rights reserved.