In TextMate (or Sublime) how can I get the path relative my project directory?

221 Views Asked by At

I've got the absolute path available in commands via $TM_FILEPATH. How can I write a command that gives me a relative path, specifically i'd like to copy this path to enable sharing and ease some git/bash operations?

1

There are 1 best solutions below

0
Graham P Heath On

pbcopy will place a string onto your clipboard. From there, you just need to do some detect and replacement.

Start with the standard bash Command TextMate boilerplate:

#!/usr/bin/env bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"

Then check if the $TM_PROJECT_DIRECTORY exists:

if [ -z ${TM_PROJECT_DIRECTORY+x} ]; then

If not, copy the full path:

  echo -n "$TM_FILEPATH" | pbcopy
  echo "$TM_FILEPATH"'\n Absolute path copied to clipboard'

If so replace the project path in the file path, and copy;

else
  TM_PROJECT_DIRECTORY="$TM_PROJECT_DIRECTORY/"
  echo -n "${TM_FILEPATH#$TM_PROJECT_DIRECTORY}" | pbcopy
  echo "${TM_FILEPATH#$TM_PROJECT_DIRECTORY}"'\n Project relative path copied to clipboard.'
fi;

Please see this gist incase of updates.