12

I'm refactoring some code and there are tons of functions with no try catch statements that are causing problems.

Is there a keyboard shortcut in VS 2010 to allow me to select an entire function or highlighted code and add a try catch end try statement around it...with the highlighted code automatically ending up under try ?

Just looking for a shortcut

Khalid Rahaman
  • 2,292
  • 3
  • 28
  • 40
  • As a heads up, you're probably going to get comments about fixing the code so it doesn't/can't create exceptions (since the exceptions are that common). Is there a reason the code can't be improved? – Matt Mar 29 '12 at 17:44
  • 2023: they added this functionality, [but only to JS/TS](https://code.visualstudio.com/updates/v1_64#_surround-with-snippets-for-jsts)... Let's hope they add this also for other languages like python soon! – tturbo Jan 23 '23 at 09:09

4 Answers4

23

I'm not aware of a keyboard short-cut, but there's a right-click available.

  • Highlight the code for your try block.
  • Right click and select
  • "Surround with..." Scroll down the selection until you see the "try" snippet label.
  • There you go!

Hope this helps!

I'll dig around to see if there's a keyboard short cut for this. There probably is -- there's hundreds of them.

UPDATE: Looks like the keyboard shortcut is Ctrl + K, Ctrl + S.

ROOT
  • 11,363
  • 5
  • 30
  • 45
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • 1
    interesting, i'll mark this as the answer but on my installation there is no "Surround with" option and CTRL K, CTRL S says "not currently available" - i've tried it in several projects – Khalid Rahaman Apr 10 '12 at 23:58
5

Download this free addon and then right click and surround with !

(Don't forget to give it 1+ )

https://marketplace.visualstudio.com/items?itemName=yatki.vscode-surround

enter image description here

grepit
  • 21,260
  • 6
  • 105
  • 81
0

2023 Update

they added this functionality, but only for JS/TS... Let's hope they add this also for other languages like python soon!

tturbo
  • 680
  • 5
  • 16
0

It's not built in, but it is easy enough to roll your own. Look into using the built in macro facility.

EvilTeach
  • 28,120
  • 21
  • 85
  • 141