In sqsh, can an alias do a reconnet and change the prompt? I want to change color for production - sqsh

Using sqsh, in my .sqshrc, I can have an alias to connect to a production server, and an alias to change my colors:
\alias prod='\reconnect -S MY_PROD_DS ...'
\alias pc='\set prompt="{0;47;34} [$histnum] ${DSQUERY}.${database}.${lineno}> "'
How can I have one alias that does both?

I don't think it is possible to have 2 or more commands in one alias. The best way to specify a server dependent prompt is to set it in the session file that is being evaluated just before setting up a new connection to the server. In your .sqshrc file you can specify the session file and prompt definition, for example:
\set session='$HOME/.sqsh_session'
\set text_color='{0}'
\set prompt='$prompt_color[$histnum]$DSQUERY.$username.$database.$lineno>$text_color '
In this session file you then can do something like:
\if [ "$DSQUERY" = "MY_PROD_DS" ]
\set prompt_color='{0;31;47}'
\else
\set prompt_color='{0;34;47}'
\fi
When you connect or reconnect to the MY_PROD_DS server you get a red on white color prompt, otherwise a blue on white prompt.

Related

make zsh prompt update each time a command is executed

TLDR;
I need a way for .zshrc to automatically be sourced each time a command is executed. PROMPT needs to be updated each time a command is executed in order to show relevant information in the prompt.
Reason
I use Watson cli for tracking time. On my previous bash setup, I prepended my prompt ($PS1) with a symbol that indicates whether the timer is running or not (red/green). I have mimicked this functionality with Oh My Zsh, as follows (in the theme file):
WATSON_DIR="$HOME/Library/Application Support/watson"
watson_status() {
local txtred="${fg_bold[red]}"
local txtgrn="${fg_bold[green]}"
local txtrst="${reset_color}"
# Started
local status_color="$txtgrn"
# Stopped
if [[ $(cat "$WATSON_DIR/state") == '{}' ]]; then
status_color="$txtred"
fi
echo -e "$status_color""◉""$txtrst"
}
PROMPT="╭── %{$(watson_status) $fg_bold[green]%}%~%{$reset_color%}$(git_prompt_info) ⌚ %{$FG[130]%}%*%{$reset_color%}
╰─➤ $ "
Current issue
The icon will indicate the color of the state at the time that .zshrc was executed. For example, if the timer is running and the icon is properly indicating green, stopping the timer will not cause the icon to turn red. In order to see the icon change color, I have to source .zshrc.
This indicates that the function watson_status() needs to be run each time a command is executed, to give the latest status at the time of the command
I recently ported some prompt code from bash to zsh - on OSX Big Sur - and these were the two big "things to know" for me:
add setopt PROMPT_SUBST to your .zshrc. This "allows for functions in the prompt"
use single quotes when defining your PS1 / PROMPT. If you use double quotes, then the whole string will be evaluated once when the terminal starts, and then that evaluated value is "re-executed" every time the command changes. But you want the functions to be re-evaluated, not the output of the function at the time when the terminal is created.
Easiest example I used to confirm I had it working:
# print_epoch() { date '+%s' }
# this is what you want
# export PS1='$(print_epoch) > '
# this is not what you want
# export PS1="$(print_epoch) > "
Also worth noting PS1 and PROMPT are interchangeable
Extra:
While we are sharing here is my fairly minimal echo my user, directory, and git branch PROMPT with some colors and a pretty leaf:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
setopt PROMPT_SUBST
autoload -U colors && colors
export PROMPT='%n %~ %F{blue}🌿$(parse_git_branch)%f > '
noting that:
%n prints name
%~ prints directory relative to home
%F{blue} changes text to blue
%f resets color
Documentation: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Visual-effects

ZSH: Hide computer name in terminal

