'synctags' shell function

4 views
Skip to first unread message

D Gowers

unread,
Jul 22, 2015, 8:30:14 AM7/22/15
to tm...@googlegroups.com
Sometimes you want to tag a file, but the format it is stored in is an editor format, not one that is easy to inspect.
If you can convert that to a more friendly format (eg. .ORA or .XCF -> .PNG or .JPG), then synctags can help with tagging it.

Basically, you tag your 'easy to examine' file foo.png, then you can run synctags on it, which will mirror those tags to the 'difficult to examine' file foo.ora.

The way it works is simply to remove the extension and use the filename + '.*' as a glob pattern; it examines each set of files with the same base name, and ensures that they are all tagged with the union of all the tags applied to files in the set.

This is slightly redundant but makes tagging of difficult file formats much easier.

The function follows:



function synctags() {
  if [ -z "$1" ]; then
    echo "synctags <file> [file [file...]]:
Synchronize tags between FILE_WITH_EXT_REMOVED.*"
  fi
  for V; do
    NOEXT=$(sed -E 's/\.[^.]+$//' <<< "$V")
    declare -a ALLFILES
    ALLFILES=("$NOEXT".*)
    if [ ${#ALLFILES[@]} -eq 1 ]; then
      echo 'skipping $V, only one candidate'
      continue
    fi
    echo "ALLFILES=$ALLFILES"
    TAGS=$(tmsu tags $ALLFILES | sed -E 's/^.+: ?//g' | tr ' ' \\n | sort -u | tr \\n ' '|sed -e 's/^ //' -e 's/ $//')
    echo "TAGS=$TAGS"
    tmsu tag --tags="$TAGS" "${ALLFILES[@]}"
    echo "$ALLFILES"
  done;
}

D Gowers

unread,
Jul 22, 2015, 11:57:58 PM7/22/15
to tm...@googlegroups.com
Slight update, I was using the wrong quoting style to report skipping, so non-informative messages were shown.


function synctags() {
  if [ -z "$1" ]; then
    echo "synctags <file> [file [file...]]:
Synchronize tags between FILE_WITH_EXT_REMOVED.*"
  fi
  for V; do
    NOEXT=$(sed -E 's/\.[^.]+$//' <<< "$V")
    declare -a ALLFILES
    ALLFILES=("$NOEXT".*)
    if [ ${#ALLFILES[@]} -eq 1 ]; then
      echo "skipping $V, only one candidate"
      continue
    fi
    echo "ALLFILES=$ALLFILES"
    TAGS=$(tmsu tags $ALLFILES | sed -E 's/^.+: ?//g' | tr ' ' \\n | sort -u | tr \\n ' '|sed -e 's/^ //' -e 's/ $//')
    echo "TAGS=$TAGS"
    tmsu tag --tags="$TAGS" "${ALLFILES[@]}"
    echo "$ALLFILES"
  done;

Reply all
Reply to author
Forward
0 new messages