Using source inside ~/.zshrc does not work - zsh

I created a a CLI tool with PicoCLI. The library generates an auto-complete script. This works fine when sourced manually, i.e.
source /path/to/script
But when I put the line into ~/.zshrc it doesn't. No errors, just doesn't work. Tried different things like . /path/to/script, source <(cat /path/to/script), added execution permissions, added autoload -U +X compinit && compinit & autoload -U +X bashcompinit && bashcompinit (see here). No success, no idea what's wrong. According to the docs it should be fine (see here).
Thanks for any suggestion!

Related

How to solve zsh compinit: insecure directories issue on MacOS (other solutions failed)

I'm aware there are many copies of this question here, but all of their answers recommend adding
ZSH_DISABLE_COMPFIX="true"
to the top of my ~/.zshrc file. I have done this and still every time I open zsh I am greeted with
zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]?
It seems that others asking this question didn't have the quotes around the true in the first sample, but I have added that. I have also run
source ~/.zshrc
Which as far as I can tell reloads the zshrc configuration. This still gives me the above warning. I'm not sure if any of these details could be relevant but I'll include them:
This is a new zsh installation on an M1 Macbook running Big Sur
I also have Oh My Zsh installed on top of zsh
I earlier ran several export commands to set my nvm directory but I don't think that would be relevant
Any idea how to resolve this permissions issue? Thanks
Edit:
compaudit returns
/usr/local/share/zsh/site-functions
/usr/local/share/zsh
Also, here are the other nonstandard entries in my ~/.zshrc file (in order, but there is some built-in stuff inbetween):
ZSH_DISABLE_COMPFIX="true"
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
export PATH="/usr/local/opt/icu4c/bin:$PATH"
export PATH="/usr/local/opt/icu4c/sbin:$PATH"
export PATH=$HOME/bin:/usr/local/bin:$PATH
plugins=(git)
source $ZSH/oh-my-zsh.sh
zstyle :compinstall filename '/Users/jonahsaltzman/.zshrc'
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
autoload -Uz compinit
compinit
First of all, one problem here is that you’re running compinit twice: Once through OMZ (Oh My Zsh) – when you do source $ZSH/oh-my-zsh.sh – and once manually. You have two options to fix this:
If you want to keep using OMZ, then you should remove the bottom 3 lines from your .zshrc file.
If you want to stop using OMZ, then instead, you should remove both plugins=(git) and source $ZSH/oh-my-zsh.sh
Secondly, note that $ZSH_DISABLE_COMPFIX is specific to OMZ and is not used by compinit itself. It has no effect when you call compinit manually. You can remove it from your .zshrc
Finally, compinit doesn’t show that warning for nothing. Rather than suppress it, you should instead do chmod g-w,o-w on the directories listed by compaudit. That will fix the problem and make the warning go away.
You want to use:
compinit -u
To disable the annoying permissions test. It doesn't add any security on macOS. See the documentation at zshcompsys(1).

Zsh always asks if I mean git blog instead of git log

I work on a lot of websites and often there is a blog directory. Issue is whenever there is i get this
➜ project-name git:(master) ✗ git log
zsh: correct 'log' to 'blog' [nyae]?
Can I just force it to never ever suggest git blog?
Add unsetopt correct_all to your zsh dotfiles (e.g., ~/.zshrc). This will disable this behavior for all corrections.
Seems like this worked from the comment from #gairfowl
autoload -U compinit && compinit
I'm using oh-my-zsh so I'm surprised this didnt work out of the box.

Can't write function to replicate the functionality of Bash's 'help' command

