Can I shorten my directory commands in Ubuntu?

7

0

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.

Spencer Cooley

Posted 2011-06-20T21:22:37.237

Reputation: 209

yeah I figured that, but since it had a little bit to do with ruby on rails I thought a programmer might have had the same problem. – Spencer Cooley – 2011-06-20T21:31:00.447

well, since it was moved perhaps consider accepting an answer on this or one of your other questions here on superuser. – matchew – 2011-06-26T17:28:10.193

Answers

3

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.

matchew

Posted 2011-06-20T21:22:37.237

Reputation: 466

I think symlinking works as long as your project doesn't reside too many levels deep in the filesystem. Otherwise, you might have to drill down through many levels before getting to the link, defeating the purpose. – phasetwenty – 2011-06-20T23:02:14.030

@phasetwenty, I agree. I also feel like symlinking to skip one dir is silly. If it was many dirs down or up and down I would argue strongly for symlinking. I edited my answer, and wonder if this may just be a case of not knowing what the [:tab:] key does. – matchew – 2011-06-20T23:10:21.613

ln is the better solution imo. – Vinicius Kamakura – 2011-06-21T00:14:54.137

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

David W.

Posted 2011-06-20T21:22:37.237

Reputation: 21

2

Take a look at Efficiently Navigating Directories on Unix which contains some useful Bash functions and aliases.

dogbane

Posted 2011-06-20T21:22:37.237

Reputation: 3 771

1

Have you considered using shell variables?

user@localhost $ USERVIEWS='app/views/user/'
user@localhost $ gedit $USERVIEWS/show.html.erb

phasetwenty

Posted 2011-06-20T21:22:37.237

Reputation: 111

1

If you have unique filenames, you can just do

gedit $(find -name show.html.erb)

The find will default to searching the current directory, and $() is equivalent to backticks, except that backticks are a deprecated shell feature in most modern shells. ;)

You could, of course, shorten that even more with a shell script along the lines of

#!/bin/sh
gedit $( find -name "$1" )

And you could replace -name with -iname to save you the hassle of using the shift key if you have upper-case filenames.

I'm not sure how gedit behaves with multiple arguments (in case you don't have completely unique filenames), but with vim it'll go through the files one at a time. So you'd just ":q" until you got to the one you wanted. I'd expect gedit to open multiple tabs if you had multiple names. But I don't use graphical editors for the same reason you don't want to use the mouse to open the file. ;)

dannysauer

Posted 2011-06-20T21:22:37.237

Reputation: 837

0

Have you considered GMate? It's a selection of plugins for Gedit that make Rails development (amongst other things) significantly easier by making it look like Textmate. It solves your problem with the Fuzzy Open plugin. You open your project directory in Gedit (I think using the File Browser Pane plugin) and then press ctrl-shift-o to activate FuzzyOpen. Next, start typing some of the letters of the file you want, and FuzzyOpen matches these against files and directories in your project. For example, if you started typing:

viushowhe

it would find all files whose path has those letters in that order, e.g.

app/VIews/USers/sHOW.Html.Erb

That same series of letters might also find things like:

Vendor/ImaplUgin/Stuff/SHOWmetHEmoney.rb etc.

Cameron Walsh

Posted 2011-06-20T21:22:37.237

Reputation:

0

I had a related need, to jump to directories far away. I wrote a script which relies on the directory name to be more or less unique, to work:

#!/bin/bash
#
# usage: pcd NAME
#
# checks, whether the first locate-match is a directory, and jumps into it, if it is.
# If not, the first match is displayed. pcd: perhaps-cd. :)
#
function pcd 
{
    folder=$(locate $1 | egrep "$1$" | head -n 1)
    # echo $folder
    if [[ -d $folder ]]
    then 
        echo $folder
        cd $folder
    else 
    echo "No directory "$folder
    fi
}

I sourced it from .bashrc to use the function.

If locate doesn't find too many files to edit, the shell could display 30 or 40 matches as a numbered list, and prompt you to enter a number from 0 to 30, 40, to open that file, or you could use zenity to display the matches, and let you select them.

The locate program relies on regular running of updatedb. It will not work for fresh new files from today, but most editors have a list of recently opened files - don't they?

The file-open-dialog can be modified to contain often used directories - pull these directories to the left, favorite-list.

user unknown

Posted 2011-06-20T21:22:37.237

Reputation: 1 623