如何使用 Visual Studio Code 作为 Git 的默认编辑器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30024353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Visual Studio Code as Default Editor for Git
提问by Gary Ewan Park
When using git at the command line, I am wondering if it is possible to use Visual Studio Code as the default editor, i.e. when creating commit comments, and looking at a diff of a file from the command line.
在命令行中使用 git 时,我想知道是否可以使用 Visual Studio Code 作为默认编辑器,即在创建提交注释时,以及从命令行查看文件的差异时。
I understand that it won't be possible to use it for doing merges (at least at the minute) but does anyone know if it is possible to use it for looking at diff's, and if so, what command line options would be required in the .gitconfig file to make this happen?
我知道不可能用它来进行合并(至少在一分钟内),但有没有人知道是否可以用它来查看差异,如果是这样,需要哪些命令行选项.gitconfig 文件来实现这一点?
UPDATE 1:
更新1:
I have tried an approach similar to what I have done for Notepad++ in the past, i.e.
#!/bin/sh
"c:/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"
And used:
并使用:
#!/bin/sh
"C:\Users\gep13\AppData\Local\Code\app-0.1.0\Code.exe" "$*"
But this results in an error message:
但这会导致错误消息:
C:\temp\testrepo [master +1 ~0 -0]> git commit
[8660:0504/084217:ERROR:crash_reporter_win.cc(70)] Cannot initialize out-of-process crash handler
Aborting commit due to empty commit message.
C:\temp\testrepo [master +1 ~0 -0]>
Code opens up correctly, with the expected content, but it isn't waiting on the response, i.e. clicking save and closing the window to return to prompt.
代码以预期的内容正确打开,但它不会等待响应,即单击保存并关闭窗口以返回提示。
UPDATE 2:
更新 2:
I have just heard back from one of the developers working on VSCode. Looks like this functionality currently isn't supported :-(
我刚刚收到一位从事 VSCode 的开发人员的回复。看起来目前不支持此功能:-(
https://twitter.com/IsidorN/status/595501573880553472
https://twitter.com/IsidorN/status/595501573880553472
If you are interested in seeing this feature get added, you might want to think about adding your votes here:
如果您有兴趣看到添加此功能,您可能需要考虑在此处添加您的投票:
UPDATE 3:
更新 3:
I have been reliably informedthat this feature has been picked up by the VSCode team, so I am looking forward to a future release that will include it.
我已经可靠地告知VSCode 团队已采用此功能,因此我期待将来的版本中包含它。
UPDATE 4:
更新 4:
Thanks to @f-boucheros comment below, I have been able to get VS Code working as the default editor for commit comments, rebase, etc. I would still like to see if it is possible to use it as the diff tool as well.
感谢下面的@f-boucheros 评论,我已经能够让 VS Code 作为提交评论、rebase 等的默认编辑器工作。我仍然想看看是否可以将它用作 diff 工具。
UPDATE 5:
更新 5:
As per the accepted answer for the question, this is now possible using the V1.0 release of code.
根据该问题的公认答案,现在可以使用 V1.0 版本的代码来实现。
回答by rexcfnghk
In the most recent release (v1.0, released in March 2016), you are now able to use VS Code as the default git commit/diff tool. Quoted from the documentations:
在最新版本(v1.0,2016 年3 月发布)中,您现在可以使用 VS Code 作为默认的 git commit/diff 工具。引用自文档:
Make sure you can run
code --help
from the command line and you get help.
if you do not see help, please follow these steps:
Mac: Select Shell Command: Install 'Code' command in pathfrom the Command Palette.
- Command Palette is what pops up when you press shift+ ?+ Pwhile inside VS Code. (shift+ ctrl+ Pin Windows)
- Windows: Make sure you selected Add to PATHduring the installation.
- Linux: Make sure you installed Code via our new .deb or .rpm packages.
- From the command line, run
git config --global core.editor "code --wait"
Now you can run
git config --global -e
and use VS Code as editor for configuring Git.Add the following to enable support for using VS Code as diff tool:
确保您可以从命令行运行
code --help
并获得帮助。
如果您没有看到帮助,请按照以下步骤操作:
Mac:选择Shell 命令:在命令面板的路径中安装“代码”命令。
- 命令面板是当您在 VS Code 中按shift+ ?+P时弹出的内容。(shift+ ctrl+P在Windows中)
- Windows:确保在安装过程中选择了添加到 PATH。
- Linux:确保您通过我们新的 .deb 或 .rpm 包安装了 Code。
- 从命令行,运行
git config --global core.editor "code --wait"
现在您可以运行
git config --global -e
并使用 VS Code 作为配置 Git 的编辑器。添加以下内容以支持使用 VS Code 作为差异工具:
[diff]
tool = default-difftool
[difftool "default-difftool"]
cmd = code --wait --diff $LOCAL $REMOTE
This leverages the new
--diff
option you can pass to VS Code to compare two files side by side.To summarize, here are some examples of where you can use Git with VS Code:
git rebase HEAD~3 -i
allows to interactive rebase using VS Codegit commit
allows to use VS Code for the commit messagegit add -p
followed bye
for interactive addgit difftool <commit>^ <commit>
allows to use VS Code as diff editor for changes
这利用了
--diff
您可以传递给 VS Code的新选项来并排比较两个文件。总而言之,以下是一些可以将 Git 与 VS Code 结合使用的示例:
git rebase HEAD~3 -i
允许使用 VS Code 进行交互式变基git commit
允许将 VS Code 用于提交消息git add -p
其次是e
交互式添加git difftool <commit>^ <commit>
允许使用 VS Code 作为差异编辑器进行更改
回答by Frank Boucher
For what I understand, VSCode is not in AppData anymore.
据我了解,VSCode 不再在 AppData 中。
So Set the default git editor by executing that command in a command prompt window:
因此,通过在命令提示符窗口中执行该命令来设置默认的 git 编辑器:
git config --global core.editor "'C:\Program Files (x86)\Microsoft VS Code\code.exe' -w"
The parameter -w
, --wait
is to wait for window to be closed before returning. Visual Studio Code is base on Atom Editor. if you also have atom installed execute the command atom --help
. You will see the last argument in the help is wait.
参数-w
,--wait
是等待窗口关闭后再返回。Visual Studio Code 基于 Atom 编辑器。如果您还安装了 atom 执行命令atom --help
。您将看到帮助中的最后一个参数是等待。
Next time you do a git rebase -i HEAD~3
it will popup Visual Studio Code. Once VSCode is close then Git will take back the lead.
下次执行时,git rebase -i HEAD~3
它会弹出 Visual Studio Code。一旦 VSCode 关闭,Git 将夺回领先地位。
Note: My current version of VSCode is 0.9.2
注意:我当前的 VSCode 版本是 0.9.2
I hope that help.
我希望有帮助。
回答by IluxaKuk
You need to use command:
您需要使用命令:
git config --global core.editor "'C:\Program Files\Microsoft VS Code\code.exe' -n -w"
Make sure you can start your editor from Git Bash
确保您可以从 Git Bash 启动编辑器
If you want to use Code.exe with short path, you can do this by adding the following line to your .bash_profile:
如果要使用具有短路径的 Code.exe,可以通过将以下行添加到 .bash_profile 来实现:
alias vscode="C:/Program\ Files/Microsoft\ VS\ Code/Code.exe"
And now, you can call it using only vscode
command(or whatever you named it)
现在,您可以仅使用vscode
命令(或您命名的任何名称)来调用它
Some additional info:
一些额外的信息:
Setup will add Visual Studio Code to your %PATH%, so from the console you can type 'code' to open VS Code on that folder. You will need to restart your console after the installation for the change to the %PATH% environmental variable to take effect.
安装程序会将 Visual Studio Code 添加到您的 %PATH%,因此您可以从控制台键入“code”以打开该文件夹上的 VS Code。安装后,您需要重新启动控制台以使对 %PATH% 环境变量的更改生效。
回答by Ruslan Stelmachenko
Another useful option is to set EDITOR
environment variable. This environment variable is used by many utilities to know what editor to use. Git also uses it if no core.editor
is set.
另一个有用的选项是设置EDITOR
环境变量。许多实用程序使用此环境变量来了解要使用的编辑器。如果没有core.editor
设置,Git 也会使用它。
You can set it for current session using:
您可以使用以下方法为当前会话设置它:
export EDITOR="code --wait"
This way not only git
, but many other applications will use VS Code as an editor.
不仅如此git
,许多其他应用程序也会使用 VS Code 作为编辑器。
To make this change permanent, add this to your ~/.profile
for example. See this questionfor more options.
要使此更改永久化,请将其添加到您~/.profile
的示例中。有关更多选项,请参阅此问题。
Another advantage of this approachis that you can set different editors for different cases:
这种方法的另一个优点是您可以为不同的情况设置不同的编辑器:
- When you working from local terminal.
- When you are connected through SSH session.
- 当您从本地终端工作时。
- 当您通过 SSH 会话连接时。
This is useful especially with VS Code (or any other GUI editor) because it just doesn't work without GUI.
这对于 VS Code(或任何其他 GUI 编辑器)尤其有用,因为它在没有 GUI 的情况下无法工作。
On Linux OS, put this into your ~/.profile
:
在 Linux 操作系统上,把它放到你的~/.profile
:
# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then # SSH mode
export EDITOR='vim'
else # Local terminal mode
export EDITOR='code -w'
fi
This way when you use a local terminal, the $SSH_CONNECTION
environment variable will be empty, so the code -w
editor will be used, but when you are connected through SSH, then $SSH_CONNECTION
environment variable will be a non-empty string, so the vim
editor will be used. It is console editor, so it will work even when you are connected through SSH.
这样当您使用本地终端时,$SSH_CONNECTION
环境变量将为空,因此code -w
将使用编辑器,但是当您通过 SSH 连接时,则$SSH_CONNECTION
环境变量将是一个非空字符串,因此vim
将使用编辑器。它是控制台编辑器,因此即使您通过 SSH 连接它也能工作。
回答by miqh
I opened up my .gitconfig
and amended it with:
我打开.gitconfig
并修改了它:
[core]
editor = 'C:/Users/miqid/AppData/Local/Code/app-0.1.0/Code.exe'
That did it for me (I'm on Windows 8).
那是为我做的(我在 Windows 8 上)。
However, I noticed that after I tried an arbitrary git commit
that in my Git Bashconsole I see the following message:
但是,我注意到git commit
在我的Git Bash控制台中尝试任意操作后,我看到以下消息:
[9168:0504/160114:INFO:renderer_main.cc(212)] Renderer process started
Unsure of what the ramifications of this might be.
不确定这可能会造成什么后果。
回答by ?afak Gür
GitPadsets your current text editor as the default editor for Git.
GitPad将您当前的文本编辑器设置为 Git 的默认编辑器。
My default editor for .txt
files in Windows 10 is Visual Studio Code and running GitPad once made it the default editor for Git. I haven't experienced the problems mentioned in the question (Git waits until VS Code window is closed in my case).
我.txt
在 Windows 10 中的默认文件编辑器是 Visual Studio Code,并且运行 GitPad 曾经使它成为 Git 的默认编辑器。我没有遇到问题中提到的问题(在我的情况下,Git 一直等到 VS Code 窗口关闭)。
(The link for the .exe
file didn't work for me, you may need to compile the source yourself.)
(该.exe
文件的链接对我不起作用,您可能需要自己编译源代码。)
回答by Wasif Hossain
Good news! At the time of writing, this feature has already been implemented in the 0.10.12-insidersrelease and carried out through 0.10.14-insiders. Hence we are going to have it in the upcoming version 1.0 Release of VS Code.
好消息!在撰写本文时,此功能已在0.10.12-insiders版本中实现并通过0.10.14-insiders 实现。因此,我们将在即将发布的 VS Code 1.0 版中使用它。
Implementation Ref: Implement -w/--wait command line arg
实施参考:实施 -w/--wait 命令行参数
回答by Victor Ma
Just want to add these back slashes to previous answers, I am on Windows 10 CMD, and it doesn't work without back slashes before the spaces.
只是想将这些反斜杠添加到以前的答案中,我在 Windows 10 CMD 上,如果空格前没有反斜杠,它就不起作用。
git config --global core.editor "C:\Users\your_user_name\AppData\Local\Programs\Microsoft\ VS\ Code\Code.exe"
回答by Mohammed
Run this command in your Mac Terminal app
在 Mac 终端应用程序中运行此命令
git config --global core.editor "/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
回答by LuisCarlos Rodriguez
on windows 10 using the 64bit insiders edition the command should be:
在使用 64 位内部人员版本的 Windows 10 上,命令应该是:
git config --global core.editor "'C:\Program Files\Microsoft VS Code Insiders\bin\code-insiders.cmd'"
git config --global core.editor "'C:\Program Files\Microsoft VS Code Insiders\bin\code-insiders.cmd'"
you can also rename the 'code-insiders.cmd' to 'code.cmd' in the 'Program Files' directory, in this way you can now use the command 'code .' to start editing the files on the . directory
您还可以将“Program Files”目录中的“code-insiders.cmd”重命名为“code.cmd”,这样您现在就可以使用命令“code”了。开始编辑 . 目录