How to create an alias for .. to mean cd .. in macOS shell - bash

I often find myself typing cd.. (which results in -bash: cd“: command not found) in stead of cd ..
So would like to add an alias for this in my .bashrc or better yet, I would like an alias for .. to mean cd ..
I can't get it to work however; I tried several options:
alias ..=cd .. results in alias ..='cd'
alias ..='cd ..' results in alias ..=''cd'
Also escaping the dots in various ways doesn't work:
alias ..=cd \.\. results in alias ..='cd'
alias ..=cd '\.\.' results in alias ..='cd'
What is correct way to do this right?

This works for me on linux (Ubuntu 16.04) with bash v4.3.46. I believe it won't be any different for macOS.
alias '..'='cd ..'

I assume that you are interested in solutions working for bash.
If you want to solve this with an alias, you have to use a name which starts with a letter, for instance
alias up='cd ..'
Maybe more interesting for you would perhaps a general solution, where typing any directory name would cd to this directory. You can do it by placing into your .bashrc the command
shopt -s autocd
After this, .. would bring you one directory up, ~ would bring you to your home direcory and aunt/bertie would cd you right to your aunt.

Related

How to properly write a bash executable file

I am having some issue in writing a simple executable .sh file via bash.
The order of operation as soon as I open a terminal (ctrl+alt+T) are:
pc:~$ roscd
pc:~/catkin_docking_ws/devel$ cd ..
pc:~/catkin_docking_ws$ cd devel/lib/tuginterface/
pc:~/catkin_docking_ws/devel/lib/tuginterface$ ./tuginterface
I have been investigating this small issue and came across this source which advises to change and rename the project as an alias and that is exactly what I tried to do:
alias proj="roscd"
alias proj2="cd .."
alias proj3="cd devel/lib/tuginterface/"
alias exec="./tuginterface"
My current executable file after many trials is:
#!/bin/bash
alias proj="roscd"
alias proj2="cd .."
alias proj3="cd devel/lib/tuginterface/"
alias exec="./tuginterface"
But it still does not work.
The same post advises to create a script and after that an alias in the startup file.
Please advise on how to solve this problem and sorry if it is a simple question but I don't seem to catch the mistake I am making.
The script doesn't need to define aliases. Aliases are commands that you can type yourself. The script can just execute the commands directly.
#!/bin/bash
cd ~/catkin_docking_ws/devel/lib/tuginterface
./tuginterface
I've combined the three cd commands into one that jumps straight to the correct directory.
"But I thought cd doesn't work in shell scripts?"
It depends what you're looking for. When a script changes directory it affects later commands in the script, so in that respect it does work. The change of directory is only inside the script, though. The person calling the script won't see the directory change. Their current directory is unaffected.

Get current directory through alias in git-bash (windows)

