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

clipboard, paste, presentation, windows

Solution

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.

Problem

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.

Original source