Sending Ctrl-L via dbus to terminal emulator - linux

One can send text via dbus to the terminal emulator konsole as followed:
qdbus org.kde.konsole /Sessions/1 sendText "hello"
However I want to remotely clear the screen of the specified terminal window.
So I tried:
qdbus org.kde.konsole /Sessions/1 runCommand "clear"
Does partly what I want. Only problem: The screen doesn't get cleared when there is a process running.
In the terminal emulator, in this case the key combination "Ctrl + L" would do the job.
So I'm trying to send a string with escape characters for this shortcut.
Is this going to work? This, however doesn't do;
qdbus org.kde.konsole /Sessions/1 sendText "\033[2J"
(runCommand neither)

This is working for me:
qdbus org.kde.konsole /Sessions/1 sendText $'\014'
First, to produce a character from its octal code, the syntax "\033" would work in C but not in Bash.
Second, while "ESC [ 2 J" is a VT100 code to Erase Screen, it work for me only if I echo $'\033[2J', and that will not work if a command is running.
Third, Ctrl-L will work if a program is expecting input from a terminal (like irb or python do), but it will not work for a while sleep 1; do echo Still running; done loop.

Related

What does this PROMPT_COMMAND do?

I have this in /etc/bash.bashrc on my Linux system:
PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'printf "\033]0;%s#%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'
From man bash I understand that it sets a command to be executed prior to issuing each prompt, but I'm wondering what exactly it's doing.
Basically, it updates the title of the terminal after every command you issue to reflect the current values of the envariables, using XTerm escape sequences.
Some of the escape sequences recognized by XTerm-compatible terminal emulators:
ESC]0;stringBEL — Set icon name and window title to string
ESC]1;stringBEL — Set icon name to string
ESC]2;stringBEL — Set window title to string
where ESC is the escape character (\033), and BEL is the bell character (\007).
Sets your prompt to be whatever is being executed now in addition to a printf that will show your username # your hostname with your present working directory. You'll have to look up the \033]0; terminal code yourself.

Make color escape codes work in cgdb

I have the following function residing in ~/.gdbinit:
define foo
echo \033[34m
echo testing...\n
echo \033[0m
end
When running foo in gdb it prints testing... in blue, however, when running it in cgdb the result is:
[34mtesting...
[0m
How can I enable color escape codes in cgdb?
Unfortunately, the gdb "window" in cgdb is not a full-fledged terminal... it can handle basic text I/O but it will not render any terminal escape sequences such as colors, cursor movement, etc.

Can't close a scpi(telnet) session with echo "^]" when I use it in a script

The use of echo-e "\ 029" does not work either.
But if use strg + alt gr + ] directly in a terminal session -> it works.
I have to ask my question more concretely:
I connect an RF generator (AGILENT) via Telnet/SCPI.
If I do this manual on terminal and press at the end of the session CTRL + ALT GR +] for '^]' then close the scpi session properly and I can type quit to close the telnet session properly.
There is no error message on the display of the RF generator. So it should be.
If I do this via script the SCPI session seems not to recognize the break signal condition '^]' and will be forced to close after the end of the script (telnet and scpi). -> Message: "Disconnected by foreign host". Unfortunately, I get error messages on the display of the RF generator -> "invalid header", etc.
After successful connection appears:
Connected to 192,168.10.66
Escape Character is ‘^]’ -> This is the point at issue. Manual entry in the terminal works correctly, script does not work.
My script looks something like this:
function getIDNMessage()
{
(
echo open $1 $2
sleep 1
echo "*IDN?"
sleep 1
echo –e "\029" # or echo “^]” does not work well
sleep 1
echo "quit\r"
sleep 1
) | telnet > scpi_telnet.log 2>&1
}
getIDNMessage 192.168.10.66 7777
On Linux it's actually:
CTRL + ] then ENTER
Finally type in the quit command.
^]
telnet> quit
Connection closed.
[fred#localhost ~]$
To quit telnet on redhat:
type "CTRL+5" and then type "quit"
To Close Session Use below command
Ctrl + ]
telnet> quit
it works perfect in REHL and CentOS.
The ^] means ctrl + right bracket. As strange as that is, it works. You'll be taken to the telnet prompt, where you can type quit.
On international keyboards the ] character is often not a single key, and needs to be replaced with some other key. The correct key is typically the key to the right of P or the next key after that.
Here's a list based on comments below:
Finnish, Swedish, Norwegian, Danish: ctrl + å
French: ctrl + 6
German: ctrl + ü
Swiss: ctrl + ¨
Hungarian: ctrl + 5
Portuguese: ctrl + ´
Dutch, Belgian: ctrl + $
Canadian French: ctrl + ç
It must be so. Because ^] printed in the terminal on the server means for the client side nothing. The client must catch this symbol before it will be transmitted to server and of course you can't just write it to terminal in te program running on the server.
So you need to interrupt session in other way. There are many methods.
If you are inside the running program, you can simple terminate it (exit in shell or sys.exit() in python or exit() in many other languages).
If you can't control program flow you can close terminal by killing the process that is owner of the terminal. You need to find the process and then use kill ... (PID of the process instead of ...).
If you want to close the client from client side, you need to do the same (kill ...) but on the client side.
On my danish keyboard it was not Ctrl + å - but instead the key to the right side of å (which has a hat, a tilde and a umlaut)
On MacOS with Turkish keyboard try:
Ctrl + Option + ü
Then,
> quit
Ctrl + ] This will show as ^] and then
telnet> q q is for quit
When opening a connection you can specify the escape character. e.g.
[root#localhost ~]# telnet -e % localhost
Telnet escape character is '%'.
Trying 127.0.0.1...
Connected to localhost.
Escape character is '%'.
>%
telnet> quit
Connection closed.
[root#localhost ~]#
In here, I just used the % symbol to close the session, for that I had to tell telnet to use it as an escape character. I find this useful since the character ^] could be different on different keyboard layouts.
IF you are here in 2022 and using Mac OS or AWS Linux and using Apple Magic keyboard
^]
telnet> quit
Connection closed.
Where ^] is "Control + ]"

