Reputation: 39034
Not sure why this is happening.
I use iterm2, and have .zshrc bash
When I run git branch
It displays my branches "in a new window" is there a way to fix this?
My config:
alias projects="cd ~/Projects"
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
export PATH=~/.node/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/lgaban/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="robbyrussell"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )
# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"
# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"
# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"
# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"
# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13
# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"
# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"
# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"
# Uncomment the following line to display red dots whilst waiting for completion.
# Caution: this setting can cause issues with multiline prompts (zsh 5.7.1 and newer seem to work)
# See https://github.com/ohmyzsh/ohmyzsh/issues/5765
# COMPLETION_WAITING_DOTS="true"
# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"
# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder
# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)
source $ZSH/oh-my-zsh.sh
# User configuration
# export MANPATH="/usr/local/man:$MANPATH"
# You may need to manually set your language environment
# export LANG=en_US.UTF-8
# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
# export EDITOR='vim'
# else
# export EDITOR='mvim'
# fi
# Compilation flags
# export ARCHFLAGS="-arch x86_64"
# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
Upvotes: 10
Views: 2619
Reputation: 489083
This is not actually a new window, and it is not vim either: it is less
, which is the default pager on a lot of systems including yours. The git branch
command uses the pager by default now (as of Git 2.16); see in particular this answer to Git branch command behaves like 'less'.
There is a further bad interaction here, worthy of note, that affects those who use zsh
but not those who use bash
, at least with many of the various defaults that are included with these shells. Explaining what is going on requires a bit of background, though the standard fix is easy to list:
export LESS=FRX
in your .zshrc
(note: some people prefer FRSX
).
In the old days of "glass ttys" (Wikipedia), there was just the screen. If you cleared The Screen, it was clear. If you wrote text to it, it had that text. These things were just emulating printers, without using paper. But they acquired new capabilities, such as highlighted or bold or underlined text, fancy cursor-addressing systems, and so on.
Some terminals required special "enable cursor motion" setup operations before they could do fancy screen drawing. (It's not really clear why: did they think someone would start cursor-addressing mode by mistake? But they did.) So early software for using this stuff had a feature: it would send an "initialize" sequence.
One small subset of terminals grew an alternate screen. The VT100 and follow-ons used an escape sequence, ESC [ ? 4 7 h, to enter Alternate Screen mode, and the same but ending with lowercase L (l
) instead of h
, to exit it. The Alternate Screen was literally that: switching to it got you a different screen, and switching back put your old screen contents back.
As windowing systems became commonplace, terminal emulators running on pseudo-terminals took place of the old "glass ttys". You could open lots of new windows and have lots of individual mini-screens. Each one could emulate some favorite existing physical terminal, such as the VT100 or VT220. These particular escape sequences were even standardized (ANSI X3.64, ISO/IEC 6429), with various extensions.
These became xterm
, which, in Biblical terms, begat (at least indirectly) iTerm and iTerm2 and others. Alas, someone got it into their heads that this was a good idea in the initialization of an xterm, into cursor-positioning screen-editor mode, that this should include the "switch to alternate screen" sequence. Exiting this mode switches back. The result is that your editor session doesn't disturb any ongoing command session—which some find nice, I guess—but the additional consequence of this decision is that when less
sends this same initialization sequence, this also flips to the alternate screen. The escape sequence is sometimes slightly different now,1 but it's the same underlying problem.
As far as I am concerned, this entire thing is a horrible idea in the first place (see also this superuser.com question and this web site). If you can completely kill off the alternate screen—as you can in xterm—that's the ultimate cure. Alas, these days you often can't. An alternative I use is to modify the termcap or terminfo to remove the alt-screen escape sequences. This is a bit tricky on many systems.
1I have no idea why. If anyone does know, that might make interesting lore.
less
and GitThe less
program has its own cure for the alternate-screen blues: the -X
command line option tells less
do not emit the initialization sequence. This is a pretty big hammer, but it works fine.
Git has a compiled-in default pager, set by whoever builds your Git distribution. The usual default is less
.
To make less
itself convenient to use, less
reads the environment variable LESS
at startup. Any options set here are applied by default unless you override them on the command line. So you can set F
, R
, S
, and/or X
—all of which are commonly-desirable options—in $LESS
and less
will use those settings.
Git will, if LESS
is not set in the environment, automatically set it to FRX
. Again, this is actually a compiled-in default, but the "default default" is LESS=FRX LV=-c
. (LV
is for the lv
command.)
Curiously, some standard zshrc, distributed with zsh, apparently does an export LESS=R
. This means that LESS
now is set in the environment, and Git carefully does not disturb it: Git assumes that you meant to have it set this way. When you switch to bash and your bashrc does not set LESS
, Git sets it for you; when you switch to zsh and your zshrc does set LESS
, Git does not override it.
The cure, then, is to set (and export) the LESS
settings you want, assuming you will continue using less
. Note that you can configure your pager in Git itself, using GIT_PAGER
or core.pager
; see the top level git
command documentation. You can set options there too; these will override the LESS
settings. For instance, you could set core.pager
to less -FRSX
to get those four options just in Git, while leaving $LESS
set to, say, just R
, to get just that one option when you run less
yourself.
Here's what the four options mean:
F
: short for --quit-if-one-screen
, this makes less
exit immediately, without displaying (END)
or similar. This is quite useful with short git branch
output.
R
: short for --RAW_CONTROL_CHARS
, this makes less
recognize ANSI color escape sequences so that git branch
can show the current branch in green, for instance, or show colored git diff
output.
S
: short for --chop-long-lines
, this makes less
truncate long lines, requiring "sideways scrolling". S
really stands for Side Scroll, at least in my own head. The default, without -S
, is to wrap long lines, so that no sideways scrolling is required.1
X
: short for --no-init
, this inhibits the initialize (and final de-initialize) sequences, which in iTerm2 are what is making your git branch
output disappear when you exit the pager.
Note that you can disable Git's pager entirely, and use your terminal's scroll-back, if you prefer that.
1I almost like the S
setting myself, except for one flaw: there's no indication that a long line got truncated and that scrolling is required. So someone can, e.g., hide a secret message by putting it way off to the right of each line. In practice, this means that some git diff
output tends to get cut off sometimes, and however rare it is, it's just annoying enough to keep me from setting -S
most of the time.
There's a secret here though: you can, while inside less
itself, turn these options on and off! If you have long lines wrapped and it would be nice to have side scrolling on now, just turn that on. Type - S and press RETURN or ENTER at the colon prompt to toggle the setting off and on.
Upvotes: 17
Reputation: 60393
I can get very similar results with LESS= git branch
. say git help config
and look through the /pager
results, also the various (git-specific and non-) environment variables that control pager behavior, the environment git and the pager it invokes are running in make the pager give the full behavior even on short results.
The simplest option that tells Git to always use the options it normally supplies only when you haven't got a default is
git config core.pager 'less -FRX'
and you can control this with more granularity if you like, see the docs.
Upvotes: 7