Terminal 跳转到 IntelliJ IDE

Summary: Author: 张亚飞 | Read Time: 2 minute read | Published: 2017-09-26
Filed under Categories: LinuxTags: Note,

Terminal 跳转到 IntelliJ IDE

如何从 iterm2 跳转到 goland

依次打开 iterm2 工具栏: Profiles -> Advanced -> Semantic History.

如果没有 idea 程序,则打开对应的 Intellij IDE 菜单: create command-line launcher 安装程序

设置 run command 模式,参数可以为:

  • /usr/local/bin/idea --line \2 \1
  • /usr/local/bin/goland --line \2 \1

以上只能跳转到固定的 ide

可以写个脚本根据文件后缀自动跳转到对应 IDE

参考网上资料设置 run command 模式,参数为: /usr/local/cos/libs/iterm-goto.sh \1 \2

示例:

#!/bin/bash

set -x

GOTO_FILE="$1"
GOTO_LINE="$2"
GOTO_CHAR="$3"

if ! [ -f "$GOTO_FILE" ]; then
    echo "ERROR: file path missing or invalid!" >&2
    exit 1
fi

# Discriminate based on file extension, open only .scala or .sbt or .rs or .java files in IntelliJ IDEA ...
if [[ "$GOTO_FILE" =~ ^.*\.(scala|sbt|rs|java)$ ]]; then
    EDITOR_PATH="$(which idea)"

    if [ -z "$EDITOR_PATH" ]; then
        EDITOR_PATH="/usr/local/bin/idea"
    fi

    if ! [ -z "$GOTO_LINE" ]; then
        exec "$EDITOR_PATH" --line "$GOTO_LINE" "$GOTO_FILE"
    else
        exec "$EDITOR_PATH" "$GOTO_FILE"
    fi
elif [[ "$GOTO_FILE" =~ ^.*\.(go)$ ]]; then
    EDITOR_PATH="$(which goland)"

    if [ -z "$EDITOR_PATH" ]; then
        EDITOR_PATH="/usr/local/bin/goland"
    fi

    if ! [ -z "$GOTO_LINE" ]; then
        exec "$EDITOR_PATH" --line "$GOTO_LINE" "$GOTO_FILE"
    else
        exec "$EDITOR_PATH" "$GOTO_FILE"
    fi
else
    EDITOR_PATH="$(which code)"

    if [ -z "$EDITOR_PATH" ]; then
        EDITOR_PATH="/usr/local/bin/code"
    fi

    if ! [ -z "$GOTO_CHAR" ]; then
        exec "$EDITOR_PATH" --goto "$GOTO_FILE:$GOTO_LINE:$GOTO_CHAR"
    elif ! [ -z "$GOTO_LINE" ]; then
        exec "$EDITOR_PATH" --goto "$GOTO_FILE:$GOTO_LINE"
    else
        exec "$EDITOR_PATH" "$GOTO_FILE"
    fi
fi

常见问题

1. 跳转后报错: env: python: No such file or directory

使用如下脚本跳转打开 IntelliJ IDE

跳转提示如下报错

+ GOTO_FILE=/Users/coam/Web/Work/vconsole/src/handlers/admin/product/app.go
+ GOTO_LINE=52
+ GOTO_CHAR=
+ '[' -f /Users/coam/Web/Work/vconsole/src/handlers/admin/product/app.go ']'
+ EDITOR_PATH=/usr/local/bin/idea
+ '[' -z '' ']'
+ EDITOR_PATH=/usr/local/bin/idea
+ '[' -z 52 ']'
+ exec /usr/local/bin/idea --line 52 /Users/coam/Web/Work/vconsole/src/handlers/admin/product/app.go
env: python: No such file or directory

查看 /usr/local/bin/idea 程序,发现是一个 python 文件

/usr/local/bin/idea

#!/usr/bin/env python
...

搜索资料要 建立 /usr/bin/python 软链

查看系统 python 环境

[coam@192: ~/Web/Work/vconsole]$ python --version
Python 3.9.1
[coam@192: ~/Web/Work/vconsole]$ python3 --version
Python 3.9.1
[coam@192: ~/Web/Work/vconsole]$ /usr/bin/python --version
-bash: /usr/bin/python: No such file or directory

发现有 python 环境,但是没有 /usr/bin/python 文件,导致系统找不到

查看 IDE 程序环境,发现默认是 #!/usr/bin/env python

$ cat /usr/local/bin/goland | head -n 3
#!/usr/bin/env python

查看系统有 /usr/bin/python3 /usr/local/bin/python

方案一: 也可以修改可执行程序为 python3 - 推荐

/usr/local/bin/goland

#!/usr/bin/env python3

方案二: 将 /usr/local/bin/ 加到 PATH 目录

~/.bash_profile

# python...
export PATH="$PATH:/usr/local/bin"

此方案加到用户目录的 ~/.bash_profile 不行

方案三: 也可以建立软链接 - 不推荐

ln -s /usr/bin/python3 /usr/bin/python

但是在 MacOS 系统已经不能操作系统目录了

ln: /usr/bin/python: Read-only file system

Comments

Cor-Ethan, the beverage → www.iirii.com