Trouble with getting the url from apm into the open command


#1

I’m was trying to write a script that would automatically open the the github page for an atom package from the command line. I think I’m getting the right string, except then when I try to give it to the open command something happens and my home directory is tacked on to the url. What am I missing here?

➜  ~  apm show vim-mode | grep https | cut -d' ' -f2
https://github.com/atom/vim-mode
➜  ~  apm show vim-mode | grep https | cut -d' ' -f2 | xargs -I{} open -a "Google Chrome" {}
The file /Users/max/https:/github.com/atom/vim-mode does not exist.
➜  ~  open -a "Google Chrome" https://github.com/atom/vim-mode

I’ve also tried from /tmp and it does look like open is trying to resolve the url as a path to my current working directory, any ideas for how to get around this?

➜  /tmp  apm show vim-mode | grep https | cut -d' ' -f2 | xargs -I{} open -a "Google Chrome" "{}"
The file /private/tmp/https:/github.com/atom/vim-mode does not exist.

#2

Do you have access to sed? If not, is there an equivalent tool for Mac? You’d just want to clip starting from https: to the end of the line.


#3

Thanks for your response!
The answer is to be aware that some commands are tacking on special characters to the output. Maybe it’s because of some zshsettings. grep -oE outputs colors which open seems to take in, ruby doesn’t put weird stuff in the output, sed, awk, and cut all seem to add special characters into the output for me.

Using some I found a terrific sed oneliner, sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" that takes care of the special characters so all everything works as expected now :slight_smile:

➜  ~  open -a "Google Chrome" $(apm show vim-mode | grep -oE 'https:.*' | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
➜  ~  open -a "Google Chrome" $(apm show vim-mode | grep http | ruby -ne 'puts /https:[\w\/\.-]+/.match($_).to_s')
➜  ~  open -a "Google Chrome" $(apm show vim-mode | grep http | awk -F' ' '{print $2}'|sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
➜  ~  open -a "Google Chrome" $(apm show vim-mode | grep http | cut -d' ' -f2 | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")

I’m using zim with the gitser theme, I suspect this might be the problem, but for now this seems like it will be enough for a quick way to get right to a package repo :slight_smile:


#4

Tangentially related question: I’m tempted to try zim, but I have a custom prompt set up that I really like. If I install it, will it assert its will on my prompt and make me work within its structures to fix it?


#5

It’s funny you ask that, I’ve been wanting to look into how to slightly modify the gitster prompt so it only shows the current working directory but I haven’t had time to look into it. I’ll take a look and get back to you :thumbsup: