How to make a path-agnostic alias for zsh? - alias

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.

Related

How to use alias for opening file from its absolute path in csh

I want to create an alias, which will take file name, get its path and open it by gvim.
Something like this:
alias gg "gvim `which \`"
usage:
> gg some_file_in_remote_path
But I cannot make it work in csh.
Can someone help please
The command "which" gets a full path of of shell commands.
It does not find the full path of of every file you have in your system.
The command you are looking for is "find", although searching the entire file system could be time consuming. Perhaps install "locate" and use that instead of find. Even then you would have to decide in your script what to do when multiple files have the same name.

tcsh shell and variable argument 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.

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`

batch scripting: how to get parent dir name without full path?

I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
FOR %%D IN ("%CD%") DO SET "DirName=%%~nxD"
ECHO %DirName%
This script gets the path from the CD variable and extracts the name only from it to DirName.
You can use basename command:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=$(basename "$FULLPATH")
You can use built-in bash tricks:
FULLPATH=/the/full/path/is/known
JUSTTHENAME=${FULLPATH##*/}
Explanations:
first # means 'remove the pattern from the begining'
second # means 'remove the longer possible pattern'
*/ is the pattern
Using built-in bash avoid to call an external command (i.e. basename) therefore this optimises you script. However the script is less portable.