How to rerun an inline loop in ZSH on single line (bash like behavior)
Asked Answered
A

1

6

I'd like to know if there is a way to get inline loops in zsh to behave like in bash.

I've started using zsh recently and so far I like it. There is one thing in zsh I would like to behave like in bash, inline loops.

I often run quick loops like so for in a b c -> hit enter -> do -> hit enter and so on...

When I recall the command/loop to change in zsh, lines are wrapped over multiple lines, but in bash it is just a single line.

ZSH initial run:

user@localhost > for i in {a..c}
for> do
for> echo ${i}
for> done
a
b
c

ZSH repeat < arrow up >:

user@localhost > for i in {a..c}
do
echo ${i}
done

BASH initial run:

[user@localhost ~]$ for i in {a..c}
> do
> echo ${i}
> done
a
b
c

BASH repeat < arrow up >:

[user@localhost ~]$ for i in {a..c}; do echo ${i}; done
Allium answered 24/10, 2016 at 21:40 Comment(0)
J
8

There is a lovely option mentioned in the Zsh Guide under Fancier Editing: it is the singlelinezle option; setting this causes "...editing to [be restricted to] one line," enabling the desired behavior that you mentioned in your question.

# without `singlelinezle` set
user% for a in a b c; do echo $a; done
# output:
# a
# b
# c
# <up-arrow>
user% for a in a b c; do echo $a; done

user% for a in a b c; do
for> echo $a; 
for> done
# output: 
# a 
# b
# c
# <up-arrow>
user% for a in a b c; do
echo $a
done

setopt singlelinezle
# with `singlelinezle` set
user% for a in a b c; do echo $a; done
# output:
# a 
# b
# c
# <up-arrow>
user% for a in a b c; do echo $a; done


user% for a in a b c; do
for> echo $a
for> done
# output:
# a
# b
# c
# <up-arrow>
user% for a in a b c; do\necho $a\ndone

unsetopt singlelinezle
Jaquelinejaquelyn answered 10/1, 2017 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.