如何让 Sublime Text 成为 Git 的默认编辑器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8951275/
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 can I make Sublime Text the default editor for Git?
提问by Spencer Moran
I have a problem setting Sublime Text 2 as the core.editor
with git
.
我在将 Sublime Text 2 设置为core.editor
with 时遇到问题git
。
I've read through every post I could find addressing the problem, but still nothing is working for me. I am running Windows.
我已经通读了我能找到的所有解决问题的帖子,但仍然没有任何内容对我有用。我正在运行 Windows。
I have done:
我已经做好了:
git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe'"
and tried that with various arguments like -m
. When I open my .gitconfig
, this is what is in there:
并尝试使用各种参数,例如-m
. 当我打开我的.gitconfig
,这是里面的内容:
[user]
name = Spencer Moran
email = [email protected]
[core]
editor = 'C:/Program Files/Sublime Text 2/sublime_text.exe'
If I go to Git and type:
如果我去 Git 并输入:
README.markdown --edit
the README file opens in Notepad, not Sublime Text.
README 文件在记事本中打开,而不是 Sublime Text。
Does anyone have any idea what I'm doing wrong or how I could fix this?
有谁知道我做错了什么或者我如何解决这个问题?
回答by jrotello
Windows
视窗
Sublime Text 2 (Build 2181)
Sublime Text 2(内部版本 2181)
The latest Build 2181just added support for the -w
(wait) command line argument. The following configuration will allow ST2 to work as your default git editor on Windows. This will allow git to open ST2 for commit messages and such.
最新的Build 2181刚刚添加了对-w
(wait) 命令行参数的支持。以下配置将允许 ST2 作为您在 Windows 上的默认 git 编辑器。这将允许 git 为提交消息等打开 ST2。
git config --global core.editor "'c:/program files/sublime text 2/sublime_text.exe' -w"
Sublime Text 3 (Build 3065)
Sublime Text 3(内部版本 3065)
Sublime Text 3(Build 3065) added the subl.exe
command line helper. Use subl.exe -h
for the options available to you. I have hot_exit: true
and remember_open_files: true
set in my Sublime Text user settings. I have found the following to git config to work well for me.
Sublime Text 3(Build 3065) 添加了subl.exe
命令行助手。使用subl.exe -h
用于提供给您的选项。我hot_exit: true
和remember_open_files: true
我的崇高文字使用者设定。我发现以下 git config 对我来说很有效。
git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"
Mac and Linux
Mac 和 Linux
Set Sublime as your editor for Git by typing the following command in the terminal:
通过在终端中键入以下命令,将 Sublime 设置为 Git 编辑器:
git config --global core.editor "subl -n -w"
git config --global core.editor "subl -n -w"
With this Git config, the new tab is opened in my editor. I edit my commit message, save the tab (Ctrl+S) and close it (Ctrl+W).
使用此 Git 配置,新选项卡将在我的编辑器中打开。我编辑我的提交消息,保存选项卡 ( Ctrl+ S) 并关闭它 ( Ctrl+ W)。
Git will wait until the tab is closed to continue its work.
Git 将等到选项卡关闭以继续其工作。
回答by sfufoet
You can use this command on Mac and Linux:
你可以在 Mac 和 Linux 上使用这个命令:
git config --global core.editor "subl -n -w"
回答by Ahmad Awais
For Mac OS X in the file ~/.gitconfig under [core] I had to put this code to solve the issue on my end.
对于 Mac OS X,在 [core] 下的 ~/.gitconfig 文件中,我不得不把这段代码放在我这边来解决这个问题。
editor = /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -w
This was when subl was working fine but git was unable to access it. And was displaying these errors
这是 subl 工作正常但 git 无法访问它的时候。并显示这些错误
subl -n -w: subl: command not found
error: There was a problem with the editor 'subl -n -w'.
回答by nu-bo
For Mac& Sublime Text 3:
对于Mac和Sublime Text 3:
If git config --global core.editor "subl -n -w"
doesn't work, use this git config
command:
如果git config --global core.editor "subl -n -w"
不起作用,请使用以下git config
命令:
$ git config --global core.editor "'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl' -n -w"
Note: it does notcontain escaping backslashes.
注意:它不包含逃避反斜杠。
回答by meeech
what worked for me in cygwin/zsh:
在 cygwin/zsh 中对我有用的内容:
in /usr/local/bin create subl_git file
在 /usr/local/bin 创建 subl_git 文件
#!/bin/bash
/cygdrive/c/Program\ Files/Sublime\ Text\ 2/sublime_text.exe -w -n `cygpath -w $@`
in gitconfig:
在 gitconfig 中:
editor = /usr/local/bin/subl_git
回答by Francois
With Sublime Text 2 and the following configuration, if nothing happens when you close the git commit's comment file :
使用 Sublime Text 2 和以下配置,如果关闭 git commit 的注释文件时没有任何反应:
git config --global core.editor "'c:/program files/sublime text 2/sublime_text.exe' -w"
If the git commit doesn't finish when you close the COMMIT_EDITMSG comment file in Sublime, I've found this is probably because you are running CMD in administrator mode (and have a Sublime Text already opened in non admin mode). I had the same problem and it worked for me when I startedmy CMD in normal mode.
如果在 Sublime 中关闭 COMMIT_EDITMSG 注释文件时 git commit 没有完成,我发现这可能是因为您在管理员模式下运行 CMD(并且已经在非管理员模式下打开了一个 Sublime Text)。我遇到了同样的问题,当我以正常模式启动CMD时,它对我有用。
Ps : just wanted to answer to the problem @david pointed out in the accepted answer's comments, but I couldn't post comment since I'm new here... so I've made a new answer, because I think this can be useful to some people encountering the problem.
Ps:只是想回答@david 在接受的答案的评论中指出的问题,但由于我是新来的,所以我无法发表评论......所以我做了一个新的答案,因为我认为这可能是对一些遇到问题的人有用。
回答by rwinzhang
I just successfully made it with the help of PowerShell (command line tools for windows, which is also be use as the default command line on GithubforWindows)
我刚刚在 PowerShell(Windows 的命令行工具,也被用作 GithubforWindows 上的默认命令行)的帮助下成功完成了它
Just make a folder "WindowsPowerShell" on "C:\Users\%username%\My Documents". Then, create a file called "profile.ps1". Make sure that you have the correct file extension, .ps1 and not .txt. inside the profile.ps1, put this line to set an alias command:
只需在“C:\Users\%username%\My Documents”上创建一个文件夹“WindowsPowerShell”。然后,创建一个名为“profile.ps1”的文件。确保您拥有正确的文件扩展名,.ps1 而不是 .txt。在 profile.ps1 中,放置这一行来设置别名命令:
Set-Alias subl 'c:\program files\sublime text 2\sublime_text.exe'
Make sure the sublime directory on your machine is correct. You can also see the configuration details in here.
确保你机器上的 sublime 目录是正确的。您还可以在此处查看配置详细信息。
Now, I can edit files with Sublime Text 2 via PowerShell command just by typing:
现在,我可以通过 PowerShell 命令使用 Sublime Text 2 编辑文件,只需键入:
subl filename
Hopefully this is usefull...
希望这是有用的...
回答by Diego Buendia
I've faced the same problem with git editor in Windowsthis evening, and finally I've got it.
今天晚上我在 Windows 中使用git 编辑器遇到了同样的问题,最后我得到了它。
You'll need to set your editor path with the bash syntax and using the DOS 8.3 format to avoid spaces:
您需要使用 bash 语法并使用 DOS 8.3 格式设置编辑器路径以避免空格:
This doesn't work (for me):
这不起作用(对我而言):
git config --global core.editor "C://Program files/Sublime Text/sublime_text.exe"
This do:
这样做:
git config --global core.editor /C/PROGRA~1/SUBLIME~1/sublime_text.exe
You can get the DOS 8.3 name for a directory with dir /x
您可以使用dir /x获取目录的 DOS 8.3 名称
回答by bvmCoder
For MAC Users I can say for Sublime use the following:
对于 MAC 用户,我可以说 Sublime 使用以下内容:
First Check the .gitconfig file using command:
首先使用命令检查 .gitconfig 文件:
cd ~
ls -la
cat .gitconfig
cd ~
ls -la
cat .gitconfig
Now one can add this configuration:
现在可以添加此配置:
git config --global core.editor "subl -n -wl1"
Where l1
will tell to start at line 1.
哪里l1
会告诉从第 1 行开始。
回答by justin.m.chase
I was just struggling with this for some reason, none of the examples above quite worked for me. This was my solution however:
出于某种原因,我只是在为此苦苦挣扎,上面的例子都没有对我有用。然而,这是我的解决方案:
git config --global core.editor "'c:/<path to sublime>/sublime_text.exe' -w $*"
You specifically need the ' '
around the path and the $*
at the end of the args, all in " "
.
您特别需要' '
路径周围和$*
参数末尾的" "
.