How would I hide the computer name in the prompt while using the terminal?
At the moment it shows both username and computer name like so:
It would save some space by removing anwarchoukah#anwars-mbp, seeing as I actually know who I am:)
Try to add export DEFAULT_USER=$USER to your .zshrc file
On MacOS 10.15 Catalina:
Open the file /private/etc/zshrc in a text editor
Locate the comment: # Default prompt
Modify the line that looks like this: PS1="%n#%m~ %& # "
Save the file. You will be prompted to enter a password to save it.
Load a new Terminal window.
For example, you can:
Remove "%n#%m" to get rid of both the username and computer name
Remove "%n" to get rid of the user name
Remove "%m" to get rid of the machine name
step 1. one your .zshrc file by vim .zshrc
step 2. go to end of your file.
Paste this code:
careful indent again your code
prompt_context() {
if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
fi
}
EDIT - explaining what this does
This will remove the machine#user name from the prompt IF:
- you are not logged in as the default user
- you are not in an ssh client shell
For most people, not needed, but if you regularly ssh to other machines and have multiple terminals open (eg remote sys admin type work) then this is extremely useful so when you look at your terminal you know which machine and user you are logged in as inside that terminal.
If you don't need/manage that type of complexity then use one of the other answers to just modify your export PROMPT/PS1 value.
* WARNING *
If you are using a custom shell or theme, this might not work and although the prompt will no longer show your computer and username it will keep throwing the error:
prompt_context:2: command not found: prompt_segment
For example, you can see with this (very popular) powerlevel9k it does not work. This is because the Powerlevel9k theme uses it's own magic and you simply add commands to your ~/.zshrc file to get the same result, eg:
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context)
More info on that here.
Open up .zshrc, find the line reading export PS1 or maybe export PROMPT.
Remove the variable that is used for the hostname, could be %m or %M.
Zsh on Archlinux wiki
You can use colors and also have a prompt (or some information) on the right side.
I like this approach (on my mac)
put in .zshrc
PS1="%n$ "
The terminal will look like
username$
Just add prompt_context() {} to .zshrc
Unfortunately none of the .zshrc changes worked for me.
Machine : Mac M1, Big Sur 11.4
So this is what worked.
I Navigated to where the ZSH themes were installed, opened my theme, agnoster in TextEdit, and modified the configuration where it chooses what do display, which by default is $username#%m.
Note : %m here is the machine name.
Here is a screenshot of delta. Yellow is what I did, Green is the default setting from github version of agnoster theme.
Voila this worked. Now to me it just displays the machine name, as I intended.
Hope that helps. Many links and SOF posts only made me click that solution.
Set DEFAULT_USER in ~/.zshrc file to your regular username. You can get your exact username value by executing whoami in the terminal.
Something like this:
export DEFAULT_USER=username
If you are using PowerLevel9k theme, there is a variable POWERLEVEL9K_CONTEXT_TEMPLATE that can change the view of your hostname and computer name.
The default option is %n#%m,
%n -> username
%m -> machine name
to hide hostname:
Open the .zshrc file using sudo nano ~/.zshrc
Add the line
POWERLEVEL9K_CONTEXT_TEMPLATE="%n" at the end of .zshrc file
Save the file.
Maybe this will help you [ Open Profile => Shell ]
PS1="~ $: ";clear;
Just add this to your ~/.zshrc file:
export DEFAULT_USER=\`whoami`
Install Oh My Zsh is the easiest solution for me:
https://ohmyz.sh/
One liner install:
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Result:
If you're using Powerlevel10k, then you can run p10k configure and configure the output a bit.
My username and computer-name was gone after having gone through it. It feel less hacky, than the other solutions here.
I don't know why I can't find a simplified solution.
So here is the simplified one.
Just jump into the themes directory,
# ~/.oh-my-zsh/themes
Select the theme, as I have been using
# vim agnoster.zsh-theme
Just remove %m to remove and also you can remove the username too.
Once done,
Just run
#zsh
To reflect the changes, Enjoy :)
Thanks to Wes Bos' amazing video series Command Line Power User , I managed to find the answer.. It's pretty simple. Stuff like that is set in the ZSH theme.
Open up .zshrc, change the theme from ZSH_THEME="agnoster" (which is what I was using) to something else.
Wes Bos has made a lovely theme called Cobalt2 which does exactly what I was looking for :)
I've now changed it to ZSH_THEME="cobalt2"

Reload aliases on change automatically without closing the shell window

I have my aliases stored in ~/.zsh_aliases and sourced in ~/.zshrc:
# Access custom aliases in the shell
[ -e "${HOME}/.zsh_aliases" ] && source "${HOME}/.zsh_aliases"
However, when changing the name of an alias, I have to always close the current shell window and open a new one for the change to become active.
Can Zsh automatically reload aliases on change to make them available without having to close the shell window?
You do not actually need to close and reopen your terminal for that, just running source ~/.zsh_aliases (loads the new and changed aliases) or maybe exec zsh (replaces the current shell with a new one) would work, too.
If you really want to re-source ~/.zsh_aliases whenever it is modified, I would suggest adding the following to your ~/.zshrc:
# File containing aliases;
ALIAS_FILE="${HOME}/.zsh_aliases
reload_aliases () {
# do nothing if there is no $ALIAS_FILE
[[ -e ALIAS_FILE ]] || return 1
# check if $ALIAS_FILE has been modified since last reload
# the modifier `(:A)` resolves any symbolic links
if [[ $LAST_ALIAS_RELOAD < $(stat -c %Y ${ALIAS_FILE}(:A)) ]]; then
# remove all aliases; optional!
# only do this if all of your aliases are defined in $ALIAS_FILE
# also affects aliases defined on the command line
unalias -m '*'
# load aliases
source $ALIAS_FILE
# update date of last reload
LAST_ALIAS_RELOAD=$(date +%s)
fi
}
# make reload_aliases to be run before each prompt
autoload -Uz add-zsh-hook
add-zsh-hook precmd reload_aliases
Note, that any changes will only be available on a new prompt. That means, if you modify ~/.zsh_aliases, you need to press at least Enter once in the all terminals for the changes to take effect.
I use an alias, thusly: -
alias vialias='vi ~/.oh-my-zsh/custom/alias.zsh ; source ~/.oh-my-zsh/custom/alias.zsh'
When I run vialias, I edit my aliases, then when I leave vi, the change(s) take effect.
To simplify the accepted answer add:
source ~/.zsh_aliases
in the ~/.zshrc below the plugins section.
Then add an alias inside the ~/.zsh_aliases like so:
alias f="exec zsh"
To refresh zsh & aliases type f

