DEV Community

loading...
Cover image for Open File Explorer and Browser from ZSH

Open File Explorer and Browser from ZSH

equiman profile image Camilo Martinez Updated on ・2 min read

Have you noticed that we can open a folder inside VSCode with code . command?

Will it be cool if we could do the same to open the file manager on the current folder or open the browser on a specific path from the terminal (or from VSCode)?

Well, it can be done by creating some functions and aliases 😎.

macOS

alias o="open ."
alias obp="open ./build/"
alias ocr="open ./coverage/lcov-report/index.html"
alias ogr="open $(git config remote.origin.url)"
alias onp="open \"https://www.npmjs.com/package/$(npm show ./ name --silent)\""
Enter fullscreen mode Exit fullscreen mode

Windows (WSL)

local OPEN="explorer.exe"
alias o="${OPEN} ."
alias obf="${OPEN} \".\\build\""
alias ocr="${OPEN} \".\\coverage\\lcov-report\\index.html\""
alias ogr="${OPEN} \"$(git config remote.origin.url)\""
alias onp="${OPEN} \"https://www.npmjs.com/package/$(npm show ./ name --silent)\""

onc() {
  local url="https://www.npmjs.com/package"
  local clipboard="$( powershell.exe -Command get-clipboard )"
  if [[ -n $clipboard ]]; then
    url="$url/$clipboard"
  fi
  ${OPEN} ${url}
}
Enter fullscreen mode Exit fullscreen mode

Ubuntu

local OPEN="xdg-open" # or "gnome-open" or "nautilus"
local BROWSER="googlechrome" # or "firefox" or etc...
alias o="${OPEN} ."
alias obf="${OPEN} ./build/"
alias ocr="${BROWSER} ./coverage/lcov-report/index.html"
alias ogr="${BROWSER} $(git config remote.origin.url)"
alias onp="${BROWSER} \"https://www.npmjs.com/package/$(npm show ./ name --silent)\""
Enter fullscreen mode Exit fullscreen mode

Now with those aliases, you will can:

Name Alias Action
Open o open current path on file explorer or finder
Open Build Folder obf open build folder inside the current folder on file explorer or finder
Open Coverage Report ocr open the coverage report on browser
Open Git Repo ogr open git repo URL on browser
Open NPM Package onp open npm package on browser
Open NPM Clipboard onc open npm package on browser searching by name on clipboard

That’s All Folks!
Happy Coding 🖖

Buy me a cofee

Discussion (2)

pic
Editor guide
Collapse
ronaldoperes profile image
Ronaldo Peres

In Windows you can use the 'start' command to open a folder, example:

c:\Users\someone>start c:\Projects

Also you can open directly a Visual Studio project with it like this:

c:\Users\someone>start c:\Projects\MyProject\MyProject.sln

And there is a lot more, just see the help for 'start'

Collapse
equiman profile image
Camilo Martinez Author • Edited

I've tested with start from WSL2 and doesn't work.

start .
zsh: command not found: start
Enter fullscreen mode Exit fullscreen mode

I think only works with the default Windows console.

I've changed the title to avoid confusion.