tcsh shell and variable argument alias - alias

I use tcsh shell and I cannot change to another shell.
I'm trying to create an alias to quickly cd into my project.
My home directory looks like:
/home/projects/proj1
/home/projects/proj2
...
/home/stuff/stuff1
If i am working in stuff 1 directory and want to quickly navigate to my projects, i created an alias as follows:
alias P 'cd /home/projects && cd ./\!:1'
so that I can type 'P proj1' to get there
However, sometimes i want to navigate to /home/projects directory instead of a particular project and i thought just typing 'P' (without arguments) will get me there but i'm getting a "Bad ! arg selector". How do i create an aliases to handle zero or more arguments?
Thanks

This works:
alias P 'cd /home/projects/\!*'
If you pass an argument, it is appended to the end of the path, else !* is replaced by nothing.

Related

How to exclude certain char from being used as a variable name in production build?

After build, one of my variables is getting renamed to $ char. I use built code in Jquery project and this variable called $ causes errors. How can I exclude $ sign from being used as variable in the production files?
This is how the production code looke like even though I use nowhere $ sign:
function $(e,t){e.value=t==null?"":t}
How can exclude $ sign from being used as variable or function name?

zsh: global alias to redirect to temporary file

I want to add a global aliash in zsh which will look something like this:
alias -g t='> tmp-$(date +%Y%m%d-%h%m%s).txt'
What it should do is create a new timestamped temporary file in the current directory and redirect output to that file. However, the filename gets evaluated at the time of zsh being sourced instead of the alias being called.
I guess what I need is some sort of lazy evaluation. Is there a way to achieve this?
Okay, I figured out a way. More involved than I wanted it to be:
function redirect-to-tmp() {
TMPFILE="./tmp-$(date +%y%m%d-%H%M%S)"
cat >>! $TMPFILE
echo "Redirected to $TMPFILE"
}
function redirect-to-tee() {
TMPFILE="./tmp-$(date +%y%m%d-%H%M%S)"
tee -a $TMPFILE
echo "Redirected to $TMPFILE"
}
alias -g t='| redirect-to-tmp'
alias -g T='| redirect-to-tee'
Let me know if anyone else has a better answer.

Is there a way to use an alias as a part of a command?

i.e. I have an alias in my .cshrc file
alias foo = /Users/name/documents
and i want to do something like
mv foobar.txt foo
but this just renames foobar.txt to foo.
Also tried
mv footbar.txt $foo
to no avail.
I think I can create a script to take in a path and filename and alias instead to the script, but is there another way to do this?

An alias that just prints the named directory?

I'm kind of just realizing how powerful the terminal can be. My question is essentially if I can create an alias that just prints the name of a directory. For example, I could easily make an alias such as "alias sitename="cd ~/sites/path/to/my/site/". But what I want is an alias that only prints the directory name so that I can use it for several things. So that, for example, if I wanted I could just say cd "alias", or mv from-dir "alias".
Is there a way to do this? I've tried and it seems to recognize the alias if I just type it in: it will report "alias" is a directory. But if I try to couple it with another command, it fails.
You don't want to use alias, what you are after is an environment variable
$ export SITENAME="~/sites/path/to/my/site/"
$ cd $SITENAME
Bash is quite picky over syntax - note the lack of spaces in the export and the $ when you use it.
Use a variable, simply
d=/path/to/some/directory
echo $d
cd $d
mv somedir $d/
You don't need to use an alias here, a variable is sufficient.
It sounds like you want to set a variable, not an alias. Such as,
sitename=/home/jimbo/. Then, cd $sitename would put you at /home/jimbo/.
If you want this variable to have permanence (i.e. you don't have to set it every time you open a new session), then you can make it an environmental variable using the export command or add it to your .bashrc file (typically located at $HOME/.bashrc) using the line: sitename=/home/jimbo/.
FYI, $HOME is another environmental variable that's equivalent to ~/.
Using a variable is the simplest solution. You could get fancy and use an array:
mydir() { echo "/my/directory"; }
To display the value
mydir
To use the value, you need some extra puncuation
cd $(mydir)
cd `mydir`

How to make a path-agnostic alias for zsh?

I'm using hsenv. To activate it I need to do this:
cd ~/projects/foo
hsenv
source .hsenv_foo/bin/activate
I'm looking for a way to replace third command with an alias. The problem is there's a path-dependent component in the path to activate script.
How can I replace the foo in .hsenv_foo by the name of actual directory that I'm currently in?
I'm not sure how to do it in an alias, but you can wrap it in a function:
function my-hsenv {
# If we want the full path
# foo=`pwd`;
# If we want only the name of the current dir
foo=$(basename $(pwd))
source .hsenv_${foo}/bin/activate
}
Then run my-hsenv:
$ my-hsenv
my-hsenv:source:5: no such file or directory: .hsenv_tmp/bin/activate
But you get the idea.