3

Is there any command that will output text to the (next) command prompt?

I would like to write a function, which upon execution, will perform some commands and then "suggest" the next command to be executed.

~/Desktop $ foo
~/Desktop $ some_new_command_which_user_did_not_enter
3

Try this :

xvkbd -xsendevent -text ls

Check http://homepage3.nifty.com/tsato/xvkbd/

  • Great, that works! I also did some extra things: I added 2>/dev/null & to the end. The 2>/dev/null was because I was getting some useless warnings. And the & because without it, for longer strings, it was displaying some of the string before the next prompt appeared. Lastly, I wrapped the whole thing in parentheses to silence the job control messages that came from using &. – Garrett Jan 22 '15 at 23:40
  • 1
    @Garrett I don't recommend this. This could fail badly if you switch windows at the wrong time. – Gilles Jan 23 '15 at 23:04
  • Strangely, this only works with Guake and not GNOME terminal. – Garrett Jan 27 '15 at 21:39
0

Any POSIX-compliant shell will implement a vi command editing mode which can be enabled like set -o vi and disabled like set +o vi. A vi editing mode will distinguish between insert and command modes. Each prompt should default to the input mode, but pressing the ESC key will switch contexts to the command mode.

Some relevant features of this mode include:

  • <control>-L
    • Redraw the current command line. Position the cursor at the same location on the redrawn line.
  • #
    • Insert the character '#' at the beginning of the current command line and treat the resulting edit line as a comment. This line shall be entered into the command history; see fc.
  • =
    • Display the possible shell word expansions (see Word Expansions) of the bigword at the current command line position.
  • @letter
    • Insert the value of the alias named _letter. The symbol letter represents a single alphabetic character from the portable character set; implementations may support additional characters as an extension. If the alias _letter contains other editing commands, these commands shall be performed as part of the insertion. If no alias _letter is enabled, this command shall have no effect.

Note: the effects of @_[letter] are recursive and special characters can be escaped for literal input effect with CONTROL+V[CHAR]

  • [count]-

    • Set the current command line to be the countth previous command line in the shell command history. If count is not specified, it shall default to 1. The cursor shall be positioned on the first character of the new command. If a k or - command would retreat past the maximum number of commands in effect for this shell (affected by the $HISTSIZE environment variable), the terminal shall be alerted, and the command shall have no effect.
  • [count]ymotion

    • Yank (that is, copy) the characters from the current cursor position to the position resulting from the motion command into the save buffer. A number count shall be applied to the motion command. If the motion command would move toward the beginning of the command line, the character under the current cursor position shall not be included in the set of yanked characters. If the motion command is y, the entire current command line shall be yanked into the save buffer. The current cursor position shall be unchanged. If the count is larger than the number of characters between the current cursor position and the end of the command line toward which the motion command would move the cursor, this shall not be considered an error; all of the remaining characters in the aforementioned range shall be yanked.
  • Y

    • Yank the characters from the current cursor position to the end of the line into the save buffer. The current character position shall be unchanged.
  • [count]p

    • Put a copy of the current contents of the save buffer after the current cursor position. The current cursor position shall be advanced to the last character put from the save buffer. A count shall indicate how many copies of the save buffer shall be put.
  • [count]P

    • Put a copy of the current contents of the save buffer before the current cursor position. The current cursor position shall be moved to the last character put from the save buffer. A count shall indicate how many copies of the save buffer shall be put.

Many similar effects can be had via shell-specific line-editors (like bash's readline's bind command via ~/.input.rc or the BSD-licensed editline's ~/.editrc).

  • 1
    I'm a bit confused. Can you give me example? What command would I put in a Bash function to get the text ls to appear at the prompt when the function is executed? – Garrett Jan 27 '15 at 21:50

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.