11

When I execute my code using the VS-Code debugger, a new console is opened that is auto-named Python Debug Console.

      enter image description here


The right-click drop-open menu allows me to manually change the consoles name.

      enter image description here


I would rather not have to right click (and type) each time I start my debugger. Does anyone know if it is possible to configure the name of the console that the debugger opens? And if it is possible, how what does that configuration look like?




EDIT: This feature is currently not available, I've opened an issue HERE.


JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
Alon Barad
  • 1,491
  • 1
  • 13
  • 26
  • 1
    See https://github.com/microsoft/vscode/issues/109235 and https://stackoverflow.com/a/59222628/836330 – Mark Jul 02 '21 at 05:39
  • I appreciate the bounty, did you get it working? I had a hard time trying to explain how to do it, and I thought about it, because it really isn't that hard. I could a project to my github, that has it working already, so you can see how to do it if you want. But I am guessing since I collected the bounty, that you figured it out. – JΛYDΞV Jul 04 '21 at 08:46
  • And you can also change the consoles icon, so it shows a bug while your debugging instead of the console icon. In-fact there's a console icon with a bug you can use. Anyways, I never even thought to do this, until you asked about it. – JΛYDΞV Jul 04 '21 at 08:51

2 Answers2

9

Getting a Dynamic VS-Code Console Name


This could be considered by some to be one of the more advanced configurations that VSCode supports. Adding the functionality that your asking for to _V.S. Code will require the use of tasks, inputs, launch.json hooks, and a VSCode command that will take an argument. It is less work than it sounds like, and I wrote the code for you so that should help. It will require you to do some reading, likewise I will have to do some writing — "lets start!"



The V.S. Code Directory


The first thing you need to do is open a project that you can use your debugger in so you can test your tasks that your building. I printed a file tree then Copy & Pasted the tree below this excerpt. The tree is simple, but it is concise and clear. Make sure you have your .vscode directory set up like in the file tree, which includes your launch.json, settings.json & tasks.json configuration files.




SIDE-NOTE: We will not be doing anything in the settings.json file, I just want to show that it is okay to have that in your directory.
        . 
        |
        └── .vscode
            ├── launch.json
            ├── settings.json
            └── tasks.json        


The Command That makes it all possible: workbench.action.terminal.rename


Under the hood, the workbench.action.terminal.rename is no different from right clicking on the consoles name to rename the console, just like you demonstrated in your posted photo.



Creating the TASKS


Open up the "./.vscode/tasks.json" file, now; look below this excerpt, you will see a fenced code block, that is the task configuration that gives you what you want: "A dynamic console name that changes when the debugger runs." You should take a minute to look it over, its pretty straight forward and easy to understand. At your leisure copy and paste the JSON code below into your tasks.json file.


ADD THE CODE BELOW TO YOUR "./.vscode/tasks.json" File

    /__ @file ".../.vscode/tasks.json"  */


    {
        "version": "2.0.0", /* MAKE SURE THIS LINE IS EXACTLY THE SAME */

        // TASK DEFINITIONS
        "tasks": [
            {
                /* TASK_#1 */
                "label": "task|renameConsolePre",
                "command": "${input:renameConsolePre}"
            },
            {
                /* TASK_#2 */
                "label": "task|renameConsolePost",
                "command": "${input:renameConsolePost}"
            }
        ],

        // |> INPUT DEFINITIONS
        "inputs": [
            {
                /* INPUT FOR TASK_#1 */
                "id": "renameConsolePre",
                "type": "command",
                "command": "workbench.action.terminal.renameWithArg",
                "args": {
                    /* NAME OF CONSOLE WHEN DEBUGGER IS ACTIVE */
                    "name": "Debugging Console" 
                }
            },
            {        
                /* INPUT FOR TASK_#2 */
                "id": "renameConsolePost",
                "type": "command",
                "command": "workbench.action.terminal.renameWithArg",
                "args": {
                    /* NAME OF CONSOLE WHEN DEBUGGER IS INACTIVE */
                    "name": "BASH" 
                }
            }
        ]
    }



