Skip to content

Instantly share code, notes, and snippets.

@leahcim
Created July 9, 2014 18:34
Show Gist options
  • Save leahcim/ba84686f41d2daa6316b to your computer and use it in GitHub Desktop.
Save leahcim/ba84686f41d2daa6316b to your computer and use it in GitHub Desktop.
Launch Notepad++ from the shell while automatically detecting file type for syntax highlighting, even if file extension is missing.
#!/bin/bash
# 9th July 2014
lang() {
lang=
if [ -f "$1" ]; then
# keep reading until $line non empty
# ':' means "Do Nothing"
while read line && [ -z "$line" ]; do :; done < "$1"
case $line in
# if shebang present, read language from it
\#\!/*)
#!/usr/bin/python => python
#!/usr/bin/env python => python
#!/usr/bin/env python -c => python
lang=$(echo "$line" | sed 's#.*/##;s/\s\+-.*//;s/.*\s//');
if [ "$lang" == "sh" ]; then
lang=bash
fi
;;
# otherwise, if '<' and '>' present, it's xml
*\<*\>*)
lang=xml
;;
%YAML*)
lang=yaml
;;
*)
lang=
;;
esac
fi
echo $lang
}
LANG=$(lang "$1")
if [ -n "$LANG" ]; then
L=-l$LANG
else
L=
fi
cmd.exe //c start "" "C:\Program Files\Notepad++\notepad++.exe" $L "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment