Under Windows 10, download, install and use GMT with Vscode

Table of Contents

1.gmt download

2.gmt installation and configuration

2.1 gmt installation

2.2gmt configuration

2.3 Test installation

3. Install Bash environment

4. Configure VsCode

4.1 Install plug-ins

4.2 Add GitBash

5. Use gmt under Vscode

5.1 Run the .bat file

?edit

5.2 Run .sh file

5.3 Error reporting

refer to:


I recently wanted to draw a map of China and accidentally discovered this gmt software. However, when I used it, I found that there were very few installation and usage tutorials, or the descriptions were very brief. Therefore, I wanted to record my installation. and use process, and also provide some help to others.

1.gmt Download

In fact, downloading gmt is very simple. There is a download link on its official website. You can choose the 64-bit or 32-bit installation package and click directly to download the latest gmt.

2.gmt Installation and Configuration

In fact, the official website also provides an installation tutorial, but here I want to give some detailed procedures.

tips: You need to check the following things before installation

  • Before installation, please check your Windows login username: (1) whether it contains spaces; (2) whether it contains Chinese characters. Both of the above situations may cause gmt to run incorrectly. If the username does not meet the requirements, please create a new user. Then log in to Windows using the new user and install gmt.

  • Security software such as 360 will intercept modifications to the PATH environment variable during the gmt installation process. Please be sure to turn off security software before installing.

  • The installation package provided by gmt does not support Windows XP.

2.1 gmt installation

1) Download the installation package and double-click to install.

2) Click Next.

3) Here you add environment variables, you don’t need to configure them yourself, just choose the default ones.

4) Select the installation directory and click Next.

5) Here, create a folder under your installation location and just leave it as default.

6) Select components. All components here need to be checked, and then click Install.

tip: On the official website, you are given a warning that may occur during the installation process. You can follow his method to solve it, because I did not appear.

2.2gmt configuration

This section is just for explanation. In fact, in the third step of your installation, you have already selected the configuration. Here we can check it out.

Click “Computer” → “Properties” → “Advanced System Settings” → “Environment Variables” to open the “Environment Variables” editing tool

In path, there will be the path to the bin file where you installed gmt.

And a new variable name will be added to the system variables.

2.3 Test Installation

Directly [win + r] to start the command prompt.

Enter the command to view the version number:

gmt --version

If the gmt version number appears, it means that the gmt installation is successful.

3. Install Bash environment

The reason for installing Bash is given on the official website, that is: gmt can be used directly in Batch scripts under Windows, but due to the limitations of the Batch script’s own functions, the powerful functions of gmt cannot be fully utilized. Therefore, it is recommended that Windows users install a Bash environment and write Bash scripts to use gmt.

I chose Git installation.

My installation steps here are inconsistent with many tutorials on the Internet. It may be because I have installed nodejs. In fact, the general steps are to install and then configure the environment. You can refer to this blog.

4. Configure VsCode

Vscode is such a great software. You can smoothly switch between bat/bash scripts, preview jpg/gif/pdf and other graphics results in real time, and display script error messages. It is very suitable for executing gmt scripts on all platforms. You can install and configure it according to the instructions in this blog. In fact, I have written about the installation and configuration of Vscode before. You can also follow mine. Anyway, I didn’t install and configure it anymore, so I just used my own.

4.1 Install plug-in

Based on the recommendations on its blog and my own use, we install the following plug-ins:

  • Code Runner supports one-click running of code
  • vscode-pdf pdf preview plug-in

Open vscode and install the plug-in.

Among them, after installing the Code Runner plug-in, you need to configure it.

First open settings.

Then, check the three options

4.2 Add GitBash

1) Open settings

Click File->Preferences->Settings, search for shell.windows, and clickto edit in settings.json.

2) Add code

Tips: Pay attention to the Git installation path, which involves a total of three modifications.

After you add the code and save it, an error message will be displayed on shell.windows, so don’t worry about it.

"terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\Sysnative\cmd.exe",
                "${env:windir}\System32\cmd.exe"
            ],
            "args": [],
            "icon": "terminal-cmd"
        },
        "Git-Bash": {
            "path": "G:\software\\
odejs\git\Git\bin\bash.exe"
        }
    },
    "terminal.integrated.automationShell.windows": "G:\software\\
odejs\git\Git\bin\bash.exe",
    "git.path": "G:\software\\
odejs\git\Git\bin\bash.exe",
    "terminal.integrated.defaultProfile.windows": "Git-Bash",

3) Modify terminal.integrated.Default.Profiles.Windows

5. Use gmt under Vscode

After the above configuration, we can use gmt under Vscode.

5.1 Run .bat file

1) For the tutorial on installing the official website, first we create a new terminal under vscode.

At this time, our default terminal may be Git-Bsah, so we need to modify it.

If your terminal is under anaconda after clicking PowerShell, exit to the system terminal.

Note: You should be careful about the path here. The path under the F drive was created by me.

2) Go to the command prompt, enter gmt, and run gmt.

gmt

3) Generate script template

Continue typing in the terminal:

gmt --new-script > myplot.bat

At this time, a .bat file will be generated in your directory.

4) View and edit script files

The content after opening is as follows:

Among them, the end-of-line comment line starting with REM and set gmt_SESSION_NAME=7492 are advanced usage and can be ignored. The core content has only two lines, namely gmt begin and gmt end.

Add the gmt command between gmt begin and gmt end and modify the script as follows:

@echo off
REM Date: 2023-11-13T11:27:36
REM User: dell
REM Purpose: Purpose of this script
REM Set a unique session name:
set GMT_SESSION_NAME=51536
gmt begin figurename
gmt coast -Rg -JH15c -Gpurple -Baf -B + t"My First Plot"
gmt end show

5) Execute script to draw pictures

Continue to enter the command in the terminal to run:

myplot.bat

You will find that a pdf file appears in the root directory, and when you open it, you will see the picture you drew.

5.2 Run .sh file

1) For the tutorial on installing the official website, first we create a new terminal under vscode.

The terminal at this time is different from the terminal running bat. This is the Git-Bash terminal.

Pay attention to whether this path is the path where you run the code

2) Let’s create a new sh file (just right-click to create a new one) and write the code inside.

#!/usr/bin/env bash
# GMT modern mode bash template
# Date: 2019-09-10T00:44:39
# User: seisman
# Purpose: Purpose of this script
export GMT_SESSION_NAME=$$ # Set a unique session name
gmt begin figurename1
    gmt coast -Rg -JH15c -Gpurple -Baf -B + t"My First Plot"
gmt end show

3) Execute script to draw pictures

myplot.sh

5.3 Error Report

Note: This is the important point! ! ! !

1. ‘@’ is not recognized as an internal or external command, operable program or batch file. error appears

When you save, be sure to check the encoding method you saved and be sure to change it to UTF-8. Otherwise you will get the following error message.

The specific saving format is as follows:

Click to select encoding, then click Save with encoding

Select utf-8

At this time, the encoding method in the lower right corner will change

2. I have not implemented this error, but you should pay attention to the fact that when drawing, if Chinese appears in the code, then your encoding method must be changed to GB2312 encoding.

Reference:

【1】gmt Chinese learning official website

【2】[Study Notes] Using gmt6.2 under Win10 (Batch/Bash)

【3】gmt script execution tool–VS Code

【4】Add GitBash to VScode terminal