oh-my-zsh cursor up before program has finished vs after, how to make them behave consistently - oh-my-zsh

I've noticed that if I press up arrow at a prompt then I get the previous command and up again gets me the command before that.
Whereas if I press up arrow before the previous program has completed then instead I get the previous command displayed, the cursor is at the end of the line, but oh-my-zsh is now in "search for lines that start with ... " mode meaning I can't press up to get the previous command.
I'm sure this behavior is well known and expected but just in case you don't get it you can repo it like this
Type ls return
Type sleep 3 return
wait 3 seconds for prompt to appear
press ⬆ (should show sleep 3)
press ⬆ again (should show ls)
press return (to run ls)
Type sleep 3 return ⬆ (press the up arrow before the 3 seconds elapses)
It should now be showing sleep 3
Press ⬆
it will still be showing sleep 3 but it want it to be showing ls. Instead it is in "search for commands that start with sleep 3 mode instead of just go to previous command mode.
To make try to clear in both cases these are the steps
lsreturn
sleep 3return
⬆
⬆
But they end up with different results depending on if step3 happens before or after step2 finishes.
Note I saw this Q&A: https://unix.stackexchange.com/questions/324623/how-to-make-oh-my-zsh-history-behavior-similar-to-bashs
But that doesn't seem to be what I'm looking for. I like oh-my-zsh's partial line + up = search for lines that start with the partial. What I'm trying to fix is that if I press up on step 2 above it magically inserts a partial where as if I wait until step 2 finishes it doesn't.
How do I get oh-my-zsh to be consistent here so that a premature up arrow behaves the same as a normal up arrow?
I'm surprised this question isn't common. It's seriously infuriating to have the terminal act inconsistently. I'd except most devs using oh-my-zsh to run into this issue all the time and be massively frustrated.
The example above with sleep 3 is only to make it easy to show the problem. In actual usage the problem happens frequently even with short lived commands. I type say git status return git commit somefile -m "short comment" return ⬆⬆ expecting to see "git status". 66% of the time I get git status and the other 34% I get `git commit somefile -m "short comment" and pressing ⬆ again just blinks the cursor and I have to press Ctrl-C to break out of zsh's partial complete mode.
The fact that this does not seem to be a common complaint for oh-my-zsh makes me wonder if I have something setup wrong.
To make it clearer run zsh without oh-my-zsh.
zsh -d -f
autoload -U up-line-or-beginning-search
zle -N up-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search
Now try the steps above. You'll get consistent behavior.

This might be an overkill solution but, following this guide you can see that you can bind new actions to the up/down arrow key. So if you add:
bindkey "^[[A" up-line-or-beginning-search # Up
bindkey "^[[B" down-line-or-beginning-search # Down
to your ~/.zshrc, it should remove the functionality you talked about. I managed to get it to work while still maintaining regular search capabilities but this is not thoroughly tested and should probably be used with care.

Related

replace the kscreenlocker_greet

Sorry my English language
I need the following
If you launch the menu -Start -> Shutdown -> Lock
Then you can see the lock screen
Now press CTRL + ALT +F2
let's log in to this terminal under the root user
And let's see the running processes ps -eH | less
We will see the process tree, and who started whom
You can find the following
ksmserver -> kscreenlocker_greet
If you look at how kscreenlocker_greet was launched using ps -aux | grep greet
Then we will see the following
/usr/libexec/kf5/kscreenlocker_greet --immediateLock --ksldfd
Is it possible somewhere (I hope it's not hard code) to replace this command with another command or an application being launched? And if so, where can it be found? Very necessary!
I would like to replace the command call with another application.

Bash history inconsistent text

When in terminal and pressing the up arrow to go through my bash history the line will often get messed up. This is especially true when the history is multiline or has special characters in it.
It's difficult to provide and example, but if my history is:
1 echo hello world
2 export foo=bar
3 cat /my/file.txt
Then pushing up will give me first cat /my/file.txt then export foo=bar and then it may break and do something like execho hello world. Going back down then will stay broken.
My bash prompt is:
echo $PS1
%(?:%{%}➜ :%{%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)
I'm assuming there's an issue with my bash prompt, but I would hate to change it as I'm accustomed to the look/style. Would hopefully be able to find a solution.

reattaching to screen daemon makes backspace kill whole line

As part of my startup script to set up my desktop, I initialize a screen with several windows. I do this by starting a daemon and sending it -X screen and -X stuff commands, finally reattaching with -r.
Unfortunately, the "create daemon and reattach" method makes all the windows I created turn backspace into a "kill whole line" action. If I create new windows within screen with C-c c, the new windows do not have this behavior. Is this a screen bug, or can I do something special to fix this behavior? I'm using xfce4 and ubuntu 12.10 if that matters
Repro with the following:
screen -S -dm
screen -r
Type several characters and press backspace.
I'm not sure if I'm having the exact same problem as you, as your repro steps didn't work for me, but I did have the same bad behavior in screen (backspace killing the whole line), and managed to fix it.
For me, somehow I repeatedly get into a state where the output of stty is this:
$ stty
speed 9600 baud;
lflags: echoe echok echoke echoctl
iflags: -ixany -imaxbel ignpar
oflags: tab3
cflags: cs8 -parenb -hupcl clocal
eol eol2 erase2 kill min
^# ^# ^# ^H 0
Two things to note here:
There is no erase, only erase2
kill is mapped to ^H
#2 explains my issue, though #1 needs fixing, too.
Normally, ^U is "kill line", but here it is ^H instead.
If I type Ctrl-V, <backspace>, my terminal outputs ^H. So due to that mapping above, that causes the kill (kill line) to happen.
This fixed it for me:
$ stty kill ^U
# now, backspace outputs a literal ^H to the screen, so...
$ stty erase ^H
Note that in order to input the ^H and ^U, you have to use the literal control characters. I do this on my terminal with Ctrl-V, <backspace> and Ctrl-V, Ctrl-U, respectively.
I hope it helps!
I have found a work-around to this issue.
screen -r {session_name} -p 0 -X stuff "stty $(stty -g)"
screen -r {session_name} -p 0 -X stuff $'\n'
screen -r {session_name} -p 0 -X width $COLUMNS $LINES
screen -r {session_name} -p 0 -X stuff $'clear\n'
This takes the current tty settings and "stuffs" them into the screen session on Window 0, the default window that screen create on first launch. Then it "stuff" a newline to simulate pressing enter.
The next two lines are just to tell screen that initial columns and lines should match the calling terminal's columns and lines, then it clears the session's screen so that when you attach to the screen session your prompt will be at the top left. I was having issues of when attaching to the screen session the prompt would be in the middle of the terminal. A minor annoyance, but I wanted it gone.
NOTE: If you call the last two lines from within a script, those environment variables are not set. You will need to replace with $(tput cols) and $(tput lines)

GNU screen: how to highlight status bar when the command character is pressed

I have just started using the hardstatus capability of GNU screen to get a status bar of at the bottom of my terminal. This is particularly useful when running nested screen processes as I get nested status bars which makes it clear how many times I have to press the command key to access a particular screen instance.
For example
tom#stilton:~% ls
a/ b/ c/ d
.
.
.
[stilton] [1* zsh 2$ mutt]
[cheddar] [1$ emacs 2* ssh]
* indicates which window is selected. This makes it clear that I am running screen on cheddar with two windows: emacs and ssh. In the latter I am running another screen session on stilton with two windows: zsh and mutt. To switch to mutt on stilton I need to do "C-a a 2".
This is very useful, but could be more useful.
My question: when the command key is pressed, can I indicate this in the status bar, for example by highlighting it?
This would mean that when "C-a" is pressed the [cheddar] status bar would be highlighted and I would know that commands would be sent to that screen instance. If I were to further press "a" to send a literal "C-a" to the nested screen process the [cheddar] status bar would lose its highlight and the [stilton] status bar would highlight.
This would decrease the cognitive load of using screen considerably!
In git version, that feature was committed recently.
http://git.savannah.gnu.org/cgit/screen.git/commit/?id=db59704c1297c056cec0cc9305ae92e44a15bf33
This new string escape character %E can be used in the same manner as %F both caption and hardstatus.
In the following examples, when you press C-a, then foreground color of hostname %H changes to yellow
hardstatus alwayslastline '%m/%d %02c:%s %?%E%{= .Y}%:%{= .g}%?%H%{-} %L=%-w%45L>%{= g.}%n %t%{-}%+w %-17<%=%{= .y}(%l)'

What must I know to use GNU Screen properly? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I've just introduced a friend to GNU Screen and they're having a hard time getting used to it. That makes me think about the essential things he needs to know about the excellent Screen utility, the same things that you'd think worthwhile to teach someone, a beginner, from the ground up. What are some analogies and handy tips for remembering binds, etc.?
It would be awesome.
I've been using Screen for over 10 years and probably use less than half the features. So it's definitely not necessary to learn all its features right away (and I wouldn't recommend trying). My day-to-day commands are:
^A ^W - window list, where am I
^A ^C - create new window
^A space - next window
^A p - previous window
^A ^A - switch to previous screen (toggle)
^A [0-9] - go to window [0-9]
^A esc - copy mode, which I use for scrollback
I think that's it. I sometimes use the split screen features, but certainly not daily. The other tip is if screen seems to have locked up because you hit some random key combination by accident, do both ^Q and ^A ^Q to try to unlock it.
I couldn't get used to screen until I found a way to set a 'status bar' at the bottom of the screen that shows what 'tab' or 'virtual screen' you're on and which other ones there are. Here is my setup:
[roel#roel ~]$ cat .screenrc
# Here comes the pain...
caption always "%{=b dw}:%{-b dw}:%{=b dk}[ %{-b dw}%{-b dg}$USER%{-b dw}#%{-b dg}%H %{=b dk}] [ %= %?%{-b dg}%-Lw%?%{+b dk}(%{+b dw}%n:%t%{+b dk})%?(%u)%?%{-b dw}%?%{-b dg}%+Lw%? %{=b dk}]%{-b dw}:%{+b dw}:"
backtick 2 5 5 $HOME/scripts/meminfo
hardstatus alwayslastline "%{+b dw}:%{-b dw}:%{+b dk}[%{-b dg} %0C:%s%a %{=b dk}]-[ %{-b dw}Load%{+b dk}:%{-b dg}%l %{+b dk}] [%{-b dg}%2`%{+b dk}] %=[ %{-b dg}%1`%{=b dk} ]%{-b dw}:%{+b dw}:%<"
sorendition "-b dw"
[roel#roel ~]$ cat ~/scripts/meminfo
#!/bin/sh
RAM=`cat /proc/meminfo | grep "MemFree" | awk -F" " '{print $2}'`
SWAP=`cat /proc/meminfo | grep "SwapFree" | awk -F" " '{print $2}'`
echo -n "${RAM}kb/ram ${SWAP}kb/swap"
[roel#roel ~]$
Ctrl+A ? - show the help screen!
If your friend is in the habit of pressing ^A to get to the beginning of the line in Bash, he/she is in for some surprises, since ^A is the screen command key binding. Usually I end up with a frozen screen, possibly because of some random key I pressed after ^A :-)
In those cases I try
^A s and ^A q block/unblock terminal scrolling
to fix that. To go to the beginning of a line inside screen, the key sequence is ^A a.
You can remap the escape key from Ctrl + A to be another key of your choice, so if you do use it for something else, e.g. to go to the beginning of the line in bash, you just need to add a line to your ~/.screenrc file. To make it ^b or ^B, use:
escape ^bB
From the command line, use names sessions to keep multiple sessions under control. I use one session per task, each with multiple tabs:
screen -ls # Lists your current screen sessions
screen -S <name> # Creates a new screen session called name
screen -r <name> # Connects to the named screen sessions
When using screen you only need a few commands:
^A c Create a new shell
^A [0-9] Switch shell
^A k Kill the current shell
^A d Disconnect from screen
^A ? Show the help
An excellent quick reference can be found here. It is worth bookmarking.
Some tips for those sorta familiar with screen, but who tend to not remember things they read in the man page:
To change the name of a screen window is very easy: ctrl+A shift+A.
Did you miss the last message from screen? ctrl+a ctrl+m will show it again for you.
If you want to run something (like tailing a file) and have screen tell you when there's a change, use ctrl+A shift+m on the target window. Warning: it will let you know if anything changes.
Want to select window 15 directly? Try these in your .screenrc file:
bind ! select 11
bind # select 12
bind \# select 13
bind $ select 14
bind % select 15
bind \^ select 16
bind & select 17
bind * select 18
bind ( select 19
bind ) select 10
That assigns ctrl+a shift+0 through 9 for windows 10 through 19.
Ctrl+A is the base command
Ctrl+A N = go to the ***N***ext screen
Ctrl+A P = go to the ***P***revious screen
Ctrl+A C = ***C***reate new screen
Ctrl+A D = ***D***etach your screen
http://www.debian-administration.org/articles/34
I wrote that a couple of years ago, but it is still a good introduction that gets a lot of positive feedback.
I "must" add this: add
bind s
to your .screenrc, if You - like me - used to use split windows, as C-a S splits the actual window, but C-a s freezes it. So I just disabled the freeze shortcut.
Ctrl+a is a special key.
Ctrl+a d - [d]etach, leave programs (irssi?) in background, go home.
Ctrl+a c [c]reate a new window
Ctrl+a 0-9 switch between windows by number
screen -r - get back to detached session
That covers 90% of use cases. Do not try to show all the functionality at the single time.
Not really essential not solely related to screen, but enabling 256 colors in my terminal, GNU Screen and Vim improved my screen experience big time (especially since I code in Vim about 8h a day - there are some great eye-friendly colorschemes).
The first modification I make to .screenrc is to change the escape command. Not unlike many of you, I do not like the default Ctrl-A sequence because of its interference with that fundamental functionality in almost every other context. In my .screenrc file, I add:
escape `e
That's backtick-e.
This enables me to use the backtick as the escape key (e.g. to create a new screen, I press backtick-c, detach is backtick-d, backtick-? is help, backtick-backtick is previous screen, etc.). The only way it interferes (and I had to break myself of the habit) is using backtick on the command line to capture execution output, or pasting anything that contains a backtick. For the former, I've modified my habit by using the BASH $(command) convention. For the latter, I usually just pop open another xterm or detach from screen then paste the content containing the backtick. Finally, if I wish to insert a literal backtick, I simply press backtick-e.
There is some interesting work being done on getting a good GNU screen setup happening by default in the next version of Ubuntu Server, which includes using the bottom of the screen to show all the windows as well as other useful machine details (like number of updates available and whether the machine needs a reboot). You can probably grab their .screenrc and customise it to your needs.
The most useful commands I have in my .screenrc are the following:
shelltitle "$ |bash" # Make screen assign window titles automatically
hardstatus alwayslastline "%w" # Show all window titles at bottom line of term
This way I always know what windows are open, and what is running in them at the moment, too.
I use the following for ssh:
#!/bin/sh
# scr - Runs a command in a fresh screen
#
# Get the current directory and the name of command
wd=`pwd`
cmd=$1
shift
# We can tell if we are running inside screen by looking
# for the STY environment variable. If it is not set we
# only need to run the command, but if it is set then
# we need to use screen.
if [ -z "$STY" ]; then
$cmd $*
else
# Screen needs to change directory so that
# relative file names are resolved correctly.
screen -X chdir $wd
# Ask screen to run the command
if [ $cmd == "ssh" ]; then
screen -X screen -t ""${1##*#}"" $cmd $*
else
screen -X screen -t "$cmd $*" $cmd $*
fi
fi
Then I set the following bash aliases:
vim() {
scr vim $*
}
man() {
scr man $*
}
info() {
scr info $*
}
watch() {
scr watch $*
}
ssh() {
scr ssh $*
}
It opens a new screen for the above aliases and iff using ssh, it renames the screen title with the ssh hostname.
I like to set up a screen session with descriptive names for the windows. ^a A will let you give a name to the current window and ^a " will give you a list of your windows.
When done, detach the screen with ^a d and re-attach with screen -R
I like to use screen -d -RR to automatically create/attach to a given screen. I created bash functions to make it easier...
function mkscreen
{
local add=n
if [ "$1" == '-a' ]; then
add=y
shift;
fi
local name=$1;
shift;
local command="$*";
if [ -z "$name" -o -z "$command" ]; then
echo 'Usage: mkscreen [ -a ] name command
-a Add to .bashrc.' 1>&2;
return 1;
fi
if [ $add == y ]; then
echo "mkscreen $name $command" >> $HOME/.bashrc;
fi
alias $name="/usr/bin/screen -d -RR -S $name $command";
return 0;
}
function rmscreen
{
local delete=n
if [ "$1" == '-d' ]; then
delete=y
shift;
fi
local name=$1;
if [ -z "$name" ]; then
echo 'Usage: rmscreen [ -d ] name
-d Delete from .bashrc.' 1>&2;
return 1;
fi
if [ $delete == y ]; then
sed -i -r "/^mkscreen $name .*/d" $HOME/.bashrc;
fi
unalias $name;
return 0;
}
They create an alias to /usr/bin/screen -d -RR -S $name $command. For example, I like to use irssi in a screen session, so in my .bashrc (beneath those functions), I have:
mkscreen irc /usr/bin/irssi
Then I can just type irc in a terminal to get into irssi. If the screen 'irc' doesn't exist yet then it is created and /usr/bin/irssi is run from it (which connects automatically, of course). If it's already running then I just reattach to it, forcibly detaching any other instance that is already attached to it. It's quite nice.
Another example is creating temporary screen aliases for perldocs as I come across them:
mkscreen perlipc perldoc perlipc
perlipc # Start reading the perldoc, ^A d to detach.
...
# Later, when I'm done reading it, or at least finished
# with the alias, I remove it.
rmscreen perlipc
The -a option (must be first argument) appends the screen alias to .bashrc (so it's persistent) and -d removes it (these can potentially be destructive, so use at own risk). xD
Append:
Another bash-ism that I find convenient when working a lot with screen:
alias sls='/usr/bin/screen -ls'
That way you can list your screens with a lot fewer keystrokes. I don't know if sls collides with any existing utilities, but it didn't at the time on my system so I went for it.
^A A switches back to the screen you just came from.
Ctrl + A is a great special character for Unix people, but if you're using screen to talk to OpenVMS, then not being able to ^A is going to make you bald prematurely.
In VMS, if you're editing a DCL command prior to execution from the history buffer, Insert mode is off (it has to be for a few reasons I won't get into here) ... to turn it on so you don't over-type your command rather than space things out, you have to hit `^A.