Separate aliases and variables.
[profile.git] / .profile.d / grep.bashrc
diff --git a/.profile.d/grep.bashrc b/.profile.d/grep.bashrc
new file mode 100644 (file)
index 0000000..0872828
--- /dev/null
@@ -0,0 +1,19 @@
+# Use colours for grep if possible.
+if grep -V 2>/dev/null | grep GNU >/dev/null; then
+  alias grep='grep --color=auto'
+
+  # Old versions of grep understand GREP_COLOR.
+  export GREP_COLOR='0;33'
+
+  # Newer versions allow setting colours based on context, using GREP_COLORS.
+  # cx: Lines of context; sl: Matching lines; mt: Search string; fn: Filename
+  # ln: Line number; bn: Byte offset; se: Separators.
+  colours=$(tput colors 2>/dev/null)
+  if [ $colours = 256 ]; then
+    export GREP_COLORS='cx=38;5;246:sl=:mt=33:fn=38;5;25:ln=38;5;82:bn=33:se=38;5;246'
+  elif [ $colours -gt 88 ]; then
+    export GREP_COLORS='cx=38;5;84:sl=:mt=33:fn=38;5;22:ln=38;5;44:bn=33:se=38;5;84'
+  else
+    export GREP_COLORS='cx=31:sl=:mt=33:fn=36:ln=34:bn=33:se=31'
+  fi
+fi