vscode code 命令失效的解决办法

How to install Visual Studio Code's `code` command permanently?

Ask Question

Asked 3 years, 5 months ago

Active 7 months ago

Viewed 24k times

 

6

 

2

 

I just followed the top-voted answer here and successfully installed Visual Studio Code's code command in my terminal.

However, the code command fails to launch Visual Studio Code every time after I restart my MacBook Pro.

~ code
zsh: command not found: code

Is it possible to install code permanently so that I don't have to install it every time I turn on my MacBook Pro?

~ where code
/usr/local/bin/code

~ ls -l /usr/local/bin/code
lrwxr-xr-x  1 myName  admin  167 Aug  5 13:41 /usr/local/bin/code -> /private/var/folders/bh/525lnbns1213cx2651s97my00000gp/T/AppTranslocation/EA379FC4-05D2-4739-BE49-1D8870E47B8A/d/Visual Studio Code.app/Contents/Resources/app/bin/code

I also find out that the folder EA379FC4-05D2-4739-BE49-1D8870E47B8A get deleted after I restart my laptop.

This is the reason that I cannot run code after the laptop is restarted.

Why is code installed in a temporary folder?

My MacBook Pro's version is macOS Sierra version 10.12.6

macos

Share

Improve this question

Follow

asked Aug 5 '17 at 6:11

 

Brian

36122 gold badges33 silver badges99 bronze badges

add a comment

5 Answers

ActiveOldestVotes

17

 

Looks like your whole Visual Studio got installed in a temporary folder.

  • Remove the current link with sudo rm /usr/local/bin/code

  • Start Visual Studio from within /Applications

  • Reinstall code as described in https://stackoverflow.com/a/29971430:

    Now open the Command Palette (F1 or ⇧⌘P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.

If it still doesn't work you can also link manually by running

sudo ln -fs "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/

Share

Improve this answer

Follow

edited Jun 15 '20 at 8:04

Community

1

answered Aug 5 '17 at 6:29

 

nohillside

71.3k1515 gold badges149149 silver badges192192 bronze badges

  • 1

    Thank you. The solution works. I found that my Visual Studio Code.app was in Downloads folder. The code command is installed in /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code after I dragged it to Applications folder. – Brian Aug 5 '17 at 6:43 

  • 1

    Moving VSCode from /Downloads to /Applications and re-running the VSCode pallette command did the trick for me too. – Lauren Aug 8 '18 at 21:54

add a comment

3

 

If you're on Mac OS Catalina, you need to edit your .zprofile instead of .bash_profile.

  1. Edit your ~/.zprofile file: vim ~/.zprofile
  2. Add the following code in it, on it's own line: code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
  3. Save the file: :wq
  4. Re-run the updated file: source ~/.zprofile.
  5. Test that running code . opens your current folder in VS Code!

Share

Improve this answer

Follow

answered Nov 13 '19 at 21:14

Josiah Ayres

3122 bronze badges

add a comment

2

 

OK, it seems "ln" command not work for me (I use this command set up sublime text). But I got an another solution.

  1. edit your ~/.bash_profile file.

  2. add the following code in it. code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

  3. after save. do not forget to run source ~/.bash_profile.

  4. Finished and test it, type code

Share

Improve this answer

Follow

edited May 5 '18 at 2:20

 

Monomeeth

58.4k1111 gold badges130130 silver badges175175 bronze badges

answered May 5 '18 at 2:00

 

Alex Lu

12111 bronze badge

  • For zsh, we probably want to edit .zshrc instead of .bash_profile, though. That way, we don't need to source ~/.bash_profile each time we need the settings... – Richard Ambler Dec 1 '19 at 14:07

add a comment

1

 

GOOD TO KNOW:
Since Catalina update, MacOS uses zsh bash environment by default*
*Resource about zsh and terminal

ALTERNATIVE APPROACH:
Personnal settup: currently using macOS with Catalina:

  • bash file & location: .zshrc~/.zshrc
  • .zshrc existance: ls -la > and look for .zshrc file

    • if no .zshrc file: touch .zshrc
  • open & write –– alias code="open -a 'Visual Studio Code'".
    Which will behave like what Monomeeth mentioned above:
    opens your VSCode from your folder Applications.

Share

Improve this answer

Follow

answered Mar 24 '20 at 20:16

 

Lowla

1122 bronze badges

add a comment

0

 

One thing I noticed was that my Visual Studio Code.app was in Downloads/, not Applications/.

If this is the case, run mv /Users/moi/Downloads/Visual\ Studio\ Code.app /Applications and try again!

Good luck

Share

Improve this answer

Follow

answered May 29 '20 at 14:57

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值