Bash, Python, Javascript, code command, etc. in Visual Studio Code on Mac

If you just want to write an Atom? Sublime? Script, you can just put Linux in VirtualBox and install Vim, and that's fine.

I was thinking about it until a while ago, but I changed my mind by touching Visual Studio Code, so I made a note.

It's only the first day since I started touching it, so I think there are many mistakes and inefficiencies. If you notice anything, please point it out.

2017/12/04 Addendum: I ended up returning to Vim (NeoVim) because I have a lot of work with CUI.

Settings for debugging Bash

Install an extension called Bash Debug created by a person named rogalmic.

Bash Debug: https://marketplace.visualstudio.com/items?itemName=rogalmic.bash-debug

First, make sure you have all the commands you need to use Bash Debug. If you can't find it, install it as brew.

$ which {cat,mkfifo,rm,pkill,bashdb}
-----
/bin/cat
/usr/bin/mkfifo
/bin/rm
/usr/bin/pkill
/usr/local/bin/bashdb
-----

After installing Bash Debug, create a script that works with Bash, and open it with an editor, it will look like the following.

$ cat vce_hoge/hello.sh 
-----
hoge="HELLO"
echo "$hoge"
-----

When you press F5, you will be asked what to do as shown below, so choose Bash Debug.

Then I was asked how to decide the path of the script, but I have not investigated the specific difference and I have no idea, so I chose 1.Script path should be selected and so on. ..

This will create a configuration file with a directory called .vscode and an execution environment called launch.json (see the cat result below).

$ cat vce_hoge/.vscode/launch.json 
-----
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Bash-Debug (select script from list of sh files)",
            "type": "bashdb",
            "request": "launch",
            "scriptPath": "${command.SelectScriptName}",
            "commandLineArguments": "",
            "windows": {
                "bashPath": "C:\\Windows\\sysnative\\bash.exe"
            },
            "linux": {
                "bashPath": "bash"
            },
            "osx": {
                "bashPath": "bash"
            }
        }
    ]
}
-----

After the above file is created, return to the editor and press F5 to display the list of scripts in the current folder in the Explorer drop-down list. Select the script you want to execute (debug).

Then you can debug as shown in the figure below.

Settings for debugging Javascript

Next to Bash is Javascript.

First, make sure you have Node.js installed. If it is not installed, it will be brew (same as above)

$ which node
-----
/usr/local/bin/node
-----

VSCode comes with a built-in Javascript debugger called Node Debug 2, so I wonder if it's necessary to install the extension for the time being.

Before making various settings, I first created a file called hello.js.

After creating the file, open the debug console and add the configuration to run Javascript.

Select "Start Program". スクリーンショット 2017-03-20 20.50.09.png

Since the setting is added to launch.json, modify the value of "program" as appropriate, add "protocol", and then save it. スクリーンショット 2017-03-20 21.02.13.png Learn more about debugging protocols: https://code.visualstudio.com/docs/editor/node-debugging

Open the debug console and select which configuration you want to debug. スクリーンショット 2017-03-20 20.56.19.png

After selecting the configuration, press F5 and you will be able to debug as shown below.

スクリーンショット 2017-03-20 21.05.22.png

Python

Install an extension called Python created by a person named Don Jayamanne. https://marketplace.visualstudio.com/items?itemName=donjayamanne.python

Of course, a Python execution environment is required, so it's brew (same as above)

The procedure is the same as Javascript. Select "Python" in Add Configuration and save launch.json.

You can debug by opening the debug console, selecting Python from the configuration and pressing F5.

Extensions that seem to be useful

Code Runner This is an extension that allows you to execute only a specific part selected by dragging the source code.

usage.gif

Code Runner: https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

About the code command

A little bit about the VSCode command line-the espresso3389 diary

http://espresso3389.hatenablog.com/entry/vscodeac2016-8

This article was very helpful to get an overview of what you can do with the code command.

To use the code command on Mac

On Mac, the path of the code command executable file is

/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code

(If placed in the Application directory). Even if I add the above directory to my PATH, it doesn't work straightforwardly, so I registered an alias like the one below in .bashrc.

alias code="/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
$ code --help
------
Visual Studio Code 1.10.2

Usage: code [options] [paths...]

