在 Git-Bash 中更改目录:在 Windows 机器上使用 MINGW64 中的 Windows 样式目录地址

Changing directory in Git-Bash: using Windows-style directory addresses in MINGW64 on Windows machines

问题:

git-bash.exeMINGW64 识别地址以 /Drive_Name/Folder1/Subfolder1 形式给出的目录的 cd 命令。 Windows 机器用户更改主目录的示例是:cd /c/users/USERNAME.

背景

我每天都在使用 git-bash.exe:拉和推到 Github,到 运行 latexmk 来编译我的 *.tex 文档并 SSH 我的 Linux 机器。最好在所需目录位置快速启动 git-bash.exe 进程。并且,在 Windows OS 上,默认以 C:\users\USERNAME\project 形式获取地址。

git-bash.exe喜欢的语法分类:

或者(待定解决方案)

一个有效的替代方法是使用一些第三方进程来监视剪贴板,并将所有向后的反斜杠切换为正斜杠。就我而言,这可能会发生:

  1. [已解决]Vim 中,当我使用以下映射获取 "parent directory" 文件:nnoremap <leader><leader>p :let @* = expand("%:p:h")<CR>

    • 解决方法:set shellslash,详见"tentative solution"。
  2. 虽然有些 clipboard-monitor/recording 应用程序:简单地识别分配给系统剪贴板的字符串是 folder/directory 的地址,并将所有反斜杠替换为正斜杠斜杠.

Is it possible to use the following syntax?

cd c:\users\USERNAME

我刚刚尝试使用最新的 Git 2.13.3 bash:它有效,但我必须输入:

cd c:\users\USERNAME

2 年后(2019 年第 3 季度),您甚至不需要转义 \,或使用单引号或双引号:

  • 对于没有 space 的路径,没有 任何东西都有效;
  • 它对包含 space 的路径使用单引号或双引号。

示例:

vonc@voncav MINGW64 /c/Users
$ git version
git version 2.23.0.windows.1

vonc@voncav MINGW64 /c/Users
$ bash --version
GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

没有space的路径:

vonc@voncav MINGW64 /d/git
$ cd "c:\Users"

vonc@voncav MINGW64 /c/Users
$ cd 'D:\git'

vonc@voncav MINGW64 /c/Users
$ cd D:\git                      <=========

vonc@voncav MINGW64 /d/git       <=========

对于具有 spaces 的路径:

vonc@voncav MINGW64 /d/git
$ cd C:\Program Files
bash: cd: too many arguments

vonc@voncav MINGW64 /d/git
$ cd "C:\Program Files"

vonc@voncav MINGW64 /d/git
$ cd 'C:\Program Files'

通过Vim

初步解决

.vimrc(或_vimrc for Windows)中,使用设置:set shellslash,并使用以下映射复制地址:

  • 文件的绝对目录:

    nnoremap <leader><leader>f :let @* = expand("%:p")<CR>

  • 父文件夹的绝对目录:

    nnoremap <leader><leader>p :let @* = expand("%:p:h")<CR>

  • 参考:Is it possible to make vim use forward slashes on windows?

  • 其他有用的映射:

    " File Name only, without extension
    nnoremap <leader><leader>n :let @* = expand("%:t:r")<CR>
    " Extension.
    nnoremap <leader><leader>e :let @* = expand("%:t:e")<CR>
    " Copy the line number with file name
    nnoremap <leader><leader>l :let @* = expand("%:p").":".line(".")<CR>
    

说明

此解决方案的基础是:

  1. git-bash.exe 能够识别以下形式的地址:c:/users/USERNAME/document,其中 正斜杠 用作分隔符.

  2. Vim 的本机函数 expand("%:p")(带有其他标志)可以将目录信息复制到剪贴板。

替代解决方案:使用单引号 似乎有效。示例:

cd 'c:\users\USERNAME\Folder_1'

来源: