Select and Edit a File in the Current Directory

PS3="Enter a number: "; select f in *;do $EDITOR $f; break; done
This command displays a simple menu of file names in the current directory. After the user made a choice, the command invokes the default editor to edit that file. * Without the break statement, the select command will loop forever * Setting the PS3 prompt is optional * If the user types an invalid choice (such as the letter q), then the variable $f will become an empty string. * For more information, look up the bash's select command
Sample Output
$ select f in *;do vim $f; break; done
1) arguments		  5)	   9)
2) data			  6) getopt_tryout	  10)
3) for_example		  7) getopts_tryout	  11)
4)	  8) parameter_expansion
#? 4

By: haivu
2009-09-10 06:04:42

What Others Think

Interesting. However, need to exclude directories and include hidden files in the listing.
sklm · 638 weeks and 6 days ago
If you want to exclude dir and include hidden file, try this: PS3="Enter a number: "; select f in $(find . -type f -maxdepth 1);do $EDITOR $f; break; done
haivu · 638 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: