Can I get terminal title? (or otherwise restore old one) - terminal

Setting terminal title is easy with echo -e "\e]0;some title\007". Works with pretty much every terminal program.
What I want is to set terminal title when some program starts - and restore old one when it finishes. Is this possible?

On xterm, the terminal control sequences 22 and 23 work fine, as in
#!/bin/sh
/bin/echo -ne '\033[22;0t' # Save title on stack
/bin/echo -ne "\033]0;$(date)\007"
sleep 1
/bin/echo -ne '\033[23;0t' # Restore title from stack
It looks like this isn't supported in the Mac OS X Terminal.App though.

There are some terminal programs that supporting it (xterm has compile time options for that, as mentioned by RWS), but most terminal programs simply lack such feature, including in particular Terminal.app.

Yes, that is possible indeed. See a xterm reference manual (like this for example) and wander your way through it. xterm even has a build in stack for this, so you don't have to store the title manually.

My solution was to set the window title during my script, then unset the window title when I completed. Unsetting the title reverted to the original value. Specifically, I did the following:
# Set the terminal title
printf "\e]2;%s\a" "running my script"
# Do whatever processing is required.
...
# Restore terminal title
printf "\e]2;\a"

Related

Why does Fastscripts work with this but Platypus doesn't

Mac running Catalina. This code
#!/bin/bash
pbpaste|pbcopy
pbpaste>/tmp/tmp$$
open -W -a macvim /tmp/tmp$$
while [ `ps -A|grep MacVim|wc -l` -gt 1 ]
do sleep 1
done
cat /tmp/tmp$$|pbcopy
rm -f /tmp/tmp$$
is intended to plain-text the paste buffer then call up a terminal running macvim so I can use vi with less faff then put the result back in the clipboard. Its a way to speed-up editing when using various tools other tools and I just want to edit a section with vi.
I works well when called from Fastscripts or just plain execution but it won't work when I use Platypus to build a menubar app so its one fast click to use it - or rather, it works sometimes. Sometimes it hangs because it cannot connect input to the window in which macvim runs. I have to kill it from Activity Monitor to regain any control of input to other windows such as terminal. I've tried connecting stdin in the "open" command but still only works sometimes. And it shouldn't be standin anyway.
How is Fastscripts launching it and how can I do the same inside the script ?
I'd like very much to be able to launch it with a single click from the menubar but I don't know how to. I can build the platypus app if I know what to put in the shell script.
Thanks
andy

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

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

Why am I seeing only 8 colors in terminal (xfce-terminal)?

I'm running Xubuntu 13.04 and I want to use Vim as my default editor for everything. I have downloaded many vim color schemas and tried them out, but all of them don't look like the official screenshot.
For example, vim's own color schema - desert should look like this:
But in my vim, many colors won't display, for example the background.
So this means a fighting with xfce's Terminal and I can't force it to use 256 colors.
the command tput colors gives me
8.
At the same time the code for ((x=0; x<=255; x++));do echo -e "${x}:\033[48;5;${x}mcolor\033[000m";done shows me nice colors. it seems i missed something. If I run
**$ echo $TERM**
I get xterm. It should be 'xterm-256color'
When I try
set term=xterm-256color
and
export TERM=xterm-256color
Then: echo $TERM
I get the message
xterm-256color.
But after signout/signup, I'm still not getting the right colors in Vim. And I see the Xterm is changed to xterm again.
I added:
if $TERM == "xterm-256color" set t_Co=256 endif
and
t_Co=256
to my .vimrc file and it didn't seem to help. Then I customized the xterm entries; added this to ~/.Xdefaults:
*customization: -color
XTerm*termName: xterm-256color
Add this to ~/.xsession to apply to new terminals:
if [ -f $HOME/.Xdefaults ]; then
xrdb -merge $HOME/.Xdefaults
fi
When I changed in preferences of terminal, emulate terminal environment, the 'xterm' to 'xterm-256color'
I get the message:
'*** VTE ***: Failed to load terminal capabilities from '/etc/termcap'
When I check /usr/share/vte/termcap/xterm, the file xterm-256color is missing. Same in folder xterm0.0. I tried to find this file on internet to download and put in the folder, but I couldn't find it.
This is driving me crazy the whole day... Have anyone suggestions?
Quick (Temporary) Way
Enter this whenever you open a new terminal:
export TERM=xterm-256color
Works for as long as the window is open.
Works-but-dirty Way
Append the line above to ~/.bashrc.
The problem with that, though, is that editing $TERM in .bashrc is a bad idea because doing that automatically makes any terminal using bash try to use it regardless of whether it actually supports 256 colors or not (like when SSH-ing or accessing the terminals with Ctrl+Alt+F1 to F6).
What I did, though is that since xfce4-terminal sets the $COLORTERM value to xfce4-terminal, I, instead appended the following to .bashrc:
if [ "$COLORTERM" == "xfce4-terminal" ] ; then
export TERM=xterm-256color
fi
That way, the relevant $TERM edit only happens if you're using xfce4-terminal, which just sets it to xterm anyway (and changing the emulation environment results in that "VTE" message).
References:
https://bbs.archlinux.org/viewtopic.php?id=175581
http://vim.wikia.com/wiki/256_colors_in_vim#Comments
http://promberger.info/linux/2008/04/05/getting-256-color-support-working-for-mutt-in-xfce4-terminal/
http://docs.xfce.org/apps/terminal/getting-started

