0

(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 ?)

1

That's ${(flag)tmp} where (flag) are parameter expansion flags (see info zsh 'flags, parameter expansion'. The % parameter expansion flag turns on prompt expansion upon the expansion of the $tmp variable.

%~ in prompt expansion expands to the current working directories but with things like /home/you replaced with ~.

Note that you don't need the temporary variable, you could do ${(%):-%~} or even simpler:

print -Pn '\ek%~\e\\'

Where the -P option of print also turns on prompt expansion (see info zsh print).

Note that instead of using precmd, you could also add that string directly in your prompt:

PROMPT=$'%{\ek%~\e\\%}$ '

␛kTITLE␛\ is an escape sequence recognised by GNU screen to set the title. See info screen -n 'Dynamic Titles' for details.

  • thanks for the answer, when you say that we can add the string PROMPT=$'%{\ek%~\e\\%}$ ' directly in our prompt instead of using precmd, does that means that I can write this line in my .zshrc file instead of the precmd function, and it will execute $'%{\ek%~\e\\%}$ ' each time I use the command lines ? Like a surveillance of the prompt action ? – hugogogo Jul 24 '18 at 9:55
  • so I tried: what you meant was to write PROMPT=$'%{\ek%~\e\\%}$ ' directly in the terminal prompt, it works indeed but weirdly: usually zsh show me multiple informations at the beginning of each prompt, like the git branch or the name of the folder, after typing your command line it's replaced by just a dollar sign $, also it doesn't solve my question which is to have the name of the file I'm working on, not only the folder – hugogogo Jul 24 '18 at 11:00
  • I'm wondering if I posted the question on the right forum ? maybe super user or stackoverflow are better place for my issue ? – hugogogo Jul 25 '18 at 13:30
  • i got one working answer here stackoverflow.com/a/51769020/9497573 – hugogogo Aug 14 '18 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.