Mîchæl
Mîchæl

Reputation: 49

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 ?

Upvotes: 0

Views: 665

Answers (5)

davidcondrey
davidcondrey

Reputation: 35963

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

Upvotes: 0

David C. Rankin
David C. Rankin

Reputation: 84541

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.

Upvotes: 0

romainl
romainl

Reputation: 196496

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

Upvotes: 1

Caek
Caek

Reputation: 592

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

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 992857

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

Upvotes: 0

Related Questions