Define initial input for zsh read builtin
Bash's builtin read
command has a -i
option, which specifies an initial input, which the user can accept as it is or edit or add to. I cannot find anything similar for Zsh's read
command.
None of the options listed in the zshbuiltins
man page seem relevant:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to
the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.
-r Raw mode: a `' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.
-s Don't echo back characters if reading from the terminal.
-q Read only one character from the terminal and set name to `y' if this character was `y' or `Y' and to `n' otherwise. With this flag set the return status is zero only
if the character was `y' or `Y'. This option may be used with a timeout (see -t); if the read times out, or encounters end of file, status 2 is returned. Input is
read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.
-k [ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the ter-
minal unless one of -u or -p is present. This option may also be used within zle widgets.
Note that despite the mnemonic `key' this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.
-z Read one entry from the editor buffer stack and assign it to the first name, without word splitting. Text is pushed onto the stack with `print -z' or with push-line
from the line editor (see zshzle(1)). This flag is ignored when the -k or -q flags are present.
-e
-E The input read is printed (echoed) to the standard output. If the -e flag is used, no input is assigned to the parameters.
-A The first name is taken as the name of an array and all words are assigned to it.
-c
-l These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the cur-
rent command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.
-n Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word
number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.
-u n Input is read from file descriptor n.
-p Input is read from the coprocess.
-d delim
Input is terminated by the first character of delim instead of by newline.
-t [ num ]
Test if input is available before attempting to read. If num is present, it must begin with a digit and will be evaluated to give a number of seconds, which may be a
floating point number; in this case the read times out if input is not available within this time. If num is not present, it is taken to be zero, so that read returns
immediately if no input is available. If no input is available, return status 1 and do not set any variables.
This option is not available when reading from the editor buffer with -z, when called from within completion with -c or -l, with -q which clears the input queue before
reading, or within zle where other mechanisms should be used to test for input.
Note that read does not attempt to alter the input processing mode. The default mode is canonical input, in which an entire line is read at a time, so usually `read
-t' will not read anything until an entire line has been typed. However, when reading from the terminal with -k input is processed one key at a time; in this case,
only availability of the first character is tested, so that e.g. `read -t -k 2' can still block on the second character. Use two instances of `read -t -k' if this is
not what is wanted.
If the first argument contains a `?', the remainder of this word is used as a prompt on standard error when the shell is interactive.
The value (exit status) of read is 1 when an end-of-file is encountered, or when -c or -l is present and the command is not called from a compctl function, or as described
for -q. Otherwise the value is 0.
The behavior of some combinations of the -k, -p, -q, -u and -z flags is undefined. Presently -q cancels all the others, -p cancels -u, -k cancels -z, and otherwise -z can-
cels both -p and -u.
The -c or -l flags cancel any and all of -kpquz.
How can I achieve the same goal in Zsh?
zsh
add a comment |
Bash's builtin read
command has a -i
option, which specifies an initial input, which the user can accept as it is or edit or add to. I cannot find anything similar for Zsh's read
command.
None of the options listed in the zshbuiltins
man page seem relevant:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to
the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.
-r Raw mode: a `' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.
-s Don't echo back characters if reading from the terminal.
-q Read only one character from the terminal and set name to `y' if this character was `y' or `Y' and to `n' otherwise. With this flag set the return status is zero only
if the character was `y' or `Y'. This option may be used with a timeout (see -t); if the read times out, or encounters end of file, status 2 is returned. Input is
read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.
-k [ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the ter-
minal unless one of -u or -p is present. This option may also be used within zle widgets.
Note that despite the mnemonic `key' this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.
-z Read one entry from the editor buffer stack and assign it to the first name, without word splitting. Text is pushed onto the stack with `print -z' or with push-line
from the line editor (see zshzle(1)). This flag is ignored when the -k or -q flags are present.
-e
-E The input read is printed (echoed) to the standard output. If the -e flag is used, no input is assigned to the parameters.
-A The first name is taken as the name of an array and all words are assigned to it.
-c
-l These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the cur-
rent command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.
-n Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word
number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.
-u n Input is read from file descriptor n.
-p Input is read from the coprocess.
-d delim
Input is terminated by the first character of delim instead of by newline.
-t [ num ]
Test if input is available before attempting to read. If num is present, it must begin with a digit and will be evaluated to give a number of seconds, which may be a
floating point number; in this case the read times out if input is not available within this time. If num is not present, it is taken to be zero, so that read returns
immediately if no input is available. If no input is available, return status 1 and do not set any variables.
This option is not available when reading from the editor buffer with -z, when called from within completion with -c or -l, with -q which clears the input queue before
reading, or within zle where other mechanisms should be used to test for input.
Note that read does not attempt to alter the input processing mode. The default mode is canonical input, in which an entire line is read at a time, so usually `read
-t' will not read anything until an entire line has been typed. However, when reading from the terminal with -k input is processed one key at a time; in this case,
only availability of the first character is tested, so that e.g. `read -t -k 2' can still block on the second character. Use two instances of `read -t -k' if this is
not what is wanted.
If the first argument contains a `?', the remainder of this word is used as a prompt on standard error when the shell is interactive.
The value (exit status) of read is 1 when an end-of-file is encountered, or when -c or -l is present and the command is not called from a compctl function, or as described
for -q. Otherwise the value is 0.
The behavior of some combinations of the -k, -p, -q, -u and -z flags is undefined. Presently -q cancels all the others, -p cancels -u, -k cancels -z, and otherwise -z can-
cels both -p and -u.
The -c or -l flags cancel any and all of -kpquz.
How can I achieve the same goal in Zsh?
zsh
add a comment |
Bash's builtin read
command has a -i
option, which specifies an initial input, which the user can accept as it is or edit or add to. I cannot find anything similar for Zsh's read
command.
None of the options listed in the zshbuiltins
man page seem relevant:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to
the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.
-r Raw mode: a `' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.
-s Don't echo back characters if reading from the terminal.
-q Read only one character from the terminal and set name to `y' if this character was `y' or `Y' and to `n' otherwise. With this flag set the return status is zero only
if the character was `y' or `Y'. This option may be used with a timeout (see -t); if the read times out, or encounters end of file, status 2 is returned. Input is
read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.
-k [ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the ter-
minal unless one of -u or -p is present. This option may also be used within zle widgets.
Note that despite the mnemonic `key' this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.
-z Read one entry from the editor buffer stack and assign it to the first name, without word splitting. Text is pushed onto the stack with `print -z' or with push-line
from the line editor (see zshzle(1)). This flag is ignored when the -k or -q flags are present.
-e
-E The input read is printed (echoed) to the standard output. If the -e flag is used, no input is assigned to the parameters.
-A The first name is taken as the name of an array and all words are assigned to it.
-c
-l These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the cur-
rent command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.
-n Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word
number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.
-u n Input is read from file descriptor n.
-p Input is read from the coprocess.
-d delim
Input is terminated by the first character of delim instead of by newline.
-t [ num ]
Test if input is available before attempting to read. If num is present, it must begin with a digit and will be evaluated to give a number of seconds, which may be a
floating point number; in this case the read times out if input is not available within this time. If num is not present, it is taken to be zero, so that read returns
immediately if no input is available. If no input is available, return status 1 and do not set any variables.
This option is not available when reading from the editor buffer with -z, when called from within completion with -c or -l, with -q which clears the input queue before
reading, or within zle where other mechanisms should be used to test for input.
Note that read does not attempt to alter the input processing mode. The default mode is canonical input, in which an entire line is read at a time, so usually `read
-t' will not read anything until an entire line has been typed. However, when reading from the terminal with -k input is processed one key at a time; in this case,
only availability of the first character is tested, so that e.g. `read -t -k 2' can still block on the second character. Use two instances of `read -t -k' if this is
not what is wanted.
If the first argument contains a `?', the remainder of this word is used as a prompt on standard error when the shell is interactive.
The value (exit status) of read is 1 when an end-of-file is encountered, or when -c or -l is present and the command is not called from a compctl function, or as described
for -q. Otherwise the value is 0.
The behavior of some combinations of the -k, -p, -q, -u and -z flags is undefined. Presently -q cancels all the others, -p cancels -u, -k cancels -z, and otherwise -z can-
cels both -p and -u.
The -c or -l flags cancel any and all of -kpquz.
How can I achieve the same goal in Zsh?
zsh
Bash's builtin read
command has a -i
option, which specifies an initial input, which the user can accept as it is or edit or add to. I cannot find anything similar for Zsh's read
command.
None of the options listed in the zshbuiltins
man page seem relevant:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
Read one line and break it into fields using the characters in $IFS as separators, except as noted below. The first field is assigned to the first name, the second field to
the second name, etc., with leftover fields assigned to the last name. If name is omitted then REPLY is used for scalars and reply for arrays.
-r Raw mode: a `' at the end of a line does not signify line continuation and backslashes in the line don't quote the following character and are not removed.
-s Don't echo back characters if reading from the terminal.
-q Read only one character from the terminal and set name to `y' if this character was `y' or `Y' and to `n' otherwise. With this flag set the return status is zero only
if the character was `y' or `Y'. This option may be used with a timeout (see -t); if the read times out, or encounters end of file, status 2 is returned. Input is
read from the terminal unless one of -u or -p is present. This option may also be used within zle widgets.
-k [ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when -q is present. Input is read from the ter-
minal unless one of -u or -p is present. This option may also be used within zle widgets.
Note that despite the mnemonic `key' this option does read full characters, which may consist of multiple bytes if the option MULTIBYTE is set.
-z Read one entry from the editor buffer stack and assign it to the first name, without word splitting. Text is pushed onto the stack with `print -z' or with push-line
from the line editor (see zshzle(1)). This flag is ignored when the -k or -q flags are present.
-e
-E The input read is printed (echoed) to the standard output. If the -e flag is used, no input is assigned to the parameters.
-A The first name is taken as the name of an array and all words are assigned to it.
-c
-l These flags are allowed only if called inside a function used for completion (specified with the -K flag to compctl). If the -c flag is given, the words of the cur-
rent command are read. If the -l flag is given, the whole line is assigned as a scalar. If both flags are present, -l is used and -c is ignored.
-n Together with -c, the number of the word the cursor is on is read. With -l, the index of the character the cursor is on is read. Note that the command name is word
number 1, not word 0, and that when the cursor is at the end of the line, its character index is the length of the line plus one.
-u n Input is read from file descriptor n.
-p Input is read from the coprocess.
-d delim
Input is terminated by the first character of delim instead of by newline.
-t [ num ]
Test if input is available before attempting to read. If num is present, it must begin with a digit and will be evaluated to give a number of seconds, which may be a
floating point number; in this case the read times out if input is not available within this time. If num is not present, it is taken to be zero, so that read returns
immediately if no input is available. If no input is available, return status 1 and do not set any variables.
This option is not available when reading from the editor buffer with -z, when called from within completion with -c or -l, with -q which clears the input queue before
reading, or within zle where other mechanisms should be used to test for input.
Note that read does not attempt to alter the input processing mode. The default mode is canonical input, in which an entire line is read at a time, so usually `read
-t' will not read anything until an entire line has been typed. However, when reading from the terminal with -k input is processed one key at a time; in this case,
only availability of the first character is tested, so that e.g. `read -t -k 2' can still block on the second character. Use two instances of `read -t -k' if this is
not what is wanted.
If the first argument contains a `?', the remainder of this word is used as a prompt on standard error when the shell is interactive.
The value (exit status) of read is 1 when an end-of-file is encountered, or when -c or -l is present and the command is not called from a compctl function, or as described
for -q. Otherwise the value is 0.
The behavior of some combinations of the -k, -p, -q, -u and -z flags is undefined. Presently -q cancels all the others, -p cancels -u, -k cancels -z, and otherwise -z can-
cels both -p and -u.
The -c or -l flags cancel any and all of -kpquz.
How can I achieve the same goal in Zsh?
zsh
zsh
asked Nov 23 '18 at 23:39
iconoclasticonoclast
10.3k1076102
10.3k1076102
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In zsh you could manually set the variable and use vared
instead of read
:
name=iconoclast
vared -p "Enter your name: " name
This is the equivalent of the following in bash:
read -p "Enter your name: " -e -i iconoclast name
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453924%2fdefine-initial-input-for-zsh-read-builtin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In zsh you could manually set the variable and use vared
instead of read
:
name=iconoclast
vared -p "Enter your name: " name
This is the equivalent of the following in bash:
read -p "Enter your name: " -e -i iconoclast name
add a comment |
In zsh you could manually set the variable and use vared
instead of read
:
name=iconoclast
vared -p "Enter your name: " name
This is the equivalent of the following in bash:
read -p "Enter your name: " -e -i iconoclast name
add a comment |
In zsh you could manually set the variable and use vared
instead of read
:
name=iconoclast
vared -p "Enter your name: " name
This is the equivalent of the following in bash:
read -p "Enter your name: " -e -i iconoclast name
In zsh you could manually set the variable and use vared
instead of read
:
name=iconoclast
vared -p "Enter your name: " name
This is the equivalent of the following in bash:
read -p "Enter your name: " -e -i iconoclast name
answered Nov 29 '18 at 16:04
agostonbarnaagostonbarna
4612
4612
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453924%2fdefine-initial-input-for-zsh-read-builtin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown