disable history substitution "preview" in zsh - zsh

in bash, if i enter:
mkdir /tmp/foo
cd !$
bash substitutes /tmp/foo for the bang-dollar, and executes the command
if i do the same in zsh, zsh fills in /tmp/foo for the bang-dollar and shows me the resulting command, pausing to let me again hit before executing:
mkdir /tmp/foo
cd !$
cd /tmp/foo # command prompt at the end of this line
but i don't want to do see a preview, i want it to do what bash did.
is there a way to un-preview in zsh?

It sounds like you have the HIST_VERIFY option set; you can disable it with
setopt NO_HIST_VERIFY

Related

default to zsh interpreter in Mac Terminal?

How can I make sure .sh files are executed with the zshell interpreter from the Mac terminal without specifying the zsh prefix every time? The shell default is set to zshell already but a command to run a .sh file won't execute unless I prefix with zsh...
To change the shell to zsh: chsh -s $(which zsh) (zsh is probably the default shell already).
To run a .sh script without having to use zsh command:
prefix the script with a shebang line such as #!/usr/bin/env zsh.
make the script executable: chmod +x <filename>
run it as ./<filename>

Tmux name window at creation and use the name in zsh command

Currently, in my tmux.conf I have the following binding which allows me to name a window when I create a new one. Then the window opens in the same directory I was in the last window:
# Name windows before you create them
bind-key c command-prompt -p "window name:" "new-window -c '#{pane_current_path}'; rename-window '%%'"
I want to achieve the following instead:
When I type the name, it will create the window and execute a bash command like z <TYPED_NAME> - the idea is that it will use the name I typed to search for the most relevant directory (using the z command for example) and cd into it. Can that be achieved?
If no relevant directory is found with z it will use the current behaviour which is to cd into the current path.
One problem is that only the first %% in the template is replaced. The other is that a cd command done in a subshell will be lost when you return to the parent shell. For bash you can try a binding something like this:
bind-key c command-prompt -p "window name:" \
"new-window -c '#{pane_current_path}' 'myfn \"%%\"'"
This runs a bash script myfn that must be in your PATH to do the work. It contains:
#!/bin/bash
name=$1
tmux rename-window "$name"
cd ../"$name" || echo "failed to cd to $name" >&2
exec bash -i
Remember to chmod +x the script. It calls tmux to rename the window, and then you would have your z code to find and cd to a directory. In this example, I just look in ../ for it. The final exec ensures we replace the current shell by an interactive one.
For zsh, you can do something similar, but if you want myfn to be found in your fpath you need new-window to run zsh with the -i option, as zsh only does this for interactive shells. So use a binding like
bind-key c command-prompt -p "window name:" \
"new-window -c '#{pane_current_path}' 'exec zsh -ic \"myfn %%\"'"
Here I've added an exec to avoid having a pointless parent shell, and since -c must be followed by the command as a single string I've put myfn and %% inside the same double-quotes. You'll need to add more quoting if your name can contain whitespace etc.
The myfn zsh script can be the same, with bash replaced by zsh.
I don't know enough about zsh to avoid the final exec zsh -i which is needed to stop the zsh -ic shell from terminating.

Why is fc -l 1 (aka history 1) in zsh script limited to only 30 commands?

When I run a fc -l 1 from the shell I get back the full history from .zsh_history. When I run the same command from a script its only being limited to the last 30. Any ideas why?
I have the following zsh history settings from oh-my-zsh
if [ -z $HISTFILE ]; then
HISTFILE=$HOME/.zsh_history
fi
HISTSIZE=10000
SAVEHIST=10000
setopt appendhistory
setopt extended_history
setopt hist_expire_dups_first
setopt hist_ignore_dups # ignore duplication command history list
setopt hist_ignore_space
setopt hist_verify
setopt inc_append_history
setopt share_history
If you put an echo $HISTSIZE into your script it's probably 30. Scripts do not source the .zshrc, so your history settings are not known to the script.
Put the history specific options into its own file history.zsh and source it from the script AND use fc -R to read the history file before any other call to fc:
source ${ZDOTDIR-$HOME}/.zsh/history.zsh
fc -R
...
fc -l 1

run zsh script in specified folder

How to run zsh script in specified folder? How to specify a folder to run a script:
zsh script_name.sh
Documents said that: "-s Force shell to read commands from the standard input. If the -s flag is not present and an argument is given, the first argument is taken to be the pathname of a script to execute." but it does not work.
What is the difference between zsh -c ~/path1/ script1.sh (2 parameters) and zsh -c ~/path1/script1.sh?
You should just open a subshell, Execute the following from zsh or bash (including the parentheses):
(cd ~/path1 && source script1.sh)
Note: If your script is written for zsh, name it script1.zsh instead, since zsh syntax is not retro-compatible with old sh's.
This should work:
zsh -c "cd ~/path1 && ./script_name.sh"

line doesn't execute in .bash_profile and .bashrc on my Mac OSX Snow Leopard

I just installed Ruby Version Manager (RVM) which is working fine but it asked me to put the following line in my /.bash_profile and ~/.bashrc files:
if [[ -s /Users/tammam56/.rvm/scripts/rvm ]] ; then source /Users/tammam56/.rvm/scripts/rvm ; fi
looking at the content I see the following:
tammam56$ cat /.bash_profile
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
if [[ -s /Users/tammam56/.rvm/scripts/rvm ]] ; then source /Users/tammam56/.rvm/scripts/rvm ; fi
tammam56$ cat ~/.bashrc
export PATH=/usr/local/bin:$PATH
export MANPATH=/usr/local/man:$MANPATH
if [[ -s /Users/tammam56/.rvm/scripts/rvm ]] ; then source /Users/tammam56/.rvm/scripts/rvm ; fi
However when I start new Terminal window it doesn't execute the command I know this as I set my default Ruby verion to 1.9 and if I execute the line manually I get to the correct version:
tammam56$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
Macintosh-37:~ tammam56$ if [[ -s /Users/tammam56/.rvm/scripts/rvm ]] ; then source /Users/tammam56/.rvm/scripts/rvm ; fi
Macintosh-37:~ tammam56$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
Any ideas how I can fix that?
Thanks,
Tam
If you have a ~/.profile try adding the following line to it.
if [[ -s /Users/tammam56/.rvm/scripts/rvm ]] ; then source /Users/tammam56/.rvm/scripts/rvm ; fi
Have you confirmed that other commands in .bashrc and/or .bash_profile get updated properly?
Next you should confirm whether Terminal is starting the shell as a login shell or not (see under Preferences -> Startup).
From the bash man page:
When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
I found that the easiest solution to this issue was to move all of the contents from my ~/.bashrc file (so my ~/.bashrc is now empty) to a new ~/.bash_profile file. Now all Terminal.app tabs automatically run all lines included in ~/.bash_profile. This also applies to iTerm.

Resources