How do I create an alias for a directory in ipython? - ipython

On Windows I want to create an alias for my working directory so I can quickly cd into it.
I have tried this command
%alias $UWHPSC echo 'c:/Users/xxxx/Documents/uwhpsc'
cd $UWHPSC
which gives the following error
[Error 2] The system cannot find the file specified: u'$UWHPSC'
c:\Users\xxxx\Documents\uwhpsc

%cd has a notion of bookmarks, which persist across IPython sessions:
%bookmark UWHPSC c:/Users/xxxx/Documents/uwhpsc
%cd UWHPSC
See the output of %bookmark? for more info.

Just define a normal Python variable, and then use it with a $ in the cd command:
UWHPSC = 'c:/Users/xxxx/Documents/uwhpsc'
cd $UWHPSC

Related

How can I make a function run every time cd successfully changes to another directory within sh on FreeBSD?

I'm using sh as my shell on FreeBSD but I want to be able to have a pretty prompt like the one bash gives me on Ubuntu. There are two things that the FreeBSD implementation of sh seems to lack as far as PS1 escape characters go:
The \w works but does not expand $HOME to ~, so this is something I have already hacked up myself
I can use PS1 to update the prompt on the terminal, but as far as I can tell it is not possible to use the PS1 variable to update the title bar as well. ESC and BEL fail to set the title as one would expect if they were using bash or ksh
Here is my .shrc file
update_prompt() {
case "$PWD" in
"$HOME"*)
pretty_pwd="~${PWD#*"${HOME}"}"
;;
"/usr$HOME"*)
pretty_pwd="~${PWD#*"/usr${HOME}"}"
;;
*)
pretty_pwd="$PWD"
;;
esac
case "$TERM" in
xterm*|rxvt*)
PS1="[$USER#\\h $pretty_pwd]\\$ "
;;
*)
;;
esac
printf "\\033]0;[%s#$(hostname -s): %s]\\007" "$USER" "$pretty_pwd"
}
update_prompt
So when I fire up a terminal or log in via ssh, it gives the pretty prompt that I like. But now I need this function to run every time that cd is executed and returns an exit status of 0.
I was going to use an alias that was something like:
alias cd="cd $1 && update_prompt"
but that was before I realized that aliases do not except arguments. How might I go about doing something like this?
You can use a function instead of an alias:
cd() {
command cd "$#" && update_prompt
}
Just put it into ~/.shrc. You have to use command here to let sh know that you are referring to the actual cd builtin command instead of the function you've just defined.
Refer to the sh(1) manual page for the details on how to make sh(1) source the ~/.shrc file when it starts:
Therefore, a user should place commands that are to be executed only at login
time in the .profile file, and commands that are executed for every shell
inside the ENV file. The user can set the ENV variable to some file by placing
the following line in the file .profile in the home directory, substituting for
.shrc the filename desired:
ENV=$HOME/.shrc; export ENV
I use this trick in my cd alias manager. Here's a link to the source code of the function: https://github.com/0mp/goat/blob/v2.5.0/libgoat.sh#L31-L57
You can do it with alias+arguments if you swap the commands:
$ alias cd="echo change; cd"
$ pwd
/nas
$ cd /
change
$ pwd
/
$ cd /etc
change
$ pwd
/etc
$

Perforce p4 sync command fails for a folder which has a space in it's name

There is a folder with the name Test Logs. As it can be seen there is a space between Test and Logs
When I try to get it locally using sync command in perl script it fails.
The script has the code:
system("p4 sync -f //depot/Test Logs/OnTargetLogs/...");
I get the following error:
> //depot/Test - no such file(s).
> Logs/OnTargetLogs/... - no such file(s).
Quote the argument maybe?
system("p4 sync -f \"//depot/Test Logs/OnTargetLogs/...\"");
– Sobrique
What you said worked. Also I found another way of doing this :
my #a1 = ("p4","sync","-f","//depot/Test Logs/OnTargetLogs/..."); system #a1;
– Vishal Khemani