Commandline overwrites itself when the commands get too long

I'm using PuTTY to log into a Debian server. I have this odd problem that when a command I'm typing gets too long, it doesn't wraparound and start a new line. Instead, it starts at the beginning of the same line and starts to overwrite the prompt and then the beginning of the command.
The command will run just fine, but it is really annoying, I'm assuming there is some setting that would fix this for me?
I've just solved this myself.
It was just some color escapes in the PS1 command prompt:
LTGREEN="\033[40;1;32m"
LTBLUE="\033[40;1;34m"
CLEAR="\033[0m"
LIGHT_GRAY="\033[40;1;33m"
export PS1="$LTGREEN\u$LTBLUE#\h:$LIGHT_GRAY\w$CLEAR ❯ "
The issue is that the color literals are not enclosed in brackets. Placing escaped brackets around them fixes the issue entirely:
LTGREEN="\[\033[40;1;32m\]"
LTBLUE="\[\033[40;1;34m\]"
CLEAR="\[\033[0m\]"
LIGHT_GRAY="\[\033[40;1;33m\]"
export PS1="$LTGREEN\u$LTBLUE#\h:$LIGHT_GRAY\w$CLEAR ❯ "
Hope this helps.
I've seen this behavior when you resize the Putty window while another command is running since bash doesn't get the signal telling it the window has resized.
If this is the problem adding:
shopt -s checkwinsize
should make it go away.
Try:
eval $(resize)
ََََََََََ
Try pressing Ctrl + L. (Or maybe Alt + L - something + L anyway.) If I remember rightly this reruns the code to figure out your terminal size and adjusts accordingly.
Ctrl-l
Ctrl-l works. I guess the putty needed to
This solution worked for me:
Add the following snippet to your .bashrc:
if [ $(tty) == '/dev/ttyS0' ]; then
trap resize DEBUG
fi
This one helps me to resolve this after searching lot of places.
PS1='\[\e[0;31m\]\t\[\e[m\]-\[\e[0;32m\]\u\[\e[m\]#\[\e[0;36m\]\h\[\e[m\]:\[\e[0;23m\]\w\[\e[m\e[0;32m\]\$\[\e[m\]'
Enclosing enclosing each of the colour escape sequences in escaped square brackets.
Nothing to do with putty its a "bash"ism.
Its quite useful once you are used to it, but, if it bothers you you can switch to "ksh" which does something different with long command lines (not sure what on Debian!)

current directory doesn't appear in title bar when running under screen

My xterm $prompt variable in my .tcshrc is:
set prompt="%{\033]0;%m:%~\007%}%{^[[;37;1m%}%B%{^[[;34;1m%}%m%{^[[;34;1m%}:%b%c%# "
The highlighted part above (%{\033]0;%m:%~\007%}) puts the hostname (%m) and the current directory (%~) in the title bar. (At least I think that that's what puts it in the title bar; it's been a while since I fiddled with this prompt).
When I run screen, however, the current directory stops getting updated when I change directories.
My questions:
How can I make this prompt work in screen?
Is there a better way to display the current directory in the title bar?
I am running linux with xterm and tcsh.
I think there is no direct way, because of the way screen works. However screen can display its own status bar, that you can define in .screenrc. Here's mine for instance :
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%=%{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
Firstly, to make it work you must check where exactly is the line with set prompt=blah-blah in your .tcshrc. For example, the code below that perfectly works in plain xterm would not work under screen in xterm:
switch ($TERM)
case "xterm*":
set prompt="%{\033]0;${HOME:t}#%m:%l:%c08\007%}%{\033[36m%}%l:%c02%#%{\033[0m%} "
# update xterm title to display current cmd in it
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${tty} \!#:q\007"'
...
because screen by default sets $TERM variable to screen and not xterm! So you must add:
case "screen":
# lame, but prevents an error in screen after 'su - root'
if (! $?WINDOW) setenv WINDOW 1
set prompt="%{\033]0;${HOME:t}#%m:${WINDOW}:%c08\007%}%{\033[36m%}%c02%#%{\033[0m%} "
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${WINDOW} \!#:q\007"'
...
Secondly, make sure yo have this line in ~/.screenrc:
termcapinfo xterm* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'

Resources