How open default terminal instead of using `xterm` ? is it safe to use ${TERM}?

I want to run steam on linux, and I don't want to install xterm, there are two moments when does it want xterm
echo -e "$*" >$tmpfile
xterm -T "$title" -e "cat $tmpfile; echo -n 'Press enter to continue: '; read input"
rm -f $tmpfile
and
# Fall back to sudo in a terminal
xterm -e sudo -p "$prompt
[sudo] password for user: " $*
is it safe for posix to use ${TERM} instead of xterm here?
Assuming the script will be running in an X session; no, there is no reason to believe that TERM will be set under those circumstances (and if it is, it would be a coincidence for it to point to a binary of the same name -- the contents of this variable should be a key for Termcap or Terminfo; a lot of the time, on modern systems, it might be something like vt220 if it's not some variant of xterm, or emacs or dumb).
On the other hand, if your environment is X, you are basically guaranteed to have xterm.
Debian defines x-terminal-program (IIRC) to let the user or system administrator pick a different default terminal emulator.
I don't think POSIX 1003 has any concept of the problem you are attempting to solve (and we are guessing, too; perhaps you could add more details to your question?)
A better platform to require users to have available might be Freedesktop.org but I have no idea whether e.g. Mac OSX adheres to any useful subset of that. (They might implement all if it; I simply don't know.)
On the other hand, the simple, crude fix would be to require the script to be run from a terminal in the first place; then, you can simply use cat to display text files (your EULA, I would guess?) and echo (or cat again) and read to interact with the user. That is, don't attempt to open a new terminal window for these things. Maybe do all interaction near the beginning of the script to make it easy to bail out early.
If you want graphical interaction, you need to limit your target platform, or write complex wrapping for each different environment.

MRXVT bash script to change tab title

I am not exactly a pro at BASH Scripting, but I can get by with the basics. I use MRXVT with Cygwin on my windows box (MRXVT is an RXVT Terminal with tabs. RXVT is a Standard Cygwin terminal, but with enhanced features). I found a command to change the tab names for MRXVT:
echo -ne "\e]62;New tab title\a"
It works like a charm. I'm not, however, interested in A)Changing it manually or B)Changing it to a static String
I use a lot of SSH. I'd like the tab title to be username#host[current directory] at any given time.
I'm not sure if I'm supposed to use a trap for this. Frankly, I don't understand how traps work. I guess I could create an ssh alias that pulls the first argument of the ssh command and sets the tab title to that...This would be sufficient, but not preferred.
Do you guys have any ideas?
Found an answer. added
trap 'echo -ne "\e]62;mrxvt: $USER#${HOSTNAME}\007"' DEBUG
to the very end of my .bashrc. I decided I didn't like having the path in my tabs (or titlebar for that matter). Also, I had to add "mrxvt: " to the beginning of the string because of an AHK script that I wrote; it wouldn't work properly otherwise. The full answer to this is:
trap 'echo -ne "\e]62;$USER#${HOSTNAME}[${pwd}]\007"' DEBUG
Again, just add that line to the end of your .bashrc, and you're good to go.
Hope this helps somebody else :)

Resources