暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Windows 下 git-bash 常用 alias

生有可恋 2024-05-06
366

Windows 下的 git-bash 可以模拟大部分 linux 命令,基本上可以将 git-bash 当成一个简版的 linux 子系统使用。配合 alias 和 function 可以进行许多系统改进。

比如显示当前文件绝对路径:

    lsw 显示文件绝对路径
    lsww 显示文件 windows 路径
    sedw 将带盘符的 linux 路径替换成 windows 路径
    复制

      bash$ type lsw
      lsw is a function
      lsw ()
      {
      for i in $@;
      do
      ls -F --color=auto --show-control-chars `pwd`/$i;
      done
      }
      bash$ type lsww
      lsww is a function
      lsww ()
      {
      for i in $@;
      do
      ls -F --color=auto --show-control-chars `pwd`/$i | sedw;
      done
      }
      bash$ type sedw
      sedw is a function
      sedw ()
      {
      sed -e 's#^.##' -e 's#^.#\U&:#' -e 's#/#\\#g' $*
      }


      复制

      第一个 bash 函数 lsw 比较好理解,使用了 `pwd` 来获取当前目录路径。函数 sedw 不好理解,它使用了 sed 的多重编辑功能。第一段是删除第一个字符,第二段是将盘符改为大写并加上冒号,第三段是将斜杠替换为反斜杠。

      简单的命令包装使用 alias,复杂的命令特别是含单引号双引号的,使用函数会比较方便。

      在 git-bash 下调用 windows 下的命令会出现乱码。可以使用 iconv 工具进行编码转换。当使用 \ipconfig 时调用的是原生命令,不会调用 alias

        bash$ \ipconfig


        Windows IP Configuration




        Ethernet adapter ▒▒̫▒▒ 2:


        Connection-specific DNS Suffix . :
        IPv4 Address. . . . . . . . . . . : 10.20.98.201
        Subnet Mask . . . . . . . . . . . : 255.255.0.0
        Default Gateway . . . . . . . . . : 10.20.0.1
        bash$ type ipconfig
        ipconfig is aliased to `ipconfig|iiconv'
        bash$ type iiconv
        iiconv is aliased to `iconv -f GBK -t UTF-8'


        复制

        在git-bash下打开 windows 的文件浏览器,可以使用 windows explorer 命令。反之 git-bash 下访问 windows 路径可以使用双引号将路径扩起来然后 cd 进去。这样就能快速在 windows 和 linu 之间切换了。

        在 git-bash 下想启动一个新的 git-bash 窗口,可以这样包装一下:

          $ alias gsh
          alias gsh='/c/bin/git_shell.sh&'
          $ cat c/bin/git_shell.sh
          #!/bin/bash


          exec "C:\Program Files\Git\git-bash.exe"


          复制

          执行 alias 可以输出所有的 git-bash 下的命令别名,使用 declear -f 可以输出 git-bash 下所有的函数。

            $ declear -f
            pwdw ()
            {
            pwd | sed -e 's#^.\(.\)#\1#' -e 's#^.#\U&:#' -e 's#/#\\#g'
            }
            sedw ()
            {
            sed -e 's#^.##' -e 's#^.#\U&:#' -e 's#/#\\#g' $*
            }
            upenv ()
            {
            source ~/.bashrc
            }
            proxy ()
            {
            export http_proxy=http://127.0.0.1:7890;
            export https_proxy=http://127.0.0.1:7890
            }
            unproxy ()
            {
            unset http_proxy;
            unset https_proxy
            }
            ls_path ()
            {
            echo $PATH | sed 's/:/\n/g'
            }
            复制

            文章转载自生有可恋,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

            评论