简体   繁体   English

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

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

Question: 题:

git-bash.exe , or MINGW64 , recognizes cd commands for directories whose addresses are given in form of: /Drive_Name/Folder1/Subfolder1 . git-bash.exeMINGW64识别其地址以: /Drive_Name/Folder1/Subfolder1形式给出的目录的cd命令。 An example for changing to home dir for a Windows machine users is: cd /c/users/USERNAME . 更改为Windows计算机用户的主目录的示例是: cd /c/users/USERNAME

  • Is it possible to use the following syntax? 是否可以使用以下语法? cd c:\\users\\USERNAME Note, the directory address is given in its "native" format: the syntax for which the Windows OS recognizes. cd c:\\users\\USERNAME注意,目录地址以“本机”格式给出:Windows操作系统识别的语法。

  • Alternatively, is there a way to automatically change all the backward slashes into forward slashes when pasting into the Git-bash window? 或者,有没有办法在粘贴到Git-bash窗口时自动将所有反斜杠更改为正斜杠?

Background 背景

I am using git-bash.exe on a daily basis: to pull and push to Github, to run latexmk for compiling my *.tex documents and to SSH my Linux machines. 我每天都在使用git-bash.exe :拉动并推送到Github,运行latexmk来编译我的*.tex文件和SSH我的Linux机器。 It would be nice to quickly start a git-bash.exe process at the desired directory location. 在所需的目录位置快速启动git-bash.exe进程会很不错。 And, when on Windows OS, getting addresses in form of C:\\users\\USERNAME\\project is the default. 而且,在Windows操作系统上,以C:\\users\\USERNAME\\project形式获取地址是默认设置。

Assortment of syntax that git-bash.exe likes: git-bash.exe喜欢的语法分类:

  • Format 1: the format native to GNU --- /c/users/USERNAME ; 格式1:GNU原生格式--- /c/users/USERNAME ;
  • Format 2: semi-Windows OS format ==> use forward slash as divider --- c:/users/USERNAME ; 格式2:半Windows操作系统格式==>使用正斜杠作为分隔符--- c:/users/USERNAME ;
  • Format 3: semi-Windows OS format ==> use TWO backward slashes as divider --- c:\\\\users\\\\USERNAME (credits: @VonC ) 格式3:半Windows操作系统格式==>使用两个反斜杠作为分隔符--- c:\\\\users\\\\USERNAME (credit: @VonC

Alternatively (pending solutions) 或者(待定解决方案)

A valid alternative is to have some third-party process to monitor the clipboard, and switch all the backward backward slashes into forward slashes. 有效的替代方法是使用某些第三方进程来监视剪贴板,并将所有向后反斜杠切换为正斜杠。 In my case, this could happen: 就我而言,这可能发生:

  1. [Solved] In Vim , when I use the following mapping to fetch the "parent directory" of the file: nnoremap <leader><leader>p :let @* = expand("%:p:h")<CR> [解决]Vim中 ,当我使用以下映射来获取文件的“父目录”时: nnoremap <leader><leader>p :let @* = expand("%:p:h")<CR>

    • Solution: set shellslash , see "tentative solution" for more details. 解决方案: set shellslash ,有关详细信息,请参阅“暂定解决方案”。
  2. Though some clipboard-monitor/recording apps: to simply recognize that the string being assigned to the system clipboard is an address for a folder/directory, and replace all backward slashes to be forward slashes. 虽然一些剪贴板监视/录制应用程序:只是简单地识别分配给系统剪贴板的字符串是文件夹/目录的地址,并将所有反斜杠替换为正斜杠。

Tentative solution through Vim 通过Vim的初步解决方案

In .vimrc (or _vimrc for Windows), use setting: set shellslash , and use the following mapping to copy the addresses: .vimrc (或Windows的_vimrc )中,使用设置: set shellslash ,并使用以下映射来复制地址:

  • Absolute directory to the file: 文件的绝对目录:

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

  • Absolute directory to the parent folder: 父文件夹的绝对目录:

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

  • Ref: Is it possible to make vim use forward slashes on windows? 参考: 是否可以让vim在Windows上使用正斜杠?

  • Other mappings that are helpful: 其他有用的映射:

     " 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> 

Explanation 说明

What this solution builds on are: 该解决方案的基础是:

  1. git-bash.exe 's ability to recognize addresses of the following form: c:/users/USERNAME/document , where the forward slashes are used as divider. git-bash.exe能够识别以下格式的地址: c:/users/USERNAME/document ,其中正斜杠用作分隔符。

  2. Vim's native function expand("%:p") (with other flags) that can copy directory-info to clipboard. Vim的本机函数expand("%:p") (带有其他标志)可以将directory-info复制到剪贴板。

Is it possible to use the following syntax? 是否可以使用以下语法?

cd c:\users\USERNAME

I just tried with the latest Git 2.13.3 bash: it works, but I had to type: 我刚尝试使用最新的Git 2.13.3 bash:它可以工作,但我必须输入:

cd c:\\users\\USERNAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM