(got one possible solution on stack overflow : https://stackoverflow.com/a/51769020/9497573)
edited to be more accurate
I try to make the title of my windows in screen automatically equal to the path of the working directory PLUS the process running if there is one (e.g: npm start or vim file.js)
for that purpose I added these lines in my .zshrc
:
precmd () {
local action = action_to_define
if [[ $TERM == screen* ]]; then
printf -Pn '\ek%~ $action\e\\'
fi
}
this send (somehow) the path as title to screen (see Stéphane Chazelas answer)
and the variable action
would print the running program if it exist
I tried local action= $(history | tail -1 | sed 's#[0-9 ]*##') | grep vim
because this select the prompt of the last command in the history (just like history !!
would do if the option !!
was recognized, which is not for some reason...)
and local action= $(ps -lr | tail -1 | sed 's#^.*:...##')
because this select the command of the running process
but it doesn't works, as if the process was not captured neither by history
or ps
... maybe precmd
run before the action is launched, so I tried other functions like preexec
or zshaddhistory
without any luck...
how can I capture the running process to put it in the title ?
first posted question
I found this: GNU Screen: new window name change which gives a way of doing something close by modifying the .zshrc file, and source the explaination with this section of the screen manual: https://www.gnu.org/software/screen/manual/screen.html#Dynamic-Titles
however it does only show the folder, not the file, and I do not understand a single word of what is exlained, so I can't adapt to my need : which is not to print just the directory, but also the working file
my understanding of the script for the .zshrc file so far:
precmd () {
local tmp='%~'
local HPWD=${(%)tmp}
if [[ $TERM == screen* ]]; then
printf '\ek%s\e\\' $HPWD
fi
}
-precmd ()
is a function which is executed just before each prompt (could also be chpwd ()
) (src: http://tldp.org/HOWTO/Xterm-Title-4.html)
-local
means the variable has a local scope (src: https://stackoverflow.com/questions/13667284/how-do-i-keep-functions-variables-local-to-my-zshrc)
-tmp='%~'
tmp is a variable equal to the string '%~' which refers to the $HOME directory (but why decomposing the next variable with this one ??)
-HPWD=${(%)tmp}
HPWD is a variable, it has something to do with pwd (print working directory), and is equal to something with the previous variable inside, but I don't know what is the meaning of a dollar sign before something into brackets
-if [[ $TERM == screen* ]]; then printf
if the software screen is running then print
-'\ek%s\e\\'
no idea...
(and how come that screen is listening to this script ?)