You can use this to save some frequently used directories and easily switch to one of them by choosing from a list or by Bash completion.
I have been using this script since last year. Until days ago, I finally rewrote it with Bash completion functionality. You can read the source code. Licensed under the MIT License.
Warning
The content in this page may be outdated, please follow the instructions on project website, instead.
1 Usage & Installation
There are two ways to use this script:
- Run it as a normal Bash script
- Source it and use the Bash function g() inside this g script.
I prefer latter, because you can use it with Bash completion and you don’t need to prefix a dot when switching directory. I will explain how to use in either way.
1.1 Bash Script Way
Firstly, download this g script to a place you like, say /path/to/g. You can run /path/to/g -h to see what options you have. Here is a sample:
Commands: g dir : change working directory to dir g (-g|g) : get a list of directories g (-a|a) : add current directory g (-a|a) dir : add dir g (-r|r) : remove a directory from list g (-h|h) : show what you are reading right now
Before you can really use this g script, you need to save some directories. Use cd to one of the most used directories. Then, run /path/to/g -a to add current directory to ~/.g_dirs (default file). Once you have added some, run:
. /path/to/g g
That dot at beginning and g letter at tail are very important. You will see a list of directories, which you have just added. Choose one, enter the number and hit Enter key to switch directory.
If you want to remove just run:
/path/to/g -r
The dot above is doing sourcing a Bash script, which is necessary to let directory changing takes effect in Bash shell when you want to do that by running a script.
1.2 Bash Function Way
I assume that you want to use Bash completion with this g script. You need to do few things first:
- Install the Bash completion package for your distro. On Fedora, run yum install bash-completion.
- Save this script to /path/to/g.
- In you home directory, run ln -s /path/to/g .bash_completion. If you want to make it system-wide, switch to /etc/bash_completion.d, then run ln -s /path/to/g .bash_completion as root.
- (Optional) Normally, TAB is the key to get the list, but usually you still need to type some more. You can make it to cycle the options in the list. You only need to have TAB: menu-complete in ~/.inputrc. Once again for system-wide, put that in /etc/inputrc.
More about inputrc, I don’t want to change the behavior of TAB, so I have:
"\e[6~": menu-complete #TAB: menu-complete
set completion-ignore-case On
\e[6~ allows me to use PAGEDOWN to cycle options, and TAB still gives me a list.
After you finishing steps above, open another terminal. Type g and hit ENTER key, you should see a help menu like the one in Bash Script Way. If not, there is something wrong.
Then you will need to add some directories, run g -a to add current directory. After you add some, run g again, you should have a list of them. You can choose one from them to switch directory.
Now, type g and TAB or PAGEDOWN (if you have same ~/.inputrc like mine). The directories should be cycling, press ENTER when you get the one you want.
In this way, you run a Bash function not a script. Here is a screenshot of using as a function:
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.