Open a doc from terminal, but not by its name
You probably haven't discovered Tab-completion (see here) yet.
While typing a filename in Terminal just type a first few letters and hit Tab and see magic!
Just for fun, literally answering the question:
#!/usr/bin/env python3
import os
import subprocess
show_hidden = False
currfiles = os.listdir("./")
if not show_hidden:
currfiles = [f for f in currfiles if not f.startswith(".")]
n = 1
for f in currfiles:
print(str(n) + ". " + f)
n = n + 1
picked = int(input("Quick, quick, give me a number:\n"))
subprocess.run(["xdg-open", currfiles[picked - 1]])
How it works in practice
- In terminal, in the working dir, run "o" (as a command)
-
The content of the current directory is listed, numbered. Pick the number and the item is opened:
Set up
...is easy:
- Create, if it doesn't exist yet, a folder named "bin" in your home directory
- Copy the script into an empty file, save it as (literally) "o" (no extension), and make it executable
-
Log out and back in and start using the command by just typing
$ o
in terminal
N.B.
If you'd like to show hidden files as well, change
show_hidden = False
into:
show_hidden = True
There is a little-known feature in Bash that allows you to do this without calling on python or any other third-party tool, and with a single line:
select file in *; do open "$file"; break; done