This command prints the directory name just fine:
echo ${PWD##*/}
This alias in .bashrc does not:
alias echodir="echo ${PWD##*/}"
They both work fine in the home directory, but after changing directories only typing it in manually works. The alias still prints the home folder. I understand this is because git bash works with nested shells or something - the base shell doesn't change directories at all, but the surface one does.
Is there a way to create an alias that works as expected?
I'm just posting the answer twalberg put in the comments(he gets the credit):
As a one-off command:
echo "-----Updating "${PWD##*/}"-----"
As an alias:
alias updating='echo "-----Updating "${PWD##*/}"-----"'

How can I create an alias to a directory so that I don't have to type the long path every time?

Currently, to get to the directory I need to type this:
cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser
Is there a way to make it so that I can just type something like:
cd FolderBelongingToUser ?
I'm familiar with z (uses ranking) and cdargs (uses shortcuts) but there are many other tools designed to make navigation in your shell easier and built-in solutions like CDPATH or the ** wildcard…
CDPATH
Adding something like this in your *rc file:
export CDPATH='.:~:/cygdrive/c/Users/NameOfUser/'
allows you to do exactly what you are after:
$ cd FolderBelongingToUser
or, better:
$ cd Fold<Tab>
**
If your bash is recent enough, you can do something like this:
$ cd **/foo
If you're using OSX you can open the hidden file named .bash_profile in your root user directory and add an entry like this:
alias define_your_shortcut='define your path'
You can do this for anything. For example here is an alias for your example:
alias FolderBelongingToUser='cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
Here's another example using a command to toggle hidden files
alias showfiles='defaults write com.apple.finder ShowAllFiles TRUE'
alias hidefiles='defaults write com.apple.finder ShowAllFiles FALSE'
After you make any changes to your bash_profile you'll need to either logout and login or you can open terminal and tell it to reload your bash_profile with this command
source ~/.bash_profile
I'm not personally familiar with Windows but if your using Windows a quick search result explained that this is how you would create a command prompt alias in Windows
AddConsoleAlias( TEXT("test"),
TEXT("cd \\<a_very_long_path>\\test"),
TEXT("cmd.exe"));
Alternatively it looks like someone provided a good answer to doing this in Windows here: https://superuser.com/questions/560519/how-to-set-an-alias-in-windows-command-line
Vim normally supports tab completion, so you can probably type something like
cd ~NamTabFolTab
which will expand to
cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser
Of course, if NameOfUser is you, then you can probably just type
cd ~/FolTab
You can add the following line to your .vimrc
cabbr FolderBelongingToUser /cygdrive/c/Users/NameOfUser/FolderBelongingToUser
Then you can can
cd FolderBelongingToUser
If you want to add more to the path (eg to specify a filename with :w) you can press / after FolderBelongingToUser and it will replace it with the full path and allow you to continue typing.
:ca[bbrev] is a Command line only abbreviation. See: :help :cabbr
If this is a permanent alias, then in your ~/.bashrc, create an alias:
alias FTBU='/cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
You will then be able to access it in your shell by:
$ cd FTBU
As another trick, if you are only going to use the alias to change to the directory then simply add cd to the alias
alias FTBU='cd /cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
You then need only type:
$ FTBU
to change to the /cygdrive/c/Users/NameOfUser/FolderBelongingToUser directory. However, if you plan to use the alias for any other purpose, leave the cd out of the alias definition.
If this is a temporary alias, you can simply create an alias from the command line with:
$ alias FTBU='/cygdrive/c/Users/NameOfUser/FolderBelongingToUser'
with the same results. (substitute anything you like for FTBU) Note: you remove an alias with the unalias command. Also Note: you should to check whether an existing system command exists with the name of your alias before assigning it. Simply type the proposed alias at the command line. If you receive something line bash: your_alias: Command Not Found, then you are good to go. There is no minimum number of characters required in an alias. So it you want to use a single-character, that's fine.

How to create a symlink to open a directory in Terminal on Mac osx?

There are certain folders I am cd'ing into all day long...for instance, "dev" which is in my Documents folder.
I had this bright idea to set up symlinks so I could simply type "dev" and Terminal would cd into that directory. This doesn't seem to work though. My two attempts are below.
Anyone know how to do this or is there a better way?
ln -s /Users/kelly/Documents/Dev/ dev
ln -s 'cd /Users/kelly/Documents/Dev/' dev
$ dev
bash: dev: command not found
With a symlink you can use
ln -s your/dev/directory/ dev
but you can only use it in the directory you created it in and in the form of cd dev.
If you just want to type dev at any point, use an alias:
alias dev="cd your/dev/direcrory/"
(this should then be in your ~/.bashrc)
You could use an alias for dev. Add to your ${HOME}/.bashrc
alias dev='cd /Users/kelly/Documents/Dev/'
and bash correctly parses ~ in an alias:
alias dev='cd ~/Documents/Dev/'
Using an alias eliminates the need for the symbolic link at all.
You have to write cd dev in your case, but it might be better for you to use bash aliases...
Write in your $HOME/.bash_aliases file:
alias dev='cd /Users/kelly/Documents/Dev/'
after opening a new terminal executing dev will give you, what you want...
In your $HOME/.bashrc, declare an array which maps directories to aliases.
declare -A __diraliasmap=(
[dev]="/Users/kelly/Documents/Dev"
[other]="/Users/kelly/Documents"
)
Also define a command_not_found_handle function. If defined, this function will be run by bash when a command is not found. In the function, check whether the command which failed is listed as an alias for a directory and, if so, cd to the associated directory.
command_not_found_handle()
{
if [[ ${__diraliasmap[$1]+set} = set ]]; then
builtin cd "${__diraliasmap[$1]}"
else
false
fi
}

bash cd autocompletion without alias?

How do I use cd such that when I type 'cd wo' and there is only one directory starting with 'wo' (say, 'work'), that cd cd's into work? I think I remember doing this and I did not make a custom alias.
In Bash, pressing the "tab" key will autocomplete paths and directories.
So cd wo[tab] becomes cd work.
More on other forms of Bash autocompletion here.
You're probably looking for bash-completion.

Resources