从Git Bash调用notepad++

51
我正在Windows 7中使用msysgit。如何从Git Bash调用notepad++,就像我们使用默认的notepad一样?
例如:
name@usename notepad textfile.txt
相反,我想要用notepad++打开文件。
注意:我已将notepad++添加到我的PATH中,但仍然无法从命令行调用它。
编辑:
我在.gitconfig中尝试了这个——> [alias] notepad='C:/Program Files/Notepad++/notepad++.exe'
但没有起作用。

编写别名很困难,因为需要转义所有特殊字符——'program files'中的空格、'(x86)'中的括号以及'notepad++'中的加号。 - Colonel Panic
1
@ColonelPanic https://dev59.com/UHNA5IYBdhLWcg3wn_Q1 - Leponzo
15个回答

63

默认情况下,您不会有一个.bashrc文件,因此只需键入以下内容即可导航到您的主目录:

cd ~

使用vim(或您熟悉的任何编辑器)创建或编辑.bashrc文件:

vim .bashrc

这是我必须添加到我的代码中的一行(我正在运行64位操作系统,所以如果你不是,请不要完全复制)

alias notepad="/c/Program\ Files\ \(x86\)/Notepad++/notepad++.exe"

如果你的 Windows 是 32 位的,则应该看起来像这样

alias notepad="/c/Program\ Files/Notepad++/notepad++.exe"

最后,关闭并重新打开您的终端/ bash(或者,如注释所述,运行 source〜/ .bashrc ),然后就完成了!


2
哇,这真是一个改变游戏规则的东西!太好了!我使用了:alias open="/c/Program\ Files\/Notepad++/notepad++.exe" - Ryan
你确定吗?它仍然告诉我找不到命令。 - Bartis Áron
2
如果您尚未关闭并重新打开Bash客户端,则可能无法看到新添加的内容。这让我在进行其他脚本编写以打开VS解决方案时头痛了一段时间。 - Josh Gust
2
@JoshGust 或者你可以使用 source 命令。(source ~/.bashrc) - aderchox
Notepad++现在适用于32位和64位的Windows系统。因此,第二个片段也可以在64位操作系统中使用。 - Wolf

46

这些是实现目标的更快方法

start notepad++ 
start notepad++ <filename>
alias np='start notepad++'
np <filename>

经过尝试和测试,只需去做!


6

我为我的64位机器添加了32位Notepad++。

$ cd ~
$ vim .bash_profile

请将以下内容添加到文件中并保存:

64位系统

alias npp="/c/Program\ Files\ \(x86\)/Notepad++/notepad++.exe"

32位系统

alias npp="/c/Program\ Files/Notepad++/notepad++.exe"

现在您应该能够通过输入以下内容使用notepad++打开任何文件:
$ npp [file_name]

1
你认为这个需要添加到 .bashrc 文件中吗? - HattrickNZ
Notepad++的新版本是原生构建为64位系统的,因此默认安装到“/c/Program\ Files/Notepad++/notepad++.exe”。 - Jim Fell

5

我相信git-bash是一个真正的bash shell,所以它启动时会运行某个地方的.bashrc文件(很可能是你的主目录或git-bash开始的目录)。查找该文件,并在找到后在其中添加别名行以便使用notepad++:

alias notepad="/c/Program\ Files/Notepad++/notepad++.exe"

当然,请使用实际的Notepad++路径。


所有这些都是正确的,但很有可能他还没有.bashrc文件,因为他从未使用过它。请阅读我的评论或在您的用户主目录中创建一个.bashrc文件,并添加适当的应用程序路径。 - SageMage
你可以在双引号外使用单引号,例如:alias np='"C:\Program Files (x86)\Notepad++\notepad++.exe"' - chrispepper1989

4

@SageMage的回答非常准确。

提醒一下,如果您在.bashrc中进行更改,则需要关闭并重新打开GitBash才能激活更改。

附注:希望这篇文章对您有所帮助,两年后看到它了!


3
以下内容列于Udacity的Git和GitHub课程中。这对我很有帮助:
  1. Run the following command in Git Bash after checking the location of Notepad++ on your PC.

    echo 'alias npp="C:/Program\ Files\ \(x86\)/Notepad\+\+/notepad\+\+.exe"' >> ~/.bashrc
    

    Notice how I had to escape characters like the space and the brackets. You can escape any character if you're not sure whether it should be escaped or not. Also make sure to use the alias you want; I chose npp.

  2. Close and re-open Git Bash

  3. Type npp in Git Bash, if it opens then you're good to go. If not, try the below points:
  4. Test .bashrc by running the command below in Git Bash

    source ~/.bashrc
    
  5. Retry typing npp to start Notepad++. If Notepad++ doesn't start, check the contents of the file ~/.bashrc created in step 1.

  6. To ensure the .bashrc file contents are loaded each time you open Git Bash, edit ~/.bash_profile and add the following two lines. (Reference)

    if [ -r ~/.profile ]; then . ~/.profile; fi
    
    case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac
    
  7. Close and re-open Git Bash. Type npp in Git Bash to check it starts correctly.


我想补充一下@Amgad的答案,你可能还需要在括号周围添加转义字符! - bjpreisler

1
  1. 在您的系统/项目上打开Git Bash并在Git Bash中键入以下命令

    git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

  2. 按Enter键,如果没有错误发生,则已成功将Notepad ++设置为Git Bash中的文本编辑器。 您还可以通过键入以下命令进行检查

    git config --global core.editor

enter image description here


你解释了如何设置core.editor(这是不必要的),但没有说明如何从git bash打开/调用与要打开的文件相关联的应用程序,这可以通过键入“start filename”来实现,start将打开与文件扩展名相关联的程序。 - FiruzzZ

1

在您的.bash配置文件中添加以下内容:

别名 myeditor="'C:\\Program Files (x86)\\Notepad++\\notepad++.exe'"

请使用"\\\\"代替"\\"。


1

首先,如果您还没有创建任何 .bashrc 或 .bash_profile 文件,请使用 vim 或其他编辑器(如其他人所提到的)创建其中一个。

或者

如果您还没有任何可以与 git bash 一起使用的编辑器,请手动打开记事本或 notepad++ 编辑器,并将文件保存在主目录中。

注意:您可以使用以下方法检查主目录:

 cd ~

 pwd

我的Notepad++路径是C:\Program Files\Notepad++\notepad++.exe

因此,如果要进入任何目录到Notepad++目录,我必须先进入根目录,然后进入所需的路径。因此,这是我必须添加到我的.bash_profile文件中的行。

alias note="//\/c/Program\ Files/Notepad++/notepad++.exe"

'

'//'将其带到根目录

'

P.S.:

  • 您可能需要根据目标目录(Notepad++目录)更改路径。
  • “Program Files”目录应该写成“Program\ Files”。
  • 如果您的Notepad++目录在“Program Files(x86)”中,则使用“Program\ Files\(x86)”

0

我将Notepad++文件夹添加到了我的路径中,这样我就可以直接输入notepad++

$ which notepad++
/c/Program Files (x86)/Notepad++/notepad++

@Andi,那就说明git shell中使用的路径中没有包含notepad++。你是如何将其添加到路径中的? - Code-Apprentice

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接