5

Question:

git-bash.exe, or MINGW64, recognizes cd commands for directories whose addresses are given in form of: /Drive_Name/Folder1/Subfolder1. An example for changing to home dir for a Windows machine users is: 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.

  • Alternatively, is there a way to automatically change all the backward slashes into forward slashes when pasting into the Git-bash window?

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. It would be nice to quickly start a git-bash.exe process at the desired directory location. And, when on Windows OS, getting addresses in form of C:\users\USERNAME\project is the default.

Assortment of syntax that git-bash.exe likes:

  • Format 1: the format native to GNU --- /c/users/USERNAME;
  • Format 2: semi-Windows OS format ==> use forward slash as divider --- c:/users/USERNAME;
  • Format 3: semi-Windows OS format ==> use TWO backward slashes as divider --- c:\\users\\USERNAME (credits: @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>

    • Solution: set shellslash, see "tentative solution" for more details.
  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.

llinfeng
  • 1,316
  • 1
  • 15
  • 39
  • "Is it possible to use the following syntax? `cd c:\users\USERNAME`. Q3 2019, Git 2.23, yes, it is possible. See [my edited answer below](https://stackoverflow.com/a/45136283/6309) – VonC Oct 13 '19 at 15:12

3 Answers3

2

Alternative solution: using single quotes does seem to work. Example:

cd 'c:\users\USERNAME\Folder_1'

Source: Forward slash vs backward slash for file path in git bash

Alex
  • 21
  • 2
1

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:

cd c:\\users\\USERNAME

2 years later (Q3 2019), you don't even needs to escape the \, or to use single or double quotes:

  • It does work without anything for paths without space in them;
  • It works with single or double quotes for paths with spaces in them.

Example:

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>

For paths without 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       <=========

For paths with 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'
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Exactly, "normal" addresses that comes with a single backward slash ("`\`") in between is not permissible; at minimum as you've demonstrated, we need to use two of them together: the first backward slash serving as the escape character, and the second being the actual directory-divider. – llinfeng Jul 17 '17 at 09:32
1

Tentative solution through Vim

In .vimrc (or _vimrc for Windows), use setting: set shellslash, and use the following mapping to copy the addresses:

  • 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?

  • 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.

  2. Vim's native function expand("%:p") (with other flags) that can copy directory-info to clipboard.

llinfeng
  • 1,316
  • 1
  • 15
  • 39