Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I don't like reaching for the tilde ~ key so often when I'm specifying file and program paths relative to the user home directory on the command line. Does anyone have any tips for cutting the number of keystrokes down?

share|improve this question
    
reduce the number of keystrokes down from one? –  glenn jackman Apr 15 '11 at 15:44
    
@glenn: actually, there are two keyboard strokes required: Shift + ` –  Lekensteyn Apr 15 '11 at 15:44
    
Plus pressing SHIFT is harder to do with the tilde key, esp one-handed. –  dan Apr 15 '11 at 20:47
    
unless you've one finger on a hand or an awful keyboard (like no Shift button on the left side), Shift + ~ is not uneasy with one hand. –  Lekensteyn Apr 15 '11 at 21:37
    
I guess it's easier if I move my left hand to the left and use my thumb on the shift key. How do you do it? –  dan Apr 15 '11 at 21:46

1 Answer 1

up vote 3 down vote accepted

You cannot eliminate ~ completely, but at least there are some workarounds available.

These workarounds may not always be usable, but for other cases, it can be really helpful:

  • Before doing the command, change your directory using cd (without arguments). This changes your working directory to ~.
  • If you frequently need to change the working directory to a folder within your home folder, use the CDPATH variable in bash. Run the next command to minimize cd ~/folder to cd folder, but still allow to change to a directory folder in the current directory if exist.

    CDPATH=.:~
    

    To make this change persistent, add the line to ~/.bashrc. As its name may suggest, it only works for the cd command in bash.

  • Use the $HOME variable if you really want to avoid ~, in case the key would give you an electrical shock for example. This is especially useful in cases where tilde-expansion would not occur, between quotes for example ("~")
  • Create a function for a command. If you frequently need to copy files to a directory in your home directory as in cp file ~/archive, you can run the next code to create a function:

    cph(){ cp "$1" "$HOME/$2";}
    

    and use it as:

    cph file archive
    

    Note that you cannot pass arguments or multiple files to this function, that needs additional tweaking. As with the CDPATH hack, you can put it in your ~/.bashrc file to make it persistent.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.