Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Created May 15, 2017 09:08
Show Gist options
  • Save louisswarren/7f0d1dd6c19d9c599f749b7ef1613708 to your computer and use it in GitHub Desktop.
Save louisswarren/7f0d1dd6c19d9c599f749b7ef1613708 to your computer and use it in GitHub Desktop.
Search for a file and open it with xdg-open
#!/usr/bin/env python3
import re
import subprocess
import sys
if len(sys.argv) < 2:
print("Usage: {} <findregex> [pythonregex]...")
sys.exit()
_, fregex, *refinements = sys.argv
find_call = ['find', '.', '-iregex', '.*{}.*'.format(fregex)]
files = subprocess.check_output(find_call).decode().split('\n')[:-1]
for refinement in refinements:
files = (f for f in files if re.search(refinement, f, re.IGNORECASE))
subprocess.call(['xdg-open', next(iter(files))])
# Example:
# open ftplugin python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment