News for hackers. Stuff that matters.
Re:ZSH Aliases by xmdx on Tuesday May 26, 2020 @08:05AM (#23310057)
I wish I would chain things in the navigation alias.

For example if I set the following alias:

    alias hello = '~/Docuemnts'
It would be nice if I could then execute hello/world to take me to ~/Documents/world but it requires hardcoding.

Guess I could play around with functions and arguments to solve this.

Re:ZSH Aliases by gpspake on Tuesday May 26, 2020 @08:17AM (#23310169)
I think I might be able to help with this one! Here's a little trick I learned from a friend. It's also tab completable.

I keep all my code in ~/code

$ c # cd to ~code

$ c pr<tab> # tab complete to c projectname

$ c projectname # cd to ~/code/projectname

This is pretty simple in zsh

# c to cd to ~/code

c() { cd ${CODE_DIRECTORY}/$1; }

# tab complete c against code

_c() { _files -W ${CODE_DIRECTORY} -/; }

compdef _c c

(Note: CODE_DIRECTORY is just an env var for ~/code)

I use this all the time and I use h to do the same for my home directory

https://github.com/gpspake/dotfiles/blob/master/zsh/gpspake/...

Re:ZSH Aliases by fphilipe on Tuesday May 26, 2020 @09:23AM (#23310786)
No need for an alias, a variable (I think that's what they're called) will do:

    hello=~/Documents
Then:

    cd ~hello/world
Or omitting `cd` (requires `setopt auto_cd`):

    ~hello/world
And with `setopt cdable_vars`:

    hello/word
This also works with tab completion:

    hello/<TAB>
Re:ZSH Aliases by wgetch on Tuesday May 26, 2020 @09:57AM (#23311156)
With ZSH, you can use a named directory hash to create such 'aliases' to directories:

    hash -d hello=~/Documents
    ~hello/world
The shortened names also show up in your PROMPT if it is using %~ to display cwd.
Re:ZSH Aliases by fratajcz on Tuesday May 26, 2020 @08:17AM (#23310167)
fish abbreviations kinda do this, for example if you define your alias above as an abbrev and you type hello<space>, it replaces hello by ~/Documents in your command line. You still have to press backspace to remove the space you just inserted, but it's better than nothing.

Also, apparently you can add this to zsh with a plugin: https://github.com/momo-lab/zsh-abbrev-alias

Re:ZSH Aliases by lawry on Tuesday May 26, 2020 @10:47AM (#23311821)
fish-shell abbreviations are really cool, I do miss that in zsh, I'll probably give that plugin a go. It also makes it for other people easier to see what's going on when looking at the previous commands.
Re:ZSH Aliases by smhmd on Tuesday May 26, 2020 @10:38AM (#23311688)
add hash command in your $zshrc

  $ hash -d hello=$HOME/Documents
  $ cd ~hello/world && pwd
  /home/user/Documents/world