How to mark the start of the shell session from Vim? - shell

When I am using vim-figitive it often shells out for commands like git push and so on.
Every time it opens a shell I see some leftovers from the previous session, something like:
Press ENTER or type command to continue
It sets me off every time because I can't figure out if this is the output from the current session or the previous one.
Is there a way to mark it with some sort of line every time I shell out from vim? Something like this in .zshrc would do it I think:
if [ -n $VIM ]; then
echo "----------------------------------------------------------------------------------"
fi
Update
It works when I run :sh, but not when I run commands from vim-fugitive. Is there is a way to intercept system() calls or whatever it is using?

From #tpope:
My recommendation would be to look into disabling the "alternate
screen" for your terminal emulator (or terminal multiplexor). That's
what I do.
Just add the following to tmux.conf
# tmux.conf
# For tmux , the alternate-screen option defaults to on
set-window-option -g alternate-screen off

Related

How to quickly show program help in pager

I often find myself appending --help|less to commands that I am crafting a command in a shell to explore the CLI of the program I am using. What ways are there to accomplish the same thing with fewer key-presses?
For instance, if I wanted to use the new way of switching branches (I don't, I'm stuck in my ways happily doing git checkout -b other_branch) then my history (and thought process) could look like
git change other_branch # Guess what the command should be
git --help|less # Backtrack to where I am confident I know the command and ask for help
git switch --help|less # Gradually build up the command from there
git switch other_branch
For context:
I typically want the pager either to be able to search or because I am working in tmux and activating scrolling takes a few additional, and awkward, key-presses.
I typically use zsh on Ubuntu or Debian.
I typically use arrow up to iterate on my previous command.
Add the following to your .zshrc file:
# Alt-H: Open `man` page of current command.
unalias run-help
autoload -Uz run-help{,-{git,ip,openssl,p4,sudo,svk,svn}}
Then restart your shell.
Now, whenever you're in the middle of typing a command, you can press AltH (^[h) to immediately open the man page for that command. Then, after you quit your pager, Zsh will will automatically restore your command line, so you can finish typing.

Searching your command history on macOS terminal

