ZSH and Oh-My-Zsh local environment mapping? - zsh

I used Bash Shell for a long time and recently switched to ZSH because of the greatness of the project O-My-Zsh.
I have no problems how to use the zsh but setuping the local environment. I am currently using the dotfiles structure from Peepcode screencast, illustrate file-tree below:
Map .bash_profile to .zshrc file, map .zshrc file to the ~/bin/dotfile/zshrc file, zshrc file just load 3 files which is environment, alias, config. ( Those 3 files are the logic separation of the .zshrc file )
That is my setup. It is currently working the way it should. I could use alias which I set in alias file, etc.
Here is my question, the project O-My-Zsh needs the config file like loading the .oh-my-zsh folder and .oh-my-zsh.sh files. It is working if I put .oh-my-zsh config setting in the ~/.zshrc file. Since I mapped .zshrc to another place, how could I still refer to source the O-My-Zsh themes, plugins, settings? How should I source the ~/.oh-my-zsh folder in the clean way?

I think I understand your question, and my current setup may be similar:
In an effort to make setup and sync between various machines, I have moved all of my dotfiles to Dropbox (in a folder called .zsh). A symlink connects Dropbox/.zsh/.zshrc to ~/.zshrc, and Dropbox/.zsh/.zshrc sources all of my various config files, like so:
# Set so that all other sourced files can be found.
export ZDOTDIR="$HOME/Dropbox/.zsh"
source $ZDOTDIR/checks.zsh
# source $ZDOTDIR/colors.zsh
source $ZDOTDIR/exports.zsh
source $ZDOTDIR/oh-my-zsh_opts.zsh
source $ZDOTDIR/setopt.zsh
source $ZDOTDIR/pyopts.zsh
source $ZDOTDIR/prompt.zsh
source $ZDOTDIR/completion.zsh
source $ZDOTDIR/aliases.zsh
source $ZDOTDIR/bindkeys.zsh
source $ZDOTDIR/functions.zsh
# source $ZDOTDIR/zsh_hooks.zsh
Similarly, $ZDOTDIR/oh-my-zsh_opts.zsh defines all of my Oh-my-zsh options:
# Path to your oh-my-zsh configuration.
ZSH=$ZDOTDIR/.oh-my-zsh
# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
# ZSH_THEME="af-magic"
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(battery colored-man colorize cp extract frontend git pip python pyenv\
virtualenv)
if [[ $IS_MAC -eq 1 ]]; then
plugins=($plugins brew brew-cask osx textmate)
fi
if [[ $IS_LINUX -eq 1 ]]; then
plugins=($plugins)
fi
if [[ $HAS_APT -eq 1 ]]; then
plugins=($plugins debian)
fi
if [[ $HAS_YUM -eq 1 ]]; then
plugins=($plugins yum)
fi
source $ZSH/oh-my-zsh.sh

Related

Why tmuxinator zsh complete file is not work?

According to the hint, I download the file
https://raw.githubusercontent.com/tmuxinator/tmuxinator/master/completion/tmuxinator.zsh
into /usr/local/share/zsh/site-functions/_tmuxinator(file path), but the autocompletion not work.
Then I source the file like below in ~/.zshrc, it is also not work.
. /usr/local/share/zsh/site-functions/_tmuxinator
My config environments are:
zsh: zsh 5.8 (x86_64-suse-linux-gnu)
tmuxinator: tmuxinator 2.0.1
And I use oh-my-zsh as my zsh environment and rbenv to install tmuxinator.
The fpath environment variable in my system is:
/home/run/.oh-my-zsh/plugins/git /home/run/.oh-my-zsh/functions /home/run/.oh-my-zsh/completions /usr/local/share/zsh/site-functions /usr/share/zsh/site-functions /usr/share/zsh//functions/Calendar /usr/share/zsh//functions/Chpwd /usr/share/zsh//functions/Completion /usr/share/zsh//functions/Completion/Base /usr/share/zsh//functions/Completion/Linux /usr/share/zsh//functions/Completion/Unix /usr/share/zsh//functions/Completion/X /usr/share/zsh//functions/Completion/Zsh /usr/share/zsh//functions/Completion/openSUSE /usr/share/zsh//functions/Exceptions /usr/share/zsh//functions/MIME /usr/share/zsh//functions/Math /usr/share/zsh//functions/Misc /usr/share/zsh//functions/Newuser /usr/share/zsh//functions/Prompts /usr/share/zsh//functions/TCP /usr/share/zsh//functions/VCS_Info /usr/share/zsh//functions/VCS_Info/Backends /usr/share/zsh//functions/Zftp /usr/share/zsh//functions/Zle /etc/zsh_completion.d
which includes the directory /usr/local/share/zsh/site-functions.
You may have to force rebuild zcompdump:
rm -f ~/.zcompdump; compinit

Change Location of zcompdump Files