Their are many things within the snippet of the JSON file above, that could be discussed, and gone into great detail about, however, for the sake of time I will point out, that which I feel is the most important to take from that snippet. Obviously I haven't talked about inputs, and now's probably the perfect time to mention them.

The whole purpose of the "inputs":[] array, is to have a place where variable references can be defined. Because you want to add dynamics to vscode, you are going to need to use an argument, will with out the inputs, you wouldn't be able to implement the command your using in your task, because you couldn't pass an argument to it. (At the bottom of the page is a link to variable references, you should check it out).




This is a good point to test your task

To test that your tasks in your "./.vscode/tasks.json" file are working, you will need to open up your keybindings.json file.


  • Pressing the [F1] key. The Quick-Menu should drop open.
  • Type into the Quick Menu: "Keyboard Shortcuts"
  • The quick menu should offer you 2 DIFFERENT keyboard shortcuts to choose from. Pick the one that DOES NOT say default.
  • Now you should be inside of your ~/.config/Code/User/keybindings.json file.

If you write your own Keybindings then the instructions above probably did not need to be read, and you probably already have several keybindings defined into your keybindings.json file. If you don't define your own keybindings then the file is probably empty.

In the case where you define your own keybindings, create a keybinding like this, and assign it to your keys of choice.
    {
        "key": "",
        "command": "workbench.action.tasks.runTask",
        "args": "task|renameConsolePre",
        "when": ""
    },
  


If you have have never added a keybinding to your keybindings.json file, then add the following to the file, and make sure the the array brackets that the entire code-snippet is wrapped in are the bottom most level block in the file. The keybindings.json

file takes an array, rather than the typical object that you see in JSON files.

    [
        {
            "key": "ctrl+shift+space ctrl+shift+space",
            "command": "workbench.action.tasks.runTask",
            "args": "task|renameConsolePre",
            "when": ""
        },
    ]
  

Using the Keybinding

[CTRL]+[SHIFT] & while you are holding the two keys down, press space twice — back to back.

The actual keybinding looks like this: [ctrl]+[shift]+[space] [ctrl]+[shift]+[space]


If You created the keybinding correctly, and you pasted the file contents of the code from the snippet above, into the correct file, that is located in the correct directory, then the keybinding should trigger the console's name to change. If the name doesn't change, you did something wrong. The advantage to testing if your tasks are working this way, is that it beats the heck outta starting the debugger every time you want to test your tasks. This is generally considered one of the better ways to troubleshoot & debug tasks created for the editor.


LAUNCH DOT JSON CONFIGURATION

{
    "configurations": [
        {
            "args": ["--extensionDevelopmentPath=${workspaceFolder}"],
            "name": "Launch Extension",
            "outFiles": ["${workspaceFolder}/out/**/*.js"],
            "request": "launch",
            "type": "pwa-extensionHost",

            /* "Pre-hook is assigned TASK #1" */   
            "preLaunchTask": "task|renameConsolePre",

            /* "Post-Hook is assigned TASK #2" */ 
            "postDebugTask": "task|renameConsolePost"
        }
    ]
}

Okay, so I cannot configure the launch.json file to debug your program, because I don't know what program/language you debugging, but the hooks should all be the same.

