How to set custom prompt when opening a new xterm? - linux

I want to open a new xterm with a custom prompt string.
Since prompt is set using the prompt variable, I thought I could just run:
xterm -e "set prompt = $prompt_string_of_my_choosing" #The organization I work for uses tcsh
The problem is, I want the xterm to stay interactive.
I tried tcsh -c as a command for -e, I tried sourcing another script that would set the prompt to the way I want it . The results I'm getting are either an interactive shell with the default prompt, or an xterm that just closes (or stays open with -hold but not interactive).
I was however able to bypass the problem by adding the following to ~/.cshrc:
if ($?calling_prompt) then
set prompt = "$calling_prompt"
endif
And of course I preset the $calling_prompt variable in advance.
This works, but requires me to edit ~/.cshrc, so it's not a global solution.
Any ideas?

Maybe you can set variable being a flag for your new term and than use this approach:
http://www.owsiak.org/?p=2582
This way, you can alway set whatever you like in prompt - e.g. based on type of term you have started.

Related

I want to run some commands in the same shell script on different tabs/terminal [duplicate]

This question already has answers here:
How to open a new tab in GNOME Terminal from command line? [closed]
(10 answers)
Closed 3 years ago.
I actually want to run some commands of the same script on different tabs or terminal because these commands are activating servers and listening to different ports. So they have to be always active. As well, I want to have a reference to the tab or the terminal so that I can later shut them down at the end of the script. Help please.
I tried a simple script test to see if I could find a way for opening other tabs :
tab=" --tab"
options=()
cmds[1]="echo Banana"
cmds[2]="echo Cat"
for i in 1 2; do
options+=($tab -e "bash -c \"${cmds[i]} ; bash\"" )
done
gnome-terminal "${options[#]}"
exit 0
but I get this as a result :
./test.sh
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
Is there any way to open tabs ? and get a reference to each one so that I can shut down commands related to servers once the treatment is done ?
You could use a programm called tmux. It's a terminal-multiplexer, like screen. With that you can open different sessions and windows (these are like tabs) in the same terminal window. They can be referenced by name or id via script.
It's very probable, that the package manager of your Linux distribution has tmux.
P.S: I have to post an answer instead of a comment as I don't have enough reputation for commenting.
This is what I found for man gnome-terminal ; I think that could help , thanks
gnome-terminal(1) General Commands Manual gnome-terminal(1)
NAME
gnome-terminal — is a terminal emulation application.
SYNOPSIS
gnome-terminal [-e, --command=STRING] [-x, --execute ] [--window-
with-profile=PROFILENAME] [--tab-with-profile=PROFILENAME] [--window-
with-profile-internal-id=PROFILEID] [--tab-with-profile-internal-
id=PROFILEID] [--role=ROLE] [--show-menubar] [--hide-menubar]
[--geometry=GEOMETRY] [--working-directory=DIRNAME] [-?, --help]
DESCRIPTION
GNOME Terminal is a terminal emulation application that you can use to
perform the following actions:
Access a UNIX shell in the GNOME environment.
A shell is a program that interprets and executes the commands that you
type at a command line prompt. When you start GNOME Terminal, the
application starts the default shell that is specified in your system
account. You can switch to a different shell at any time.
OPTIONS
-e, --command=STRING
Execute the argument to this option inside the terminal.
-x, --execute
Execute the remainder of the command line inside the termi‐
nal.
--window-with-profile=PROFILENAME
Open a new window containing a tab with the given profile.
More than one of these options can be provided.
--tab-with-profile=PROFILENAME
Open a tab in the window with the given profile. More than
one of these options can be provided, to open several tabs .
--window-with-profile-internal-id=PROFILEID
Open a new window containing a tab with the given profile ID.
Used internally to save sessions.

more, less are behaving like cat

While installing a new RHEL on KVM host I am displaying a text file using more command. Problem is more is behaving like cat in virt-manager i.e. not displaying page-by-page and directly scrolling to the end of the page. I tried using less instead of more but it is displaying the same behavior.
Can anyone suggest what could be the reason for this?
Edit Based on comments I tried changing TERM to xterm during first boot. But that didn't have any effect. I tried this command export TERM=xterm
I got the env variables printed. I am pasting it hoping this might be a clue.
Edit I couldn't get it working. I noticed that before the script is executed, some other script is running and this might be setting some environment variable because of which more isn't working. I am now calling more command before this script start executing. And now it is working.
I suspect your environment is not correct. Specifically your $TERM environment variable may not be set. See here for the more manual entry.
The more command respects the following environment variables, if
they exist:
MORE This variable may be set with favored options to more.
SHELL Current shell in use (normally set by the shell at login
time).
TERM The terminal type used by more to get the terminal
characteristics necessary to manipulate the screen.
VISUAL The editor the user prefers. Invoked when command key v is
pressed.
EDITOR The editor of choice when VISUAL is not specified.
After help from a senior dev, I found out what was happening behind the scenes.
Suppose A.sh is executing more command like below:
more pathtofile
A.sh is being called by some other script like below:
pathtoA.sh | tee
Because of this tee command, more command in child script was not behaving as it should have been.

