* function to replace the command line text @ 2014-04-18 19:49 Dave Yost 2014-04-18 21:55 ` Frank Terbeck ` (2 more replies) 0 siblings, 3 replies; 4+ messages in thread From: Dave Yost @ 2014-04-18 19:49 UTC (permalink / raw) To: zsh-workers Is there a builtin function that replaces the command line with the function's output? The completion mechanism must use such a function, so there has to be something like that. I want to use that function to build a function I can use to run a demo consisting of a sequence of commands. * Make an array of strings * For each step of the demo * Type a command or a keyboard shortcut that grabs the next string from the array and places it on the command line * Hit Enter to execute the command Thanks ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: function to replace the command line text 2014-04-18 19:49 function to replace the command line text Dave Yost @ 2014-04-18 21:55 ` Frank Terbeck 2014-04-19 3:27 ` Bart Schaefer 2014-04-24 20:34 ` zsh script to run prepared shell commands for a demo Dave Yost 2 siblings, 0 replies; 4+ messages in thread From: Frank Terbeck @ 2014-04-18 21:55 UTC (permalink / raw) To: Dave Yost; +Cc: zsh-workers Dave Yost wrote: > Is there a builtin function that replaces the command line with the > function's output? The completion mechanism must use such a function, > so there has to be something like that. > > I want to use that function to build a function I can use to run a > demo consisting of a sequence of commands. > > * Make an array of strings > * For each step of the demo > * Type a command or a keyboard shortcut that grabs the next string > from the array and places it on the command line > * Hit Enter to execute the command Here's an outline: zsh% foo=( FOO BAR BAZ QUUZ ) zsh% j=1 zsh% FOO() { BUFFER=${foo[j++]}; CURSOR=${#BUFFER} } zsh% zle -N FOO zsh% bindkey '^L' FOO Then press Ctrl-L to get (_ is the cursor position): zsh% FOO_ Then Ctrl-L again for: zsh% BAR_ etc... You get the idea. Regards, Frank -- In protocol design, perfection has been reached not when there is nothing left to add, but when there is nothing left to take away. -- RFC 1925 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: function to replace the command line text 2014-04-18 19:49 function to replace the command line text Dave Yost 2014-04-18 21:55 ` Frank Terbeck @ 2014-04-19 3:27 ` Bart Schaefer 2014-04-24 20:34 ` zsh script to run prepared shell commands for a demo Dave Yost 2 siblings, 0 replies; 4+ messages in thread From: Bart Schaefer @ 2014-04-19 3:27 UTC (permalink / raw) To: Dave Yost, zsh-workers On Apr 18, 12:49pm, Dave Yost wrote: } } Is there a builtin function that replaces the command line with the } function's output? As Frank demonstrated, the most basic "function" for this is to assign to the special parameter $BUFFER from inside a user-defined ZLE widget. However ... } I want to use that function to build a function I can use to run a } demo consisting of a sequence of commands. } * Make an array of strings } * For each step of the demo } * Type a command or a keyboard shortcut that grabs the next string } from the array and places it on the command line } * Hit Enter to execute the command There are a bunch of ways to do this. If you just want to execute the commands one after another, you could: - Store the commands in a file, one per line; - Load the file as history with "fc -R" or "fc -p"; - bindkey ^M accept-and-infer-next-history; - scroll back (up-history) to the first command; - each subseqent enter executes the command and advances to the next. This has the advantage that you can up-history to redo any command. If that isn't likely to be necessary you can: - push the commands onto the buffer stack, in reverse order, with one "print -z" per command; - press enter to load the first command; - each subseqent enter executes the command and pops the next. Here you'd have to use the push-line widget and then up-history if you want to redo a command. If you want to be able to go off-script to execute some other commands and then resume the demo, then you'll need something like what Frank outlined, with a key binding to produce the next step when ready. If you hit the "next step" key more than once, though, you'll skip over a command step and have no way to get back to it, unless you also create another binding that steps down through the array. Here's a variation of Frank's FOO widget that lets you select which step of the demo you want, and by default goes on to the next: FOO() { if (( NUMERIC > 0 )) then (( j=NUMERIC )) elif (( NUMERIC < 0 )) then (( NUMERIC > -j ? (j+=NUMERIC) : (j=1) )) fi BUFFER=${foo[j++]}; CURSOR=${#BUFFER} } You press ESC 2 ^L, for example, to select the second step, or to go backward, ESC - ESC 1 ^L (repeats the last step). ^ permalink raw reply [flat|nested] 4+ messages in thread
* zsh script to run prepared shell commands for a demo 2014-04-18 19:49 function to replace the command line text Dave Yost 2014-04-18 21:55 ` Frank Terbeck 2014-04-19 3:27 ` Bart Schaefer @ 2014-04-24 20:34 ` Dave Yost 2 siblings, 0 replies; 4+ messages in thread From: Dave Yost @ 2014-04-24 20:34 UTC (permalink / raw) To: zsh-workers With the kind help of Bart Schaefer and Frank Terbeck, I think this is pretty much done. See http://yost.com/computers/demo-shell-commands/demo-shell-commands.zsh On Fri 2014-04-18, at 12:49 PM, Dave Yost <Dave@Yost.com> wrote: > Is there a builtin function that replaces the command line with the function's output? The completion mechanism must use such a function, so there has to be something like that. > > I want to use that function to build a function I can use to run a demo consisting of a sequence of commands. > * Make an array of strings > * For each step of the demo > * Type a command or a keyboard shortcut that grabs the next string from the array and places it on the command line > * Hit Enter to execute the command > > Thanks ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-04-24 20:35 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2014-04-18 19:49 function to replace the command line text Dave Yost 2014-04-18 21:55 ` Frank Terbeck 2014-04-19 3:27 ` Bart Schaefer 2014-04-24 20:34 ` zsh script to run prepared shell commands for a demo Dave Yost
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).