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

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.

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.

How to run a bash script as a command?

I have a bash script that I would like to run globally as a command. For that I moved the script to /usr/local/bin/somefile.sh.
Now I still have to call a command called somefile.sh. I would like to have an alias for this command (for example I would just like to call the script with the command sf).
How do I do that?
And to complement answer from #Kent :
alias sf='/usr/local/bin/somefile.sh' using alias
ln -s somefile.sh /usr/local/bin/sf using soft link
mv /usr/local/bin/somefile.sh /usr/local/bin/sf using renaming
There are different ways:
as you mentioned, use alias, man alias for details
create a soft link named sf to your real script
rename your script
Open your .bashrc file located in ~/.bashrc. .bashrc file is read whenever you login to the system. Add the below line to the end of the file.
alias sf='/usr/local/bin/somefile.sh'
then re login or run source .bashrc
If you're on a GNU system, you could use the "alternatives" system:
dir=/usr/local/bin
sudo update-alternatives --install $dir/sf somefile $dir/somefile.sh 10
This creates 2 symbolic links:
/usr/local/bin/sf -> /etc/alternatives/somefile
/etc/alternatives/somefile -> /usr/local/bin/somefile.sh

How do I properly set aliases in a bash script (Ubuntu 17.04)?

I have this script called menal in my ~/bin directory:
#!/bin/sh
alias mendir='cd ~/projects/myproject'
It has executable property and I expect that when I run it it sets an appropiate alias for cd command for the terminal session. But it doesn't. When I type $ menal in terminal it shows no error, but when I try $ mendir after that I get
No command 'mendir' found, did you mean:
Command 'menhir' from package 'menhir' (universe)
mendir: command not found
When I type
$ alias mendir='cd ~/projects/myproject'
$ mendir
in terminal, it works.
What am I doing wrong? Is it a script scope issue or something?
Yes, it's a scope problem. Calling it the following way won't produce the result you expect:
./bin/menal
If you want the alias to persist, use source:
source ./bin/menal
You can add it to your .bash_profile.
alias mendir='cd ~/projects/myproject'
then do source ~/.bash_profile
It should create the alias and also will work on every login.

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

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.

How to "source" ~/.bashrc automatically once it has been edited?

I would like to create an alias that does the following:
Opens TextMate with ~/.bashrc and allows me to edit it
Once I close TextMate, "sources" ~/.bashrc (so if I add a new alias, for example, it will be available immediately)
I tried the following:
alias b="/usr/bin/mate -w ~/.bashrc; source ~/.bashrc"
but it doesn't work: when I close TextMate, the shell doesn't return.
Any ideas?
I hesitate to suggest it, but if this is a feature you really want, you can make something similar happen by setting the PROMPT_COMMAND variable to something clever.
PROMPT_COMMAND is run every time the shell shows the shell prompt So, if you're okay with the shells updating only after you hit Enter or execute a command, this should nearly do it.
Put export PROMPT_COMMAND="source ~/.bashrc" into your ~/.bashrc file. Re-source it into whichever shell sessions you want the automatically updating behavior to work in.
This is wasteful -- it re-sources the file with every prompt. If you can get your editor to leave the old version in a specific file, say ~/.bashrc~ (where the first ~ means your home directory and the last ~ is just a ~, a common choice for backup filenames) then you could do something more like (untested):
export PROMPT_COMMAND="[ ~/.bashrc -nt ~/.bashrc~ ] && touch ~/.bashrc~ && source ~/.bashrc "
then it would stat(2) the two files on every run, check which one is newer, and re-source only if the ~/.bashrc is newer than its backup. The touch command is in there to make the backup look newer and fail the test again.

Resources