aboutsummaryrefslogtreecommitdiff
path: root/bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-09-08 11:34:29 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-09-08 11:34:29 +1200
commitf9f70099edcadd2d7bf93ddf2fc91b2b0ae993e0 (patch)
tree46a4b4f7d66b9922628fca7b0bc0a0c01b87f9ba /bash
parentFix an issue (silencing Git prompt errors) (diff)
downloaddotfiles-f9f70099edcadd2d7bf93ddf2fc91b2b0ae993e0.tar.gz
dotfiles-f9f70099edcadd2d7bf93ddf2fc91b2b0ae993e0.zip
Add text-filename heuristic completion
Filesystem type and filename extension-based to keep things quick and simple and not forking out to other tools. We'll see how well this works, but so far I really like it.
Diffstat (limited to 'bash')
-rw-r--r--bash/bash_completion.d/_text_filenames.bash61
-rw-r--r--bash/bash_completion.d/awk.bash5
-rw-r--r--bash/bash_completion.d/cat.bash5
-rw-r--r--bash/bash_completion.d/ed.bash5
-rw-r--r--bash/bash_completion.d/ex.bash5
-rw-r--r--bash/bash_completion.d/grep.bash5
-rw-r--r--bash/bash_completion.d/head.bash5
-rw-r--r--bash/bash_completion.d/m4.bash5
-rw-r--r--bash/bash_completion.d/sed.bash5
-rw-r--r--bash/bash_completion.d/source.bash5
-rw-r--r--bash/bash_completion.d/tail.bash5
-rw-r--r--bash/bash_completion.d/vi.bash5
-rw-r--r--bash/bash_completion.d/view.bash5
-rw-r--r--bash/bash_completion.d/vim.bash5
14 files changed, 126 insertions, 0 deletions
diff --git a/bash/bash_completion.d/_text_filenames.bash b/bash/bash_completion.d/_text_filenames.bash
new file mode 100644
index 00000000..d12dce7f
--- /dev/null
+++ b/bash/bash_completion.d/_text_filenames.bash
@@ -0,0 +1,61 @@
+# Exclude files by filesystem type and extension that likely aren't
+# viewable/editable in plain text
+#
+# I've seen some very clever people figure out ways to actually read the files
+# or run something like file(1) over them to make an educated guess as to
+# whether they're binary or not, but I don't really want to go that far. It's
+# not supposed to be perfect, just a bit more likely to complete singly with
+# the thing I want, and I want it to stay fast.
+#
+_text_filenames() {
+ while IFS= read -r item ; do
+
+ # Exclude blanks
+ [[ -n $item ]] || return
+
+ # Exclude files with block, character, pipe, or socket type
+ [[ ! -b $item ]] || return
+ [[ ! -c $item ]] || return
+ [[ ! -p $item ]] || return
+ [[ ! -S $item ]] || return
+
+ # Check the filename extension to know what to exclude
+ case $item in
+
+ # Binary image file formats
+ *.bmp|*.gif|*.jpeg|*.jpg|*.png|*.xcf) ;;
+ *.BMP|*.GIF|*.JPEG|*.JPG|*.PNG|*.XCF) ;;
+
+ # Video file formats
+ *.avi|*.gifv|*.mkv|*.mov|*.mpg|*.rm|*.webm) ;;
+ *.AVI|*.gifv|*.MKV|*.MOV|*.MPG|*.RM|*.WEBM) ;;
+
+ # Audio file formats
+ *.au|*.aup|*.flac|*.mid|*.h2song|*.mp[34]|*.ogg|*.wav) ;;
+ *.AU|*.AUP|*.FLAC|*.MID|*.H2SONG|*.MP[34]|*.OGG|*.WAV) ;;
+
+ # Document formats
+ *.cbr|*.doc|*.docx|*.odp|*.odt|*.pdf|*.xls|*.xlsx) ;;
+ *.CBR|*.DOC|*.DOCX|*.ODP|*.ODT|*.PDF|*.XLS|*.XLSX) ;;
+
+ # Compressed/archived file formats
+ # (Yes I know Vim can read these)
+ *.bz2|*.deb|*.gz|*.tar|*.xz|*.zip) ;;
+ *.BZ2|*.DEB|*.GZ|*.TAR|*.XZ|*.ZIP) ;;
+
+ # Known binary extensions
+ # (I haven't included .com; on UNIX, that's more likely to be
+ # something I saved from a website and named after the domain)
+ *.a|*.dat|*.drv|*.exe|*.o) ;;
+ *.A|*.DAT|*.DRV|*.EXE|*.O) ;;
+
+ # Filesystems
+ *.bin|*.cue|*.img|*.iso|*.raw) ;;
+ *.BIN|*.CUE|*.IMG|*.ISO|*.RAW) ;;
+
+ # Complete everything else; some of it will still be binary
+ *) COMPREPLY[${#COMPREPLY[@]}]=$item ;;
+
+ esac
+ done < <(compgen -A file -- "${COMP_WORDS[COMP_CWORD]}")
+}
diff --git a/bash/bash_completion.d/awk.bash b/bash/bash_completion.d/awk.bash
new file mode 100644
index 00000000..6c6f4b72
--- /dev/null
+++ b/bash/bash_completion.d/awk.bash
@@ -0,0 +1,5 @@
+# Completion for awk(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames awk
+
diff --git a/bash/bash_completion.d/cat.bash b/bash/bash_completion.d/cat.bash
new file mode 100644
index 00000000..894b18ea
--- /dev/null
+++ b/bash/bash_completion.d/cat.bash
@@ -0,0 +1,5 @@
+# Completion for cat(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames cat
+
diff --git a/bash/bash_completion.d/ed.bash b/bash/bash_completion.d/ed.bash
new file mode 100644
index 00000000..9bc2bf59
--- /dev/null
+++ b/bash/bash_completion.d/ed.bash
@@ -0,0 +1,5 @@
+# Completion for ed(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames ed
+
diff --git a/bash/bash_completion.d/ex.bash b/bash/bash_completion.d/ex.bash
new file mode 100644
index 00000000..6e2ac9fb
--- /dev/null
+++ b/bash/bash_completion.d/ex.bash
@@ -0,0 +1,5 @@
+# Completion for ex(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames ex
+
diff --git a/bash/bash_completion.d/grep.bash b/bash/bash_completion.d/grep.bash
new file mode 100644
index 00000000..dec9f7d3
--- /dev/null
+++ b/bash/bash_completion.d/grep.bash
@@ -0,0 +1,5 @@
+# Completion for grep(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames grep
+
diff --git a/bash/bash_completion.d/head.bash b/bash/bash_completion.d/head.bash
new file mode 100644
index 00000000..65a32628
--- /dev/null
+++ b/bash/bash_completion.d/head.bash
@@ -0,0 +1,5 @@
+# Completion for head(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames head
+
diff --git a/bash/bash_completion.d/m4.bash b/bash/bash_completion.d/m4.bash
new file mode 100644
index 00000000..4cf27e63
--- /dev/null
+++ b/bash/bash_completion.d/m4.bash
@@ -0,0 +1,5 @@
+# Completion for m4(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames m4
+
diff --git a/bash/bash_completion.d/sed.bash b/bash/bash_completion.d/sed.bash
new file mode 100644
index 00000000..4e27d34f
--- /dev/null
+++ b/bash/bash_completion.d/sed.bash
@@ -0,0 +1,5 @@
+# Completion for sed(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames sed
+
diff --git a/bash/bash_completion.d/source.bash b/bash/bash_completion.d/source.bash
new file mode 100644
index 00000000..3dba710f
--- /dev/null
+++ b/bash/bash_completion.d/source.bash
@@ -0,0 +1,5 @@
+# Completion for source(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames source
+
diff --git a/bash/bash_completion.d/tail.bash b/bash/bash_completion.d/tail.bash
new file mode 100644
index 00000000..e72d0d8d
--- /dev/null
+++ b/bash/bash_completion.d/tail.bash
@@ -0,0 +1,5 @@
+# Completion for tail(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames tail
+
diff --git a/bash/bash_completion.d/vi.bash b/bash/bash_completion.d/vi.bash
new file mode 100644
index 00000000..8e385318
--- /dev/null
+++ b/bash/bash_completion.d/vi.bash
@@ -0,0 +1,5 @@
+# Completion for vi(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames vi
+
diff --git a/bash/bash_completion.d/view.bash b/bash/bash_completion.d/view.bash
new file mode 100644
index 00000000..ce381ecf
--- /dev/null
+++ b/bash/bash_completion.d/view.bash
@@ -0,0 +1,5 @@
+# Completion for view(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames view
+
diff --git a/bash/bash_completion.d/vim.bash b/bash/bash_completion.d/vim.bash
new file mode 100644
index 00000000..4d3355aa
--- /dev/null
+++ b/bash/bash_completion.d/vim.bash
@@ -0,0 +1,5 @@
+# Completion for vim(1) with files that look editable
+declare -F _text_filenames >/dev/null ||
+ source "$HOME"/.bash_completion.d/_text_filenames.bash
+complete -F _text_filenames -o filenames vim
+