Options:
  -d, --diff                  Open a diff editor. Requires to pass two file paths as arguments.
  -g, --goto                  Open the file at path at the line and character (add :line[:character] to path).
  --locale <locale>           The locale to use (e.g. en-US or zh-TW).
  -n, --new-window            Force a new instance of Code.
  -p, --performance           Start with the 'Developer: Startup Performance' command enabled.
  -r, --reuse-window          Force opening a file or folder in the last active window.
  --user-data-dir <dir>       Specifies the directory that user data is kept in, useful when running as root.
  --verbose                   Print verbose output (implies --wait).
  -w, --wait                  Wait for the window to be closed before returning.
  --extensions-dir <dir>      Set the root path for extensions.
  --list-extensions           List the installed extensions.
  --show-versions             Show versions of installed extensions, when using --list-extension.
  --install-extension <ext>   Installs an extension.
  --uninstall-extension <ext> Uninstalls an extension.
  --disable-extensions        Disable all installed extensions.
  --disable-gpu               Disable GPU hardware acceleration.
  -v, --version               Print version.
  -h, --help                  Print usage.
-----

Of course you can also use the diff option.

$ cat hoge.txt 
-----
hoge
-----

$ cat hoge2.txt 
-----
hoge2
-----

$ code -d hoge.txt hoge2.txt
-----
See image below
-----

Contents of the code command

$ cat /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code 
-----
#!/usr/bin/env bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

function realpath() { /usr/bin/python -c "import os,sys; print os.path.realpath(sys.argv[1])" "$0"; }
CONTENTS="$(dirname "$(dirname "$(dirname "$(dirname "$(realpath "$0")")")")")"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
-----

Somehow, I feel like I can guess it, but I haven't seen it now ...

Shortcut key

https://code.visualstudio.com/docs/customization/keybindings

On a Mac, F11 etc. conflict with the standard functions of the OS, so it may be better to remap.

Recommended Posts

Bash, Python, Javascript, code command, etc. in Visual Studio Code on Mac
Run Python in C ++ on Visual Studio 2017
Run Python YOLOv3 in C ++ on Visual Studio 2017
Settings for Python coding in Visual Studio Code
Create a Python development environment in 10 minutes (Mac OS X + Visual Studio Code)
Python development in Visual Studio 2017
Python development in Visual Studio
Do something like a Python interpreter in Visual Studio Code
Install python and Visual Studio Code on windows10 (April 2020 version)
Installation of Visual studio code and installation of python
Notes on using code formatter in Python
How to hide the command prompt when running python in visual studio 2015
Create a Python development environment on Windows (Visual Studio Code remote WSL).
From re-environment construction of Python to graph drawing (on visual studio code)
python on mac
Build Python development environment with Visual Studio Code
Time when terminal disappears in Visual Studio Code
Try working with Mongo in Python on Mac
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Highlight Python user function calls in Visual Studio Code (vscode) Default Dark + theme
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
App development to tweet in Python from Visual Studio 2017
Try debugging Python on Raspberry Pi with Visual Studio.
Install Python on Mac
Make Visual Studio Code autocomplete for python external libraries
Python development environment with Windows + Anaconda3 + Visual Studio Code
Python development environment with Windows + Python + PipEnv + Visual Studio Code
Until you run server Django in Visual Studio Code
Japanese output when dealing with python in visual studio
AWS SDK for Python (Boto3) development in Visual Studio 2017
Install Python 3.4 on Mac
Install Bash on Ubuntu on Windows, Ruby, Python, Jupyter, etc.
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output
How to use linux commands in Visual Studio Code terminal
Set up a Python development environment with Visual Studio Code
How to debug the Python standard library in Visual Studio
Enable the virtualenv Python virtual environment for Visual Studio Code
Build Python3 for Windows 10 on ARM with Visual Studio 2019 (x86) on Windows 10 on ARM
VScode environment construction (on Mac) & graph display in Python (@browser)
Install numpy in Visual Studio 2019
Install pygame on python3.4 on mac
[Python] Insert ":" in MAC address
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Twitter posts on Python 3 etc.
Bottle Pug in Visual studio 2019
Run unix command on python
Generate QR code in Python
External command execution in Python
Character code learned in Python
How to copy and paste command line content without mouse in bash on Linux or mac
Create a Python environment for professionals in VS Code on Windows
Try to create a python environment with Visual Studio Code & WSL
Simple code to call a python program from Javascript on EC2
Try it when Visual Studio Code can't load a Python module
Edit files directly on Linux with Visual Studio Code Remote SSH
A memo for those who use Python in Visual Studio (me)
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
Arrow keys do not work in zsh + python shell on mac