Set the terminal tab title as prompt name in unix

Lets say, the prompt is as below
run_scripts >
How to set that terminal tab title same as prompt
i.e Terminal tab tile also should be
run_scripts>
So that terminal title should dynamically update when the prompt changes.
Many terminals emulators are able to understand the special escaping : "\033]0;foo\007".
I know its a old post but i saw it today :
Here is the answer:
title `pwd`
if title command does not works in your shell then:
write a shell script with follwing contents (filename = title)
#!/usr/bin/tcsh -f
echo "^[]2;$1^G^[]1;$1^G"
then:
chmod +x title (give this script executable permission)
type:
title `pwd` <enter>
This single line command will change the title of the tab.
Simply run the command from terminal tab which title need to change-
PS1=$PS1"\[\e]0;My_Tab_Name\a\]"
Some Info:
The PS1 is a primary prompt variable which holds the characters displayed at the terminal prompt. You can set it whatever you want. However the above command will make it only work for current terminal session. Once you close the terminal and opena new one, it'll be the default one.
To make it permenant edit the PS1 variable in ~/.bashrc or ~/.zshrc file.
Benefits-
It help us to easily navigate over the tabs.

How can I configure vim so that when I send the process a USR1 signal it saves and quits (as opposed to creating the .swp recovery file)

Here's my situation. I have a bunch of vim processes open. Rather than go through one by one and save/quite (:x!) I'd like to send all of the processes a signal - say USR1 - and instead of having it create a recovery .swp file, I'd like it to save the file and exit normally.
Possible?
It's not quite as simple as just sending every vim process a signal, but you can get pretty close to what you want using vim's client/server features:
:help remote.txt
(You'll need a version of vim that's been compiled with the +clientserver option.)
Here's an example from the vim help:
vim --servername BLA --remote-send '<C-\><C-N>:wqa<CR>'
That sends commands to the vim instance running as remote server "BLA", telling it to save all files and exit.
Since you're running multiple vim instances, you'll probably want to use the --serverlist option to get a list of all registered server instances, which you can then iterate over to send the save-and-exit command to each one.
I've never tried to handle signals in vim. I don't know if it can be done. Here is one possible alternative:
Option: Use just one vim process
Perhaps instead of opening a ton of vim processes, you can open a bunch of tabs in gvim (or vim). That way, when you want to quit you can do :wqa (write and quit all).
It is easier to open tabs in vim if you do the following in your vimrc file:
:map <C-Insert> :tabnew<C-M>
:map <C-Delete> :tabclose<C-M>
:map <C-PgUp> :tabprev<C-M>
:map <C-PgDown> :tabnext<C-M>
The bindings above will allow Ctrl-insert and Ctrl-delete to open and close tabs, and Ctrl-pgup/pgdown will move between tabs much like firefox.
If you wanted those bindings to work in insert mode as well, you could do something like this in your vimrc file
:imap <C-Insert> <C-o>:tabnew<C-M>
:imap <C-Delete> <C-o>:tabclose<C-M>
:imap <C-PgUp> <C-o>:tabprev<C-M>
:imap <C-PgDown> <C-o>:tabnext<C-M>
I just wanted to add that you can get make to run in the background asynchronously by using the following
nnoremap <leader>m : call setqflist([]) \| cclose \| silent execute ":! (make &>~/.vim/cpperr \|\| vimx --servername DEFAULT --remote-send '\ <C-\\>\<C-N\>:cgetfile ~/.vim/cpperr\<CR\>')&" \| redraw!<CR>
For this to work you must open your files using
vim --servername DEFAULT --remote-silent FILE..
I actually have that set as an alias to vim in my bashrc.

Resources