What is the shortcut to search my command history in macOS terminal?
For how long is the history available for searching? Where is it stored?
How about using Ctrl+R for searching on the Terminal Utility in Mac for searching on the command history,
dudeOnMac: freddy$ whoami
freddy
(reverse-i-search)`who': whoami
Well for controlling how long the history would be retained that depends on a few shell environment variables, HISTFILESIZE which is nothing but number of lines of history you want to retain. Set a huge value for it in .bash_profile for it to take effect
HISTFILESIZE=10000000
Use Ctrl + R for searching a command from history in Terminal.
(reverse-i-search)`':
Type any substring of the command you want to search e.g. grep
(reverse-i-search)`grep': grep "XYZ" abc.txt
It will return the latest command that matches your input. If that is not the command you were searching for, keep pressing Ctrl + R for next match until you find your command.
Once you found your command press Return to execute it.
If you want to exit without running any command, press Ctrl + G
PS: This answer is same as suggested by Inian, just giving more details for easy usage.
The command history is stored under your home folder in a hidden file called .bash_history. To view it's content in nano, use the following command in Terminal:
nano ~/.bash_history
Or open with your text editor (default is TextEdit):
open ~/.bash_history
In my case it's a very long list and as I scroll through seems like the last ~500 command is stored here.
Migrating an answer to SO from this answer on the Unix and Linux Stack Exchange:
Pressing ctrl+R will open the history-search-backward. Now start typing your command, this will give the first match. By pressing ctrl+R again (and again) you can cycle through the history.
If you like to be super lazy you can bind the up/down arrow keys to perform this search, I have the following in my .inputrc to bind the up/down arrow key to history-search-backward and history-search-forward:
# Key bindings, up/down arrow searches through history
"\e[A": history-search-backward
"\e[B": history-search-forward
"\eOA": history-search-backward
"\eOB": history-search-forward
Just type something (optional), then press up/down arrow key to search through history for commands that begin with what you typed.
To do this in .bashrc rather than .inputrc, you can use:
bind '"\e[A": history-search-backward'
Use this command -
history
This works on both OSX and Linux.
History is stored in ~/.zsh_history or ~/.bash_history or ~/.history depending on your shell.
History is stored for 1000 or 2000 lines depending on your system.
echo $HISTSIZE
You can also try the following:
history | grep 'git'
Where 'git' is the command you are looking for.
For those who want to search specific command from history, you can do so with reverse-i-search. Reverse search allow you to type in any key words(any) that is part of the command you are looking for and reverse search navigate back to history, match previous commands incrementally and return the entire command.
It is especially useful as when one cannot remember all handy lengthy commands they use often. To do reverse-search ctrl + R and type any clue you have and that will return your previous commands matching the words you type. Then once found the command, hit Enter to execute it directly from search.
Automation AppleScript
Since you mentioned viewing your history as a quick solution, via the Terminal.app. You might want to automate, or quickly view history, maybe from the dock. You may use the AppleScript application as one alternative. This is an optional approach to create a simple shortcut, as to many others.
Open the AppleScript editor application.
Add your specified commands, for history.
Code
tell application "Terminal"
do script "history"
end tell
Save as application, drag to dock for convenience.
History Storage & Time Stored Details
HISTSIZE Determines how many lines will be written to the history file.
HISTFILESIZE Determines how long the file.
Find out how long history is stored:
echo $HISTSIZE $HISTFILESIZE
Note: You may also increase your command history storage size in the length of two variables. You may achieve this through HISTSIZE and HISTFILESIZE environment variables which are located in your ~/.bash_profile file.
It is possible to achieve this by modifying ~/.bash_profile, the number placeholder with SIZE represent's the number, lines value as example:
export HISTFILESIZE=SIZE # Example 1000
export HISTSIZE=SIZE # Example 10000
Pre macOS 11 Big Sur
cat ~/.bash_history
HISTFILESIZE will only set a maximum history value which is stored to the history file when a session is started. HISTSIZE will determine specifically how many lines will be stored or in other words, written at the end of the session. If the set HISTFILESIZE is determined to be a large value than what HISTSIZE is set, you will not view history larger than your set HISTSIZE. The reason is that the history file is overwritten with the HISTSIZE unless using histappend option turned ON.
You may use also histappend to append history, If the histappend shell option is turned on lines are appended to the history file. Otherwise, the overwritten alternative proceeds.
Bash GNU - histappend
macOS 11 Big Sur
nano ~/.zprofile
Modify history environment variables, set to a value:
export HISTFILESIZE=1000
export HISTSIZE=SIZE=1000
Run the source command can be used to load any functions file into the current shell script or a command prompt.
source ~/.zprofile
echo $HISTSIZE $HISTFILESIZE
Outputs:
1000 1000
Output where some history is stored:
cat ~/.zsh_history
For macOS Big Sur the file is now .zsh_history
If you do vi ~/.zsh_history in the terminal you can use regex by pressing the / and then the search term.
To review or recall recently used commands, you can just press the up arrow key to sequentially read back through the history stored in .bash_history.
To search through history with ease, I advise you to install fzf.
It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.
Just install it, click ctrl + R, and you'll be to scroll through you shell history, without the need to grep or waiting ages until the command you're waiting for pops up.
It supports Mac OS, Linux and even Windows.
# USAGE: find.history cd
# filter commands in shell history by a search term and execute the selected command
function find.history {
eval $(history | grep "$1" | tail | awk '{$1=""}1' | tail -r | peco)
}
You will need to have peco installed.
https://github.com/peco/peco
[$]> brew install peco

Open a small window while in Vim for executing commands

I need to write lot of code and compile very often. I hate switching back and forth various windows just to compile the code. Is it possible to open a small window at bottom and run invoke shell and close that window when needed?
With GVim or MacVim, you can run external commands in the command-line: Gvim/MacVim comes with a (very) limited shell that will happily show you whatever the compiler outputs. The general usage pattern is:
:!command
:!command %
With CLI Vim, the same method will pause Vim and return to the shell to execute your command.
In both cases, you'll get a message asking you to press ENTER to come back to your normal editing.
Using :make | cw would be a slightly more sophisticated alternative, with the added bonus of showing the errors in the quickfix window.
An even more sophisticated approach would be to use Tim Pope's Dispatch plugin in combination with tmux or screen.
Sounds like a problem for Screen
http://www.gnu.org/software/screen/
Quick reference of commands
http://aperiodic.net/screen/quick_reference
I use tmux to achieve something like that. I have the following in my ~/.tmux.conf file:
bind s splitw -v -p 25 -c '#{pane_current_path}' '/bin/bash'
bind q kill-pane
On pressing Ctrl-b + s (prefix + s), a new pane containing a bash shell opens up at the bottom. I can run shell commands from there: find, grep, make, etc. When I'm done, I press Ctrl-b + q to close the shell.
To enable tmux on every bash session, add the following to your ~/.bashrc:
[[ -z "$TMUX" ]] && exec tmux
Maybe map a key to shell out to the compiler and run the program if compilation is successful:
:map F8 :!cc % && ./a.out
Or maybe just
:sh
make run
Ctrl-D
Another option is to suspend vi, using Ctrl-Z and do your stuff in the shell, then type fg to bring vim back to the foreground. Note that this is actually a feature of your shell, rather than vim but it produces the effect you seek.
Note this idea originates from the book "Efficient Linux at the Command Line" by Daniel Barrett. I forget the page number.

How can I make GNU screen run a command or script every time I create a new window?

I'm trying to write a script that makes GNU screen call virtualenvwrapper's workon command to jump to an existing virtualenv based on the screen session name.
I've managed to make it work when the session starts, but I want to be able to run commands everytime a window is created, and I can't find hooks or anything similar to it.
Does screen allow a way to run commands everytime a window is created within a session?
Try putting the logic into your ~/.profile (if using the deflogin on setting) or ~/.mkshrc (or similar, depending on your shell) file. Something like this:
if test -n "$STY"; then
# we are inside GNU screen
screenpid=${STY%%.*}
screenname=${STY#*.}
# do your magic
fi
In this scenario, $screenname will contain either the name passed to the -S option of GNU screen, or something like ttyp0.shorthostname or pts-9.shorthostname (depending on the OS) if -S was not used.

Compact cygwin terminal

I'm looking for a way to make the cygwin terminal more compact, or an alternate terminal that is more compact. Currently, every command I enter has a header line above it with username and pwd, and there is a blank line trailing every command. For instance:
username ~
$ cd tmp
username ~/tmp
$
3 lines for every 1 line of command. I frequently work on a small screen, which makes all this wasted space quite irritating. Is there a setting somewhere I can alter to prevent all this wasted space? Or, perhaps another terminal?
Thanks in advance.
That's the default shell prompt set by Cygwin.
To use a smaller prompt in your current terminal:
PS1='$ '
To make the change permanent, put that command in your ~/.bashrc file.
You can set the prompt to just about anything you like, as explained by the bash manual (there are several variables that control different prompts; $PS1 is the main one).
It's important to remember than in Cygwin (as in Linux and Unix), the terminal program is a separate program from the shell that runs in it. The prompt is controlled by the shell; bash is the default. The graphical display is controlled by the terminal emulator, which could be rxvt, mintty, xterm, or even the Windows terminal that normally runs a DOS-like shell.
What you're seeing there is the prompt, as stored in the environment variable PS1
echo $PS1
will show you how it's created. By the way, that prompt is managed by the bash shell, not by the terminal.
export PS1=$
will give you just a $ prompt
export PS1="$ "
will leave some room behind the prompt. There are many more possibilities, here is a nice tutorial.
bash reads its settings from a file called ~/.bashrc aka a file called .bashrc in your home directory. Note that due to the initial dot in the name ls won't show the file by default, ls -a or ls -la will.
I would Recommend we go with modern terminals using Cygwin-X as shown in the below interactive menu
I love Xfce Terminal which allows creating tabs and new windows with font options and color options

Resources