.zshrc isn't the only startup file, if you place everything there you're likely doing it wrong and should split things like some environment settings to $ZDOTDIR/.zshenv.
Using $SHELL is not great because it is not a reliable env var. It's only set for login shells, and if your login shell isn't zsh you'll get a different shell on this "reload". (A long time ago I had an account somewhere where my sysadmin didn't respond to chsh request for whatever reason, so my login shell remained /bin/bash while I actually used zsh day to day.) It can also be anything, really:
export SHELL=/sbin/nologin # or /usr/sbin/nologin, or equivalent on your system
exec -l $SHELL
You want zsh, so just use zsh.
As for -l, exec zsh -l and exec -l zsh both replace the current shell with a login zsh, so they are effectively the same.
Really? Last time I downloaded a .js file by accident was never if my memory doesn't fail me (.html I remember a couple). Even if I did, I normally use the UI to manage my files. The only time I might try to do something with the CLI and a JS file is exactly to run `node file.js`, and this is just a convenience.
Worse, imagine if someone set their shell as the suffix alias for .sh, just to save time on their curl shell pipes... Oh the horror. Suffix aliases should come with a big caveat.
Well, first I thought about publishing 4 types (I see functions as a valid kind of alias)
However, the os-specific aliases became more and more important to me due to GitHub and VisualStudio Codespaces. Targeting different operating systems is quite important to me.
But technically it's just a regular alias. (Which is also mentioned in the corresponding paragraph)
i think one of the main advantages of zshs aliases (compared to bash aliases) is autocompletion, so I would not count regular functions as a kind of alias. But I can see the similarities.
the kgb and cia one for kubernetes always cracks me up :)
btw kubens is great
---
alias k="kubectl"
alias kns="kubens"
alias kgb="kubectl get pods"
function kex () {
kubectl exec -it $1 sh
}
function kf () {
kubectl port-forward $1 $2:$3
}
function cia () {
kubectl delete pod $1
}
function kl () {
kubectl logs -f $1
}
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.
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.
No, this really only addresses the situation where the URL ends with (suffix) “git”.
The only time I use the full URL is the initial cloning, and the remote name thereafter. But if you use the full URL in another scenario, yes, this may interfere.
I don't believe so since I think suffix aliases are only affected by the "file extension" of the command you've added. The suffix alias in the OP is saying that if they paste something that ends with ".git", then clone that thing (i.e. if you paste the git url for a repo, it clones it). It doesn't overwrite the git command
As others commented, this only acts when the first word in your commandline ends with '.git'.
- "git", alone isn't going to be hooked.
- you can do "cd .git".
- even if you have 'autocd' option set, you can '.git' and it will do "cd .git".
I'm not saying it's even a good practice, but I find it kinda cool, when using this feature in a way it's probably not intended for, you start thinking in 'what might happen inside'.
Speaking of ZSH: it is the thing I miss the most in Windows (especially with ohmyzsh, and its plugins zsh-autosuggestions and zsh-syntax-highlighting).
I know that there is oh-my-posh, but it lacks such plugins. Do you know any workaround?
This can get you a part of the way, extracted from my PowerShell profile:
##
# PSReadLine, see https://github.com/PowerShell/PSReadLine
##
## behaviour of Tab key autocomplete
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
## From docs:
## With these bindings, up arrow/down arrow will work like PowerShell/cmd if the
## current command line is blank. If you've entered some text though, it will
## search the history for commands that start with the currently entered text.
##
## Like zsh completion.
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
These will allow you to autocomplete previous commands from the tidpids you already types, so it no longer searches linearly through the history.
I also use:
No, but mostly because it is not much longer than the above! Only got started using PowerShell more seriously (thanks to Windows Terminal and WSL) a couple of months ago.
Here is the other content:
# For Agnoster theme, "user@host" will be hidden if user==DefaultUser
$DefaultUser = "<YOUR USERNAME>"
function vim ($File){
bash -c "vim $File"
}
New-Alias pi ipython
# git diff output uses less; it will be buggy without utf8
$env:LESSCHARSET='UTF-8'
# console output, e.g, when writing git diff output to file using pipe: | Out-File
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
I also love them global aliases! To push them even further (like the xargs one), I have those aliases to allow me to do quick iterations in 'streaming' fashion:
alias -g FORI='| while read i ; do '
alias -g IROF='; done '
Which "yank" package are you using? I came across one [0], which copies the file selected to the clipboard instead of outputting it to stdout.
I managed to get it to print to stdout, though:
Wow I really can’t disagree with this hard enough.
I’ve added extensive aliases to my shell and it saves me mountains of time. If they break, so what? You fix them? It’s computers; everything breaks eventually.
I’m appalled at the idea of trading constant convenience against some hypothetical and unspecified future breakage.
I don't understand what the negatives of typing `gs` over `git status` is.. Unless I have a bin named `gs`.. but as long as I know that, what are the disadvantages?
I prefer something like "alias sp=trash" (sp for suppress), and getting into the habit of using this alias (for instance by doing alias rm="echo nope"), so that if I use it on another machine by mistake, it just fails instead of using /bin/rm (and I can then resort to trash or double-check that my rm command is not faulty).
Also worthwhile investing some time in the completion system if you are writing aliases to save time. I've done it for serverless invoke/deploy/logs and they've been so useful!
What I'm talking about is typing 'sls logs <tab>' and having tab completion for the deployed functions when typed in the root of the repo containing the serverless.yml file.
Similarly, project autocompletion for the directory where I do my web dev ...
export SITES=$HOME/Sites
# Plural to cd and list contents
alias sites="cd $SITES; ll"
# Singular to access a particular project
site() { cd $SITES/$1; }
# Autocomplete function
_site() { _directories -W $SITES -/; }
compdef _site site
It works from within any directory, which is nice.
About suffix aliases, shouldn't one resort to file associations instead, and use the corresponding command (possibly with an alias)? It seems weird to have these settings stored in two places, and have inconsistent results when you open from the terminal and from another application (like a file browser).
What's the benefit of suffix aliases compared with setting up the associations? You only save two keystrokes if you have a single-letter alias (for xdg-open on linux).
I love ZSH + Oh My ZSH. And I didn't even have a choice.
At my first company, the onboarding script included switching to ZSH+OMZ. I knew it changed the shell, but thought it was just theming - not much. A year later, switched companies, didn't install it. Don't know how I lived without it. It is such an amazing productivity update to shell life I consider it a must on every machine.
> I do a lot of conference speaking; if you are presenting at conferences or meetups too, you should start your presentation by quickly explaining the aliases you will use throughout the talk to onboard your audience.
If you're giving a presentation, please give the actual commands people need to type, even if that's a little more typing for you. There are tools to help you auto-type each line of a demo, if needed. Reduce the amount of confusion and translation people need to go through to understand your presentation.
Similarly, turn off any fonts that create symbolic ligatures, so people know what symbols to actually type.
Avoid anything unnecessary that increases the distance needed for understanding between you and your audience.
I think Fish shell "abbreviations" [1] are more suitable for interactive demos than standard Bash/Zsh aliases. Basically, instead of e.g. `gc` meaning `git commit` (alias), `gc` can be expanded to `git commit` (abbr), so you can save typing but still show the actual command.
Modern versions of Fish has both abbreviations and aliases. Personally, I prefer aliases for providing default arguments to commands (e.g. turning on colorized output), but prefer abbreviations for timesavers like the `gc` above.
Is there a way to get such expansion functionality for zsh? I would always use it, not only when demonstrating something but don’t want to switch to Fish only for that feature.
This points to the newest file/dir in my current dir, and it is very easy for me to untar a downloaded file and then cd into it without caring about the name of the file/dir.
107 comments
[ 3.3 ms ] story [ 154 ms ] thread> source $HOME/.zshrc
.zshrc isn't the only startup file, if you place everything there you're likely doing it wrong and should split things like some environment settings to $ZDOTDIR/.zshenv.
Better way to reload startup changes:
As for -l, exec zsh -l and exec -l zsh both replace the current shell with a login zsh, so they are effectively the same.
Technically it's $ZDOTDIR/…
Also note from the official documentation:
> `.zprofile' is meant as an alternative to `.zlogin' for ksh fans; the two are not intended to be used together
I can see opening up in an editor as useful shortcut. But actually executing code without an executable bit seems a bit more dangerous.
Tldr: 1. aliases, 2. suffix aliases, 3. global aliases
However, the os-specific aliases became more and more important to me due to GitHub and VisualStudio Codespaces. Targeting different operating systems is quite important to me.
But technically it's just a regular alias. (Which is also mentioned in the corresponding paragraph)
>You have seen four different types of ZSH aliases that will boost your productivity
`four` in these two lines is typo or intentional? Because the title says `5 Types Of ...`
> The registration of aliases with parameters looks pretty similar to a function definition in regular programming languages.
Well that's because it is a function definition in this programming language, no?
Furthermore, this breaks down when you, say, use `alias` to view all your aliases. It won't show the functions, because they are not aliases.
btw kubens is great
---
---https://thorsten-hans.com/5-types-of-zsh-aliases
if your ad blocker is barfing on whatever bitly-based tracking doodad was submitted instead.
(Self, you idiot)
cd'ing into a directory on the fly without even thinking about paths and just opening files by name is powerful juju
Together With AUTOCD, this feels like it shouldn't be possible...but it is.
For example if I set the following alias:
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.
Also, apparently you can add this to zsh with a plugin: https://github.com/momo-lab/zsh-abbrev-alias
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/...
Overwriting a command-line tool with no other arguments seems like a bad idea.
The only time I use the full URL is the initial cloning, and the remote name thereafter. But if you use the full URL in another scenario, yes, this may interfere.
happy hacking :)
I know that there is oh-my-posh, but it lacks such plugins. Do you know any workaround?
Here is the other content:
- alias -g G="| grep " (`cat test.txt G some_words`) - alias -g CC="| pbcopy " (`cat test.txt CC`) - alias -g "?"="| fzf " (`ls -l ?`) - alias -g X="| xargs " (`ls -l X rm`), probably my favorite (makes xargs kinda look like a pipe) - alias -g Y="| yank "
They compose well, too.
- Pick some file to delete from `git status`: `git status Y X rm`
Could be clearer, basically it means that if it's part of a pipe, it's going to print to stdout instead of copying to the clipboard
also, if you use aliases for commands that you use a lot, it is better to remember how to correctly type the command.
I’ve added extensive aliases to my shell and it saves me mountains of time. If they break, so what? You fix them? It’s computers; everything breaks eventually.
I’m appalled at the idea of trading constant convenience against some hypothetical and unspecified future breakage.
I have a habit of cleaning up the workspace in my machine. This alias saved me a 1000 times already.
Although I do wish I could pass a flag that didn’t open it, as sometimes I do want to append or overwrite a file without the fuss of an editor.
% foo.jpg
# launches image viewer
% cp foo.jpg bar.jpg
# unaffected
What I'm talking about is typing 'sls logs <tab>' and having tab completion for the deployed functions when typed in the root of the repo containing the serverless.yml file.
https://github.com/larrybolt/zsh-aliases/blob/master/serverl...
e.g. `z p` might take you to ~/projects if you've been there recently.
https://github.com/skywind3000/z.lua
https://github.com/clvv/fasd
For example if I'm in "/Documents/Dotfiles", but want to go to "/Documents/Blog". Doing the "../" dance is tedious as hell.
% Documents/Dotfiles
[~/Documents/Dotfiles] % ../Blog
This does the trick.
For example, I use:
``` alias open=xdg-open ```
To call the real open command, I can use \open.
What's the benefit of suffix aliases compared with setting up the associations? You only save two keystrokes if you have a single-letter alias (for xdg-open on linux).
At my first company, the onboarding script included switching to ZSH+OMZ. I knew it changed the shell, but thought it was just theming - not much. A year later, switched companies, didn't install it. Don't know how I lived without it. It is such an amazing productivity update to shell life I consider it a must on every machine.
If you're giving a presentation, please give the actual commands people need to type, even if that's a little more typing for you. There are tools to help you auto-type each line of a demo, if needed. Reduce the amount of confusion and translation people need to go through to understand your presentation.
Similarly, turn off any fonts that create symbolic ligatures, so people know what symbols to actually type.
Avoid anything unnecessary that increases the distance needed for understanding between you and your audience.
Modern versions of Fish has both abbreviations and aliases. Personally, I prefer aliases for providing default arguments to commands (e.g. turning on colorized output), but prefer abbreviations for timesavers like the `gc` above.
[1]: https://fishshell.com/docs/current/cmds/abbr.html
alias -g NF='./*(oc[1])'
This points to the newest file/dir in my current dir, and it is very easy for me to untar a downloaded file and then cd into it without caring about the name of the file/dir.
tar xf NF; cd NF
mv ~/Downloads/[C-x m] /tmp
or even within files that match what I've typed already:
tail /logs/pho[C-x m]