Is it possible to change the location of .zcompdump and .zhistory files generated by ZSH?
They are polluting my Home Directory.
One possible solution I found was changing the value of $ZDOTDIR to $HOME/.config/zsh. I did the following, but it does not seem to work (temp files are still created in the home directory).
$ cat ~/.zshenv
ZDOTDIR=$HOME/.zsh
. $ZDOTDIR/.zshenv
.zcompdump files are produced to speed up the running of compinit, Which initializes the shell completion in zsh. You could specify an explicit dump file by compinit -d <dumpfile> in your .zshrc to change the location where compdump are stored.
Change the value for HISTFILE to set different location to store you zsh command history.
$ man zshall
/Use of compinit
To speed up the running of compinit, it can be made to produce a dumped
configuration that will be read in on future invocations; this is the
default, but can be turned off by calling compinit with the option -D. The
dumped file is .zcompdump in the same directory as the startup files (i.e.
$ZDOTDIR or $HOME); alternatively, an explicit file name can be given by
`compinit -d dumpfile'. The next invocation of compinit will read the
dumped file instead of performing a full initialization.

Symlink dotfiles

I am having trouble symlinking dotfiles. I have a folder in my home directory ~/dotfiles which I have synced to a github repo. I am trying to take my .vimrc file in ~/dotfiles/.vimrc and create a symbolic link to put it at ~/.vimrc. To do this I type in
ln -s ~/dotfiles/.vimrc ~/.vimrc
But when I run that it says
ln: /Users/me/.vimrc: File exists
What am I doing wrong?
That error message means that you already have a file at ~/.vimrc, which ln is refusing to overwrite. Either delete the ~/.vimrc and run ln again or let ln delete it for you by passing the -f option:
ln -s -f ~/dotfiles/.vimrc ~/.vimrc
There is a better solution for managing dotfiles without using symlinks or any other tool, just a git repo initialized with --bare.
A bare repository is special in a way that they omit working directory, so you can create your repo anywhere and set the --work-tree=$HOME then you don't need to do any work to maintain it.
Approach
first thing to do is, create a bare repo
git init --bare $HOME/.dotfiles
To use this bare repo, you need to specify --git-dir=$HOME/.dotfiles/ and --work-tree=$HOME, better is to create an alias
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME
At this point, all your configuration files are being tracked, and you can easily use the newly registered dotfiles command to manage the repository, ex :-
# to check the status of the tracked and untracked files
dotfiles status
# to add a file
dotfiles commit .tmux.conf -m ".tmux.conf added"
# push new files or changes to the github
dotfiles push origin main
I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Storing dotfiles with Git where I wrote about managing for multiple devices.
How to symlink all dotfiles in a directory recursively
Have a dotfiles directory that is structured as to how they should be structured at $HOME
dotfiles_home=~/dotfiles/home # for example
cp -rsf "$dotfiles_home"/. ~
-r: Recursive, create the necessary directory for each file
-s: Create symlinks instead of copying
-f: Overwrite existing files (previously created symlinks, default .bashrc, etc)
/.: Make sure cp "copy" the contents of home instead of the home directory itself.
Tips
Just like ln, if you want no headache or drama, use an absolute path for the first argument like the example above.
Note
This only works with GNU cp (preinstalled in Ubuntu), not POSIX cp. Check your man cp, you can install GNU coreutils if needed.
Thanks
To this and this.

Relative path to executable is not resolved in zsh script

I have a personal scripts folder: ~/google_drive/code/scripts. This folder is on my $path. $path is set in ~/.zshenv and not changed anywhere else (I've disabled OS X path_helper and I don't touch $path in any other zsh startup file). In this scripts folder, there is a subdirectory called alfred_workflows. From the command line, from any location I am able to run scripts in this folder with relative paths. This is expected:
$ alfred_workflows/test.sh
#=> test successful
But in a script, this does not work. It generates an error:
$ zsh -c "alfred_workflows/test.sh"
#=> zsh:1: no such file or directory: alfred_workflows/test.sh
Once again, the scripts directory that contains alfred_workflows is on $path, which is set in ~/.zshenv, and I am able to run executables that reside in the top level of this directory from a script. The issue only seems to be when I try to go through a subdirectory. What might be the problem?
Searching of the $path is only done for names containing a slash if the
path_dirs option is set. Apparently that's set in your interactive shell,
but isn't set in the process that's executing the script.

Displaying a custom message when cd'ing into a directory

I'm looking for a way to display a custom message when cd'ing into a directory. For example
$ cd some_folder
Warning: Don't edit these files!
some_folder $
From an old post I found the suggestion of adding this to my .bashrc file:
reminder_cd() {
builtin cd "$#" && { [ ! -f .cd-reminder ] || cat .cd-reminder 1>&2; }
}
alias cd=reminder_cd
With this script, if I have a file .reminder_cd in my folder, the contents of that file are displayed when I cd into it.
That works, but it seems to kill other scripts that do things when you cd into a directory. Specifically, it kills the ability for Ruby RVM to use .rvmrc to switch ruby versions when you cd into a directory.
Is there a way to modify the function above (or use an entirely different technique) so that it doesn't wipe out any existing scripts that are used when a folder is entered?
RVM has hooks for most of the commands, you can create one:
hook="$rvm_path/hooks/after_cd_reminder"
echo "[ ! -f .cd-reminder ] || cat .cd-reminder 1>&2" > "$hook"
chmod +x "$hook"

Resources