launch sublime text 3 in terminal with zsh

I recently purchased a new MacBook and I am trying to re-configure my system.
The app is inside the Applications folder as 'Sublime Text.app'
I have edited the sublime.plugin.zsh file via other advice I found online to 'Sublime Text 3.app' as well as 'Sublime Text.app' with no luck on either:
elif [[ $('uname') == 'Darwin' ]]; then
local _sublime_darwin_paths > /dev/null 2>&1
_sublime_darwin_paths=(
"/usr/local/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
)
for _sublime_path in $_sublime_darwin_paths; do
if [[ -a $_sublime_path ]]; then
alias subl="'$_sublime_path'"
alias st=subl
break
fi
done
fi
alias stt='st .'
I still get
zsh: command not found: st
I am simply at a loss on where to go next
I had the same problem with zsh and this did the job:
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Then you launch a open a file my_file.txt with Sublime:
subl ./my_file.txt
Don't specify any file if you just want to open Sublime. I hope this helps ;)
First, try to first launch the sublime binary manually (interactively) via zsh.
To do that, you'll have to discover where this binary is. There are two practical options here, choose what you are most comfortable with:
Check manually those listed binaries, see which of them exist.
Slightly modify your script to echo something inside your if:
if [[ -a $_sublime_path ]]; then
echo "Sublime found: $_sublime_path"
alias subl="'$_sublime_path'"
alias st=subl
break
fi
After finding the correct one, create the st alias in your .zshrc file:
alias st="/correct/path/to/subl"
If you don't find anything in the first step, then your original script is really not supposed to work.
Just moved to App in mac
Check your current path
echo $PATH
Add a sym link from Sublime App to one of your path. Choose /usr/local/bin for example
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
Then back to terminal and run sublime. You should be open the sublime through terminal
To setup alias for mac users;
open ~/.zshrc using the below command
vi ~/.zshrc
Add the following alias
alias subl="'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl'"
run subl . command should work properly.
Official documentation: https://www.sublimetext.com/docs/command_line.html#mac
ZSH
If using Zsh, the default starting with macOS 10.15, the following command will add the bin folder to the PATH environment variable:
echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

Can a Fish script tell what directory it's stored in?

So, I really like Fish - but I need some help with scripting.
and in particular finding the path of the script being run.
Here is the solution for BASH
Getting the source directory of a Bash script from within
Can anyone help me find the equivalent with fish?
status --current-filename will output the path to the currently executing script.
For more information on the status command, you can run man status or see the documentation at http://fishshell.com/docs/current/commands.html#status
In fish shell,
set DIR (cd (dirname (status -f)); and pwd)
is an equivalent to the BASH one liner
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
mentioned in Can a Bash script tell what directory it's stored in?.
NOTE: fish shell will cd into that dir and stay there. Bash will cd but it stays contained in subcommand.
File path
To get the full path of the script, use status --current-filename, or
set FILE (status --current-filename) to declare a variable.
Directory path
To get the full path to the directory the script is stored in, use dirname (status --current-filename), or set DIR (dirname (status --current-filename)) to declare a variable.
The equivalent to this question is the following:
set DIR (dirname (status --current-filename)) is the equivalent
Since readlink -m does not exist on macOS,
DIR=dirname (realpath (status -f))
Works on macOS and Linux at the same time.
This solution also has the benefit to work from an .sh file or even the command line.

Git Bash shell fails to create symbolic links