I'm trying to replicate Bash's help command in my .zshrc. I thought this would work:
function help {
bash -c "help $#"
}
but this only works when I only pass a single argument -- for instance, help exit. It does not work when I pass options to help, such as help -m exit (which works in Bash). However, literally typing bash -c "help -m exit" works as expected. I imagine this has something to do with how quoting works in the shell, but I can't figure this out.
You can use
function help {
bash -c "help $*"
}
I suppose you need help for using zsh, not for bash. Bash doesn't know anything about zsh builtins and features, and the common builtins are different enough to warrant separate documentation pages.
The equivalent of bash's help builtin is run-help, but it is not active by default. It will also call man for you and comes with a few useful wrappers.
TLDR; put this in .zshrc
autoload -Uz run-help
autoload -Uz run-help-git
autoload -Uz run-help-ip
autoload -Uz run-help-openssl
autoload -Uz run-help-p4
autoload -Uz run-help-sudo
autoload -Uz run-help-svk
autoload -Uz run-help-svn
alias help=run-help
https://wiki.archlinux.org/index.php/zsh#Help_command

How to specify a custom path for my .zshrc file?

I'm trying to move .zshrc to a folder where I keep this kind of files synced with Github.
But now whenever I start a zsh session it doesn't use that config file.
Assuming I changed the file to ~/.dotfiles how can I add ~/.dotfiles/.zshrc to the PATH(?!) to make zsh start with that config?
Doing source ~./dotfiles/.zshrc only works for that session. Doesn't work anymore if I close the terminal.
You can symlink:
ln -s /path/to/original /path/to/symlink
For the zshrc you can do something like:
ln -s ~/.dotiles/.zshrc ~/.zshrc
One alternative to a symlink is to put this in ~/.zshenv:
ZDOTDIR=~/.dotfiles
If you want .zshenv in ~/.dotfiles as well, you can look into setting ZDOTDIR in one of the global configuration files (/etc/zshenv is a good choice).
Alternatively, you can do what I do and use GNU Stow. I've got my dotfiles in a repository, one subdirectory per category, like so:
dotfilerepo/zsh/.zshrc
dotfilerepo/zsh/.zlogin
dotfilerepo/git/.gitconfig
dotfilerepo/vim/.vimrc
then I can cd into repo and do stow zsh and it'll create a symlink from ~/.zshrc to repo/zsh/.zshrc, another from zsh/.zlogin to ~/.zlogin. stow vim to create symlinks from the vim subdirectory to ~, etc.
I've got a script, install-linkfarm, that does all the stow commands so when I move onto a new machine, I clone my repo, cd to it and run install-linkfarm and am good to go.
You can put this in ~/.zshrc, even as its entire contents:
if [ -r ~/.dotfiles/.zshrc ]; then
source ~/.dotfiles/.zshrc
fi
Please use the export command mentioned below to solve your problem.
export ZDOTDIR=$HOME/.dotfiles
In Linux, you can check if your zsh is loading /etc/zsh/zshrc, and edit it.
If that's the case, redirect this to your custom script by adding:
sh $HOME/.dotfiles/zshrc
Here is an interesting hack that doesn't require you to use sym-links.
In your .xsession, (or .*wmrc) have the following:
xterm -e 'zsh -c ". ~/.dotfiles/.zshrc; zsh"'.
instead of just:
xterm
Make sure to put the -e at the end after all of your other xterm options.

Zsh tries to correct a command to a file

Zsh has a feature that lets it prompt for corrections to files in the current directory. Eg, if I say cd bar when I mean to say cd baz, then zsh will say: zsh: correct 'bar' to 'baz' [nyae]?
Normally, this works fine. However, sudo seems to mess things up. Specifically, suppose I want to version control my apache2 directory with git. I would type something like sudo git add . This is the correct command to run. However, zsh would prompt me with zsh: correct 'git' to '.git' [nyae]? as if it didn't know that git was a command, so it thought I was trying to refer to the .git folder.
Why is this happening? How can I get it to stop prompting me in those situations?
Thanks!
EDIT: It seems like zsh, by default, will consider all arguments to a command to be files or directories. However, I know that there is some functionality to extend this. For instance, if I type git sttab, then zsh will complete it to stash, status, or stripspace (with documentation on each of those). I would, ideally, like zsh to keep providing tips like these even with something like sudo (so, I would rather not do a nocorrect). How do I customize that functionality in zsh?
Either use nocorrect before the command itself, or define an alias
alias sudo="nocorrect sudo"

Resources