Reputation: 20232
I don't have experience in shell scripting, so I'm unable to see the problem of the following situation:
I have defined an alias of the following form(file.c
is located in ~/dir
):
alias code="cd ~/dir | vim file.c"
When inputting it, I get the following warning, and the proper files aren't opened:
Vim: Warning: Input is not from a terminal
2 files to edit
I thought that the pipe operator in this instance would separate the two commands, first changing the directory, then opening the file.
Upvotes: 2
Views: 2617
Reputation: 942
Since you mention you don't have experience in shell, I will be verbose.
The problem is that you are using the pipe wrong.
cmd1 | cmd2
Means redirecting the output of cmd1 to the input of cmd2. Maybe you are confusing it with the OR operator ||, which can be used as:
cmd1 || cmd2
"If cmd1 is not successful, execute cmd2". This would not help you in your case, though. Both previous answers work for you:
alias code="cd ~/dir; vim file.c"
Using the semicolon, which is just a simple command separator. Or my personal favorite
alias code="cd ~/dir && vim file.c"
Which uses the AND operator.
cmd1 && cmd2
means "do cmd1. If it is successful, proceed with cmd2. Otherwise, stop".
Upvotes: 5
Reputation: 3653
You don't need pipe. Use semicolon that is command separator.
alias code="cd ~/dir; vim file.c"
You can also use pushd and popd in alias to return to previous directory.
Upvotes: 3
Reputation: 121407
A better alias would be:
alias code="cd ~/dir && vim file.c && cd -"
This will ensure you are not opening file in your current directory if cd
failed and return to the old directory where you were.
Upvotes: 3