Here are the names & definitions of the hooks. This is the code.visualstudio.com Documentation Definition of the preLaunch properties (hooks)


  • preLaunchTask - to launch a task before the start of a debug session, set this attribute to the label of a task specified in tasks.json (in the workspace's .vscode folder). Or, this can be set to $

  • postDebugTask - to launch a task at the very end of a debug session, set this attribute to the name of a task specified in tasks.json (in the work-space's .vscode folder).


Now if you need more help, you can ask me, I enjoy the conversion so feel free. It is always good to solve stuff on our own if possible though, so here are some reference you can try if you get stuck:



I had the links at different sections of the answer, but I decided to put all the links here in one place, so the are easy to locate if ever need to refer back to one of the links. The First two links are anchors from the same base URL, and are extremely helpful when you have problems configuring your launch.json file. Not just for the purpose of this question/answer, but they are helpful just in general.




"The link below shows examples, and demonstrations of correct launch.json configurations"

VSCode Docs: launch-configurations

(AT) - https://code.visualstudio.com/docs/editor/debugging#_launch-configurations



"This is by far the best resource on the list, as it really goes into detail about each launch.json attribute. (Extremely helpful!)"

launch.json Attributes

(AT) - https://code.visualstudio.com/Docs/editor/debugging#_launchjson-attributes



"This link covers inputs, and how to pass arguments to commands when they are called by events like the when the debugger starts, and you use a hook to get your code called.)"

Inputs, and how Inputs in Tasks Work

(AT) - https://code.visualstudio.com/docs/editor/variables-reference



JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
  • I know, I got to edit this. Took me a seriously long time to write, but this is the solutions, and as far as I could tell their is no other guide for doing this, or you would have got a link to the guide instead lol. Ima sleep, then wake up and edit. – JΛYDΞV Jul 03 '21 at 02:27
  • 1
    Impressive answer, the keybindings work like a charm! I'm using the GUI to launch a debug configuration, when clicking 'Start Debugging' it launches a new console. This new console is not correctly detected by the `preLaunchTask`. Ending up with no rename for the debug console, or renaming an open console if one exists. When quitting the debugger from the console, the correct console is renamed according the `postDebugTask`. Do you have an idea how to fix this? My guess is that the `preLaunchTask` is called before the new console is created. – Joep Jul 04 '21 at 10:57
  • @Joep To be honest I have never used the python debugger, but read a lot about it after reading your comment. From what I read, there have been several people who don't like how python opens new terminals, consequently, it looks like like the VSCode team added a couple config-properties to the ___`.vscode/launch.json`___ file as a way of addressing the issues (i.e. issues posted on the VSC GitHub). – JΛYDΞV Jul 04 '21 at 18:57
  • 1
    One of the `launch.json` settings that may help is: ___`"console": "internalConsole"`___. Here is a link to the docs that covers the `launch.json` `console` property. __https://code.visualstudio.com/docs/python/debugging#_console__ – JΛYDΞV Jul 04 '21 at 18:58
  • I use Chrome Dev-Tools to debug the backend node.js applications I write, and the output can be seen in my console, and in my _"Debug Console"_ so I just turned off the opening of the Debug Console, as I find it to be not very useful. I also have a console in Chrome DevTools. Your gonna have to configure your debugger to use the console you have open if possible, or to continuously reuse a console. – JΛYDΞV Jul 04 '21 at 19:11
  • I will continue to look for a good solution, and ask around. – JΛYDΞV Jul 04 '21 at 19:14
  • 3
    @JayDev https://github.com/microsoft/vscode/issues/128108 – Alon Barad Jul 07 '21 at 12:15
  • 1
    @AlonBarad That's great Alon! I hope you don't mind, but I gave my two sense on the matter. I feel that it should be implemented through a ___`"postLaunch"`___ hook, but that's just me. This is a wonderful Idea that you have proposed. – JΛYDΞV Jul 07 '21 at 15:45
  • The `preLaunchTask` hook doesn't work as it renames the "current" terminal and runs the debugger in a new terminal. – Ankit Jain May 12 '22 at 12:55
6

Just use

"consoleTitle": "your console title here"

in your launch.json file.

Example launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Django runserver",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver",
                "0.0.0.0:8000",
            ],
            "django": true,
            "consoleTitle": "django"
        }
    ]
}

I found it mentioned here https://github.com/microsoft/debugpy/issues/1178. I've verified it on vscode 1.77.0 and it works even though you get a warning in text editor:

Property consoleTitle is not allowed.

mkasp98
  • 164
  • 1
  • 6