When I try to create a symbolic link from the Git Bash shell, it fails every time all the time:
ln -s /c/Users/bzisad0/Work testlink
Output:
ln: creating symbolic link `testlink' to `/c/Users/bzisad0/Work': Permission denied
The only thing it does, besides giving the error message, is create an empty directory named (in this case) testlink.
I don't see any problem with the ln executable. For instance, it is owned by me and marked as executable:
which ln
ls -hal /bin/ln
Output:
/bin/ln
-rwxr-xr-x 1 BZISAD0 Administ 71k Sep 5 11:55 /bin/ln
I also own the current directory (~, which is /c/Users/bzisad0):
ls -dhal .
Output:
drwxr-xr-x 115 BZISAD0 Administ 40k Sep 5 12:23 .
I have administrative rights, and I've tried opening the Git Bash shell with "Run as Administrator", but that makes no difference.
I've tried opening the Windows properties for ln.exe and setting the Privilege Level to "Run this program as an administrator" but that doesn't help.
I've gone into the Security → Advanced properties in Windows and made myself (rather than the Administrators group) the owner, but that doesn't fix anything either.
I'm at a loss. I don't know whether this error message is ultimately coming from ln, from Bash, or from Windows, or how I could possibly lack the permission. How can I get to the bottom of this?
It is possible, albeit extremely awkward, to create a symbolic link in MSysGit.
First, we need to make sure we are on Windows. Here's an example function to check that:
windows() { [[ -n "$WINDIR" ]]; }
Now, we can't do cmd /C, because MSysGit will fornicate with this argument and turn it into C:. Also, don't be tempted to use /K; it only works if you don't have a K: drive.
So while it will replace this value on program arguments, it won't on heredocs. We can use this to our advantage:
if windows; then
cmd <<< "mklink /D \"${link%/}\" \"${target%/}\"" > /dev/null
else
ln -s "$target" "$link"
fi
Also: note that I included /D because I'm interested in directory symlinks only; Windows has that distinction. With plenty of effort, you could write a ln() { ... } function that wraps the Windows API and serves as a complete drop-in solution, but that's... left as an exercise for the reader.
As a thank-you for the accepted answer, here's a more comprehensive function.
# We still need this.
windows() { [[ -n "$WINDIR" ]]; }
# Cross-platform symlink function. With one parameter, it will check
# whether the parameter is a symlink. With two parameters, it will create
# a symlink to a file or directory, with syntax: link $linkname $target
link() {
if [[ -z "$2" ]]; then
# Link-checking mode.
if windows; then
fsutil reparsepoint query "$1" > /dev/null
else
[[ -h "$1" ]]
fi
else
# Link-creation mode.
if windows; then
# Windows needs to be told if it's a directory or not. Infer that.
# Also: note that we convert `/` to `\`. In this case it's necessary.
if [[ -d "$2" ]]; then
cmd <<< "mklink /D \"$1\" \"${2//\//\\}\"" > /dev/null
else
cmd <<< "mklink \"$1\" \"${2//\//\\}\"" > /dev/null
fi
else
# You know what? I think ln's parameters are backwards.
ln -s "$2" "$1"
fi
fi
}
Also note a few things:
I just wrote this and briefly tested it on Windows 7 and Ubuntu, give it a try first if you're from 2015 and using Windows 9.
NTFS has reparse points and junction points. I chose reparse points, because it's more of an actual symbolic link and works for files or directories, but junction points would have the benefit of being an usable solution in Windows XP, except it's just for directories.
Some filesystems, the FAT ones in particular, do not support symbolic links. Modern Windows versions do not support booting from them anymore, but Windows and Linux can mount them.
Bonus function: remove a link.
# Remove a link, cross-platform.
rmlink() {
if windows; then
# Again, Windows needs to be told if it's a file or directory.
if [[ -d "$1" ]]; then
rmdir "$1";
else
rm "$1"
fi
else
rm "$1"
fi
}
For my setup, that is Git for Windows 2.11.0 installed on Windows 8.1, export MSYS=winsymlinks:nativestrict does the trick as
The Git Bash shell may need to be run as an administrator, as by default on Windows only administrators can create the symbolic links.
So, in order to make tar -xf work and create the required symbolic links:
Run Git Bash shell as an administrator
Run export MSYS=winsymlinks:nativestrict
Run tar
A workaround is to run mklink from Bash. This also allows you to create either a symbolic link or a junction point.
Take care to send the mklink command as a single argument to cmd...
cmd /c "mklink link target"
Here are the options for mklink...
cmd /c mklink
Output:
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link specifies the new symbolic link name.
Target specifies the path (relative or absolute) that the new link
refers to.
If you want to create links via a GUI instead ... I recommend Link Shell Extension that is a Windows Explorer plugin for creating symbolic links, hard links, junction points, and volume mount points. I've been using it for years!
Link Shell Extension
Symbolic links can be a life saver if you have a smaller SSD drive on your system C: drive and need to symbolic link some bloated folders that don't need to be on SSD, but off onto other drives. I use the free WinDirStat to find the disk space hogs.
I believe that the ln that shipped with MSysGit simply tries to copy its arguments, rather than fiddle with links. This is because links only work (sort of) on NTFS filesystems, and the MSYS team didn't want to reimplement ln.
See, for example, http://mingw.5.n7.nabble.com/symbolic-link-to-My-Documents-in-MSYS-td28492.html
Do
Grant yourself privileges to create symbolic links.
Search for local security policies
Local Policies/User Rights Assignment/Create symbolic links
Take a moment to scold Windows. "Bad OS! Bad!"
Profit
This grants you the privilege to create symbolic links. Note, this takes effect on the next login.
The next step is to figure out how ln is configured:
env | grep MSYS
We are looking for MSYS=winsymlink: which controls how ln creates symbolic links.
If the variable doesn't exist, create it. Note, this will overwrite the existing MSYS environment variable.
setx MSYS winsymlinks:nativestrict
Do not
Run your shell as an administrator just to create symbolic links.
Explanation
The error is somewhat self-explanatory, yet elusive.
You lack the appropriate privileges to run the command.
Why?
Be default, Windows only grants symlink creation rights to Administrators.
Cygwin has to do a song and dance to get around Windows subpar treatment of symbolic links.
Why?
Something, something "security"
¯\_(ツ)_/¯
Edit:
I just realized OP had admin rights. I leave this answer up, hoping it's useful to others.
Extending Camilo Martin's answer as you need to use the /j parameter switch for Windows 10; otherwise the call will just return "You do not have sufficient privilege to perform this operation."
This works for Git Bash 2.20.1.windows.1/MINGW64 (Windows 10) without administrator rights (if you can read/write both /old/path and /link/path:
original_folder=$(cygpath -w "/old/path")
create_link_new_folder=$(cygpath -w "/link/path")
cmd <<< "mklink /j \"${create_link_new_folder}\" \"${original_folder}\"" > /dev/null
For anyone who's interested in how to accomplish this in Windows 10 Git Bash 2.28.0.0.1:
You have to prefix the ln -s command with the MSYS=.. instead of executing export MSYS=.. first, namely it's just one command:
MSYS=winsymlinks:nativestrict ln -s <TARGET> <NEW_LINK_NAME>
Since this is one of the top links that come up when searching for creating symbolic links in MSYS or Git Bash, I found the answer was to add
set MSYS=winsymlinks:native when calling git-cmd.exe (I run ConEmu) or uncomment the same line in the msys2_shell.bat file.
I prefer PowerShell to CMD, and thought I'd share the PowerShell version of this.
In my case it consists of making symbolic links linking ~/.$file to ~/dotfiles/$file, for dotfile configurations. I put this inside a .sh script and ran it with Git Bash:
powershell New-Item -ItemType SymbolicLink\
-Path \$Home/.$file\
-Target \$Home/dotfiles/$file
Instead of symbolic links on Windows, I found it easier to write a small Bash script that I place in my ~/bin directory.
To start Notepad++ with the npp command, I have this file:
~/bin/npp
#!/usr/bin/bash
'/c/Program Files (x86)/Notepad++/notepad++.exe' $#
And I get the path syntax right by dragging and dropping the file from Windows Explorer into Vim.
The Windows command mklink /J Link Target doesn't seem to work any more.
git bash honors the symbolic links created by cygwin. The caveat is that the symbolic link not use, e.g., '/cygdrive/c/directory' and instead use '/c/directory'.