I'm looking for a way to make the following alias work for any file name.

alias dim='cd /home/jason/Documents; vim *the desired file*; cd'

I'm wondering if there is a way I could change this alias to make it so that I could type in any file name as such:

dim *the desired file*

And still get the same result. Basically is there a way to call whatever is typed after the alias name into the alias itself? Something like:

alias dim='cd /home/jason/Documents; vim <what is typed after alias>; cd'
  • // , What was the purpose of this? – Nathan Basanese Jul 6 '15 at 17:18

No, you can not do that using shell aliases. You need to use a function.

Here is a simple function to do the job :

dim() {
cd /home/jason/Documents
vim "$1"

The function dim will take a file name as argument. You can put this code snippet at the end of your ~/.bashrc file and then run it as:

dim file.txt

Replace file.txt with any file name you want.

To run it from the current shell session, source the ~/.bashrc file first :

. ~/.bashrc
| improve this answer | |
  • 3
    What does the "$1" signify? I'm a beginner and trying to learn rather than just copy paste and call it a day. – Jason Basanese Jul 5 '15 at 2:30
  • 1
    @JasonBasanese I appreciate your decision :) $1 indicates the first positional parameter or argument to the function..think of it as a way of giving input to a function or script ..like all programming languages bash also uses this....you'll find numerous resources about this on Google.. – heemayl Jul 5 '15 at 2:34

Not with aliases, use functions instead.

From the Bash man page:


[...] There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below).

So your function could be:

function dim () { cd ~jason/Documents ; vim $* ; cd - ;}

| improve this answer | |
  • 1
    Wouldn't ~/Documents work just fine in this case? Also what do the $ and * symbols mean in this case? and is the - after the cd needed? Usually I can just type cd and it goes up a directory. – Jason Basanese Jul 5 '15 at 2:34
  • “Usually I can just type cd and it goes up a directory” – I only know a plain cd to bring you to your home directory; cd .. goes one level up. cd - brings you to the previous directory from which you cd’ed. – Chriki Jul 5 '15 at 12:43
  • 1
    @JasonBasanese: Yes ~/Documents would work for everyone (if it is a function for all users). $* is like $1 $2 $3... just to give the whole list to vim, not the first argument you type. And as Chriki said, cd - will return to the previous directory (the one you were when you invoked the function). – CijcoSistems Jul 17 '15 at 6:17

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.