How to start shell (fish) with variable preset

I want to use fish with Cool Retro Terminal but the prompt I use normally has unicode characters, which the fonts in retro terminal don't support. I have my fish_prompt function check if $simple_prompt is 1 or not and I know that it works, but I need a way to set the variable, only for that terminal instance, with the startup command option in retro terminal.
The closest I have so far is fish -c "set -gx simple_prompt 1; fish", but that exports the variable to all the shell instances on the system and makes the prompts simple for all of those too. fish -c "set -g simple_prompt 1; fish" doesn't work.
It is expected that set -g simple_prompt 1 isn't visible to the next fish instance you launch. Global variables are only visible to the current shell instance. If the terminal you are using sets some environment vars that would allow you to determine that fish is running within that specific terminal I would go use that info. For example, iTerm2 on macOS exports ITERM_PROFILE and four other env vars unique to it. If Cool Retro Terminal doesn't do that perhaps there is some other way detect that terminal type. It may be as simple as checking the TERM variable.
If the aforementioned solutions aren't an option then the only solution is an environment variable like in your first solution. Because you say it exports that variable to all fish instances I suspect you did not accurately describe what you are doing. Because if you set that command as the command that Cool Retro Terminal should use to launch your shell it would only affect fish instances running in that terminal. Which is what you want.

How to identify which REPL is running in the terminal?

I am trying to have my terminal title change depending on what that specific window is doing. So far I have the following which will update based on directory and server.
function settitle() {
if [[ -z "$ORIG" ]]; then
ORIG=$PS1
fi
TITLE="\[\e]2;$*:$(dirs -0)\a\]" #dirs -0 is like pwd but with ~/ instead of /home/user/
PS1=${ORIG}${TITLE}
}
PROMPT_COMMAND="settitle local" # local is the server name in this case
Now, sometimes I'm in the PHP (php -a) or MySQL (mysql -u user -ppass) REPL, and I'd like the title to reflect that instead of just being whatever directory I launched the REPL from.
The best I can figure is getting the last command somehow, then figuring out what the first word is, and running an if check in settitle(). I've tried everything from here and here among other places, and while I can usually get part of it to work in the command line, non of it works in settitle(). For example.
local:~$ echo 'foobar'
foobar
local:~$ !:0
echo
# I add echo !:0 to settitle()
local:~$ source .bashrc
!:0
local:~$
A note: It should be "source", or at least "echo" from before. !:0 does not recognize itself as a command so it will repeat the last real command over and over. The "!:0" being echoed is a literal string, not the results of the command. Additionally, saving to a var does not work, and just putting the command without trying to echo/save the result gives !:0: command not found.
I don't want to make this an XY problem, so if I am barking up the wrong tree here at any step of the process, please let me know. The goal is to be able to change the title of my terminal window if I enter an REPL. How can I identify when a command will enter me into one?
Note that PROMPT_COMMAND and similar shell features are not relevant when you're in a different REPL; from the point of view of the shell, the entire REPL session is one single command. The shell prompt doesn't show up again until you exit that REPL, and that's the point at which PROMPT_COMMAND and friends are activated.
One thing you can do is alias the command you use to start the REPL so that it sets the title of the window first:
alias phpa='setttitle PHP; php -a'
alias mysqli=`settitle MySQL; mysql -u "$USER"'
or something like that.
The sequence goes like this:
PROMPT_COMMAND runs.
The shell prints its prompt.
You type the command to start a REPL
You are in the REPL. The prompt you see is printed by the REPL, not the shell, which is not involved at this point. The shell is just hanging out waiting for you to exit the REPL; it's not printing any prompts, so it's not ever running PROMPT_COMMAND.
You type commands in the REPL. No matter how many you run, it's part of a single session that the shell sees as a single command.
You exit the REPL.
PROMPT_COMMAND runs.
The shell prints its prompt.

how to configure default TERM in screen session?

When invoke vi in a session inside SCREEN, I always encounter error:
bash-3.2$ vi perf332.db2
ex: 0602-108 screen is not a recognized terminal type.
[Press return to continue]
[Using open mode]
After I explicitly export TERM=xterm or something, it works fine.
My SCREEN is running on a Linux server, and I SSH from there to a AIX server.
The default TERM variable for screen is TERM=screen. This is what its supposed to be.
You might consider putting something like this in your .bashrc to change the TERM variable to xterm when its screen.
if [[ $TERM = screen ]]; then
export TERM=xterm
fi
However I do not recommend doing this as this might confuse other application.
It seems vi does not know how to send commands to screen when the TERM variable is set to screen. The other way of getting around this would be to install vim which does understand how to communicate with screen.
Try putting shell -$SHELL into your ~/.screenrc file

Resources