Can I shorten my directory commands in Ubuntu?

When working on a rails app I like to open all of my files through the command line like so

CD my_app
gedit app/views/user/show.HTML.erb

Is there a way that I could shorten this so that I could just write something like

gedit  user_views/show.HTML.erb

?

I would like the console to stay in the main directory, I just don't like having to type out app/controller/user_controler.rb every time I want to open the user controller. I know that I could just open the file with my mouse, but I feel like moving from keyboard to mouse breaks my focus a little bit. When I can just tap away at the keyboard it seems like I have a more smooth workflow.


Solution 1:

Perhaps create a symbolic link from the main dir.

ln -s app/views/user/ user_views

will create a symbolic link in your main/working dir that will update the files in app/views/user. so doing an ls -l will show something like:

user_views -> app/views/user/

this way you could just go

$ gedit user_views/show.html.erb

also you could consider just setting a var

like

$ VIEWS='app/views/user/'

which would allow you to go

$ gedit $VIEWS/show.html.erb

you could consider putting this in your bash profile so it does not have to be re set each session.

more on symblic links

more on the bash profile and setting an environment variable.

EDIT

On my walk home I wondered why someone would want a symbolic link to just jump one dir?

/apps/views/use

in this case the views dir. Are you aware of what tab will do?

I remember it took me a few months to learn this handy trick.

Type something like the following and hit tab

$ gedit a

hit tab, and it will likely complete app leaving you with

$ gedit app/

type v and hit tab again, likely you will get

$gedit app/views/

hit tab twice to list all possible choices. This works on utilities as well as dir and files.

Typing

$ ged

and hitting tab will complete gedit. This should speed you up a bit.

Solution 2:

Don't know if this helps, but there's a very rarely used shell variable called CDPATH. CDPATH is very much like the PATH variable for commands. When you do a cd, it searches the CDPATH for directories that match your CDPATH.

For example:

CDPATH=.:/usr/local:$HOME/projects

If I did:

cd jiff/source

The CD command would look for a ./jiff/source, then a /usr/local/jiff/source directory, then a $HONE/projects/jiff/source directory. If there's a $HOME/projects/jiff/source directory, the directory will change to there.

This won't solve your problem where you can simply type in a partial path, and have your command execute, but you could say add myapp/app/view to your CDPATH environment variable and do this:

$ cd user
$ gedit show.html.erb

The other thing you can do is alias the gedit command to _gedit, and then write a quick shell script to take the "file name", search for a directory tree that matches what you put, and then edit the file. This is something that Kornshell users do to get the equivalent of the PS1=\u@\h:\w PS prompt that you BASH users have. Add something like this to your .bashrc file:

alias _gedit=gedit

function gedit {
    fileName=$(basename $0)
    dirName=$(dirname $0)
    for directory in app/controller my_app/app/view
    do
       if [[ -d "$directory/$dirName" ]]
       then
           $directory/$0
           break
       fi
   done
}

Now, if you typed gedit user_controler.rb, and there's a file app/controler/user_controler.rb, it'll edit app/controller/user_controler.rb