Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying variable contents to clipboard while debugging in Visual Studio Code

I'm debugging in Visual Studio Code and I have a JSON object that I would like to copy as text to the clipboard.

Is this possible inside of Visual Studio Code?

like image 876
Mike Cheel Avatar asked Feb 23 '17 18:02

Mike Cheel


People also ask

How do I Debug a variable in Visual Studio?

Go to menu Debug->Windows->Locals to make it appear. Watch Although it is a little manually you also can use 'Watch' you can drag and drop any variable to this window or right click then add to watch. Go to menu Debug->Windows->Watch to make it appear. Note.

How do you copy an object in Visual Studio?

In Visual Studio debug mode it's possible to hover over variables to show their value and then right-click to "Copy", "Copy Expression" or "Copy Value". In case the variable is an object and not just a basic type, there's a + sign to expand and explore the object.

How do I skip the code while debugging in Visual Studio?

You can also click on the line you want to skip to and hit Ctrl+F10 (Run to Cursor).


2 Answers

I found two ways to do that, both of which are a bit hacky (in my eyes).

Use console.log

I think there will be a limit to the size of the string that this can output, but it was satisfactory for my requirements.

  1. In the debug console, write console.log(JSON.stringify(yourJsonObject))
  2. Copy the resulting output from the debug console. That can be a bit tedious for long strings, but a combination of mouse and keyboard (ctrl-shift-end) worked ok for me.

Use a watch (limited to 10'000 characters)

This method only works up to a limited size of the resulting json string (it looks like 10'000 characters).

  1. Set a breakpoint in a reasonable location where your variable is in scope and start your app.
  2. Go to the debug view, add a watch for a temporary variable, e.g. tmpJson
  3. Get your breakpoint to hit.
  4. In the debug console, write var tmpJson = JSON.stringify(yourJsonObject)
  5. This will now have populated the watched variable tmpJson with the string representation of your json object
  6. In the debug view, right click on the watched variable, click copy.

If the string is too long, it cuts it off with a message like the following: ...,"typeName":"rouParallel","toolAssembly":{"id":"ASKA800201","description":"CeonoglodaloD50R6z5","c... (length: 80365)"

But it would work for smaller objects. Maybe this helps some people. It would be great to have this properly built-in with vscode.

like image 126
Ben Avatar answered Oct 04 '22 10:10

Ben


There is an open issue regarding this: https://github.com/microsoft/vscode-java-debug/issues/624

Workaround :

  1. Go to the VARIABLES panel and right click to show contextual menu on a variable
  2. select Set Value
  3. Ctrl+C

(tested on Java, not JavaScript)

like image 21
youssef Avatar answered Oct 04 '22 12:10

youssef