3

For an example, I am in the directory Desktop. I then do cd break and when I list all files in break, i have for example main.cpp file. I am trying to be able to go straight from Desktop to opening up main.cpp using sublime text(using subl to open it) without getting to break then doing subl main.cpp but can't seem to figure it out. This is what I want to do:

/Desktop $ cd break/subl main.cpp

but obviously that won't work. How do I do this and combine everything into one command?

5
  • 3
    subl break/main.cpp? Or, to explain more clearly, put the command you use to open the file at the start of the path. – Zanna Jan 12 '17 at 7:36
  • that was easy enough, thanks!! could never figure it out so I would always ls then open it – Kdrumz Jan 12 '17 at 7:40
  • 1
    @Zanna post is as answer please – Sergiy Kolodyazhnyy Jan 12 '17 at 7:42
  • 1
    @Kdrumz just FYI you don't have to cd into folder, you can specify full path. If you need to cd somewhere that is very deep into directory tree, just make a bash function and place it into .bashrc or make a symlink to that directory. Just friendly advice – Sergiy Kolodyazhnyy Jan 12 '17 at 7:43
  • I can't post as answer? I don't see how to. ahh okay, thanks @Serg ! – Kdrumz Jan 12 '17 at 7:45
7

To open a file with a specific program in a directory other than the current working directory, put the command you use to open the file first, then the path to the file. In your case:

subl break/main.cpp

You can also use an absolute path, which will work regardless of the current working directory

subl ~/Desktop/break/main.cpp

You can use wildcards in the path. For example I have a file in

/home/zanna/Dropbox/Writings/journal02/Capoeira

I open it like this

vim ~/Dr*/Wr*/j*2/Cap*

If you often open a specific file with a particular program you could make an alias to open it

alias capedit='vim ~/Dropbox/Writings/journal02/Capoeira'

As @Serg suggested you can also make a symlink to a file buried deep in a directory tree to make it more accessible

ln -s ~/Dropbox/Writings/journal02/Capoeira ~/new-writing/Capoeira

Now I can just

vim ~/new-writing/Capoeira
4
  • 2
    I would suggest tab completion rather than wildcards for this task. It's easier to figure out what is going on if something is not as planned (missing directory, more than one completion...), and it is an useful shortcut in a wider range of cases. – Federico Poloni Jan 12 '17 at 11:10
  • ~/ is not absolute, its relative home directory. – Dmitry MiksIr Jan 12 '17 at 12:20
  • @DmitryMiksIr it is an absolute path, it just uses tilde expansion - that doesn't make it relative (except to the current user of the shell), because it works anywhere in the filesystem – Zanna Jan 12 '17 at 15:43
  • Yep, you are right. – Dmitry MiksIr Jan 12 '17 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.