How do I make a command in cygwin to run Sublime Text?

随声附和 提交于 2021-01-27 06:32:05

问题


I'm trying to mimic the subl command in iterm for mac computers in cygwin.

Basically, I want to be able to open a current folder from cygwin by typing subl .

I haven't found any good instructions. I know where my .bashrc file is located. I just dont know what to do to create the command subl and make it so that the path following subl opens with Sublime.

Can anyone help?


回答1:


You'd want to make an alias and source it from bashrc.

Example

Create a file ~/.bash_aliases with:

alias subl='/cygdrive/c/sublime.exe' #make sure this command is correct for you

Now in ~/.bashrc do:

source ~/.bash_aliases    

Log out and log back in, and subl . should work




回答2:


Assuming you want correct behaviour when doing subl ~, a simple alias or adding Sublime Text to your PATH will not work.

You can use the following bash function instead (put it in your .bashrc):

function subl {
    cygpath --windows $@ | sed 's/\\/\\\\/g' | tr '\n' '\0' | xargs -0 -n1 /cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe
}

Note that when passing paths to xargs you need to 1) escape the backslashes, 2) use the NUL character as argument delimiter for it to work with paths with spaces.

Edit: Use subl.exe rather than sublime_text3.exe so that it would detach itself from the terminal.




回答3:


To open files and use Git tools (commit, rebase, tag, ... messages):

#!/bin/bash
# To create in [.babun/]cygwin/usr/local/bin/subl with chmod +x

ARGS=""
while test $# -gt 0
do
  ARGS="$ARGS ${1#/cygdrive/[a-zA-Z]}"; # Remove /cygdrive and disk letter from the path
  shift
done

/cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe $ARGS

https://gist.github.com/cmalard/16c58869319c9a88473ec08cc7989c6b




回答4:


You can also simply add Sublime to your PATH variable. For cygwin, you can:

  1. Go to your home directory and verify .bash_profile exists with ls -a
  2. Open .bash_profile with something like vim .bash_profile
  3. add the line export PATH=$PATH:"/cygdrive/c/Program Files/Sublime Text 3" (or whatever top level installation folder Sublime is in)
  4. Reopen your terminal and use subl to verify it works


来源:https://stackoverflow.com/questions/32598753/how-do-i-make-a-command-in-cygwin-to-run-sublime-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!