0 0 0 0


I'd like to print a string to command line / terminal in Windows and then edit / change the string and read it back. Anyone knows how to do it? Thanks

print "Hell"
Hello!  <---Edit it on the screen
s = raw_input()
print s

Best Answer:

If it's for your own purposes, then here's a dirty wee hack using the clipboard without losing what was there before:

def edit_text_at_terminal(text_to_edit):
    import pyperclip
    # Save old clipboard contents so user doesn't lose them
    old_clipboard_contents = pyperclip.paste()
    #place text you want to edit in the clipboard
    # If you're on Windows, and ctrl+v works, you can do this:
    import win32com.client
    shell = win32com.client.Dispatch("WScript.Shell")
    # Otherwise you should tell the user to type ctrl+v
    msg = "Type ctrl+v (your old clipboard contents will be restored):n"
    # Get the new value, the old value will have been pasted
    new_value= str(raw_input(msg))
    # restore the old clipboard contents before returning new value
    pyperclip.copy(old_clipboard_contents )
    return new_value

Note that ctrl+v doesn't work in all terminals, notably the Windows default (there are ways to make it work, though I recommend using ConEmu instead).

Automating the keystrokes for other OSs will involve a different process.

Please remember this is a quick hack and not a "proper" solution. I will not be held responsible for loss of entire PhD dissertations momentarily stored on your clipboard.

For a proper solution there are better approaches such as curses for Linux, and on Windows it's worth looking into AutHotKey (perhaps throw up an input box, or do some keystrokes/clipboard wizardry).

Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs