Paste from a file one line (or section) at a time

3.1k views Asked by At

I am a teacher using Windows and would like to be able to paste short program snippets one after another from a file of examples I have into whatever programming environment I am teaching (e.g. the python IDLE shell or editor). During the lecture I would have IDLE open and then use Ctrl-v to paste line 1 from the file into IDLE, execute & discuss it, then use Ctrl-v to paste line 2 from the file into IDLE, execute & discuss it, then use Ctrl-V to get line 3 into IDLE, and so on ...

I suspect there is some way to do this with a clipboard manager, but haven't found it online.

Being able to paste sections of code instead of just single lines would be really useful as well. The sections of code in the file could be separated by a blank line or some kind of text string indicator.

Having this functionality would allow me to have all my examples ready in a file and then during the lecture have quick access to all the examples one at a time by using Ctrl-v.

3

There are 3 answers

0
Lukas Wallrich On

Unfortunately, I cannot comment, but the great solution by @Patrick only works for me when I add a sleep command - otherwise, the clipboard content gets overwritten before the line is pasted. So if you run into a similar issue, the following version might do it:

#^v::
{
    originalClipboard := Clipboard
    StringSplit, ClipLines, originalClipboard, `n`r
    size := StrLen(ClipLines1) + 3
    Clipboard = %ClipLines1%
    Send ^v`n   
    sleep, 500 ;
    Clipboard := SubStr(originalClipboard, size)
    return
}
0
Brendan Scott On

Install the MultiLineRun.py script from the IdleX extensions for IDLE (or the whole of IdleX). Idlex is available here: http://idlex.sourceforge.net/.

If you want to automate it:

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Python 2.7.9 Shell") 
# or the title of your idle shell window
for line in source.readlines(): 
    # open your source file of examples 
    # better parse it into groups of commands
    # and work each group in a batch
    line= line.replace("(","{(}") # sendkeys escape
    line= line.replace(")","{)}")
    shell.SendKeys(line)
    shell.SendKeys("{ENTER}") # for good measure.  
"""SendKeys sends a string to the active window.
You can automate reading lines in batches linked to a button press etc
put in delays, copy per char etc 
Go to town and make it a mini slide show! 
"""
0
Patrick Szalapski On

The following AutoHotKey script will paste lines from the clipboard, one line at a time, when you press Win+Ctrl+V (on Windows).

If you haven't used AutoHotKey, I highly recommend it.

#^v::
{
    originalClipboard := Clipboard
    StringSplit, ClipLines, originalClipboard, `n`r
    size := StrLen(ClipLines1) + 3
    Clipboard = %ClipLines1%
    Send ^v`n   
    Clipboard := SubStr(originalClipboard, size)
    return
}

Caveats:

  • It may not robustly handle line endings--it only works for two-character \r\n endings (the Windows standard). This should be most if not all real-world usages.
  • AutoHotKey seems to be only for Windows.
  • After you paste a line, that line is removed from the clipboard so that you are ready for the next one.
  • It always pastes a whole line at a time, even if the source was a partial line.
  • When you run out of clipboard lines, it pastes blank lines till you realize it.
  • It adds a new line by sending a newline. Not sure if this works in all text editors, but it worked in Notepad and a few others I tried.
  • There may be other nuances that it doesn't handle well.