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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With