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) getopt_homemade.sh	   9) remove_from_path.sh
2) data			  6) getopt_tryout	  10) scriptname.sh
3) for_example		  7) getopts_tryout	  11) var_substitution.sh
4) getopt_function.sh	  8) parameter_expansion
#? 4

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

These Might Interest You

  • use this command via ssh on remote machine when you want play video remove --directory if you want select single file


    0
    lol=`zenity --file-selection --directory` && DISPLAY=:0.1 cvlc -f -I ncurses --play-and-stop "$lol"
    rusino · 2010-09-12 01:10:16 0
  • Copies file to a temporary location, edit and set to real file's time stamp then copy back. Assumes access to /tmp and has $EDITOR, but can be replaced with better values.


    0
    edit-notime () { FILE=$1; TMP=`mktemp /tmp/file-XXXXXX`; cp -p $FILE $TMP; $EDITOR $TMP; touch -r $FILE $TMP; cp -p $TMP $FILE; rm -f $TMP; }
    jecxjoopenid · 2012-10-31 00:54:19 0
  • eh stands for Edit History . Frequently, I'll mistype a command, and then step back through my history and correct the command. As a result, both the correct and incorrect commands are in my history file. I wanted a simple way to remove the incorrect command so I don't run it by mistake. . When running this function, first the ~/bash_history file is updated, then you edit the file in vi, and then the saved history file is loaded back into memory for current usage. . while in vi, remember that `Shift-G` sends you to the bottom of the file, and `dd` removes a line. . this command is different than bash built-in `fc` because it does not run the command after editing.


    7
    eh () { history -a ; vi ~/.bash_history ; history -r ; }
    unixmonkey8121 · 2011-03-23 18:00:20 5
  • Linux users wanting to extract text from PDF files in the current directory and its sub-directories can use this command. It requires "bash", "ps2ascii" and "par", and the PARINIT environment variable sanely set (see man par). WARNING: the file "junk.sh" will be created, run, and destroyed in the current directory, so you _must_ have sufficient rights. Edit the command if you need to avoid using the file name "junk.sh"


    0
    echo '#!/bin/bash' > junk.sh ; find . -iname *.pdf -type f -printf \p\s\2\a\s\c\i\i\ \"%p\"\ \ \"%p\.\t\x\u\"\;\ \p\a\r\ \<\"%p\.\t\x\u\"\ \>\"%p\.\t\x\t\"\ \;\ \r\m\ \"%p\.\t\x\u\"\ \\n >>junk.sh; chmod 766 junk.sh; ./junk.sh ; rm junk.sh
    p0g0 · 2013-01-27 21:29:08 0

What Others Think

Interesting. However, need to exclude directories and include hidden files in the listing.
sklm · 457 weeks and 4 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 · 457 weeks and 4 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: