I was thinking to take a screenshot of commands and outputs, a thought or a question come to me:

Is there anyway I can refer to a line of command without (re-)pasting?

If I have some simple and short reference points, it would be easier to write for me, and clearer to read for readers.

1   IPython Prompt Numbers

Immediately, I thought of IPython, even I don’t even have it installed nor do I use it, I probably only had used it a couple of times since the day I learned Python. Nevertheless, I always could tell the console of IPython.

IPython has something called Prompt Numbers, it would look like, from Introducing IPython:

In [1]: %timeit range(1000)
100000 loops, best of 3: 7.76 us per loop

In [2]: %%timeit x = range(10000)
   ...: max(x)
   ...:
1000 loops, best of 3: 223 us per loop

As you can see, there are [1] and [2], good and clear referencing points for writing.

2   Bash Command Number

In Bash prompt, there is Command Number (\#) for PS1 (also History Number \!), which is the number of current command of current session, for example:

PS1 $ bash
PS1 $ PS1='[\#] \$ '
[2] $ true
[3] $ false
[4] $

The problem is, perhaps my lack knowledge of session, I can’t find a way to reset a session, that is resetting the number. The only way I know is starting a new shell process.

3   Numbering with Arithmetic Expansion

The following simple prompt utilizing Arithmetic Expansion does the trick, you can easily reset the number [3] without starting a new shell:

PS1 $ bash
PS1 $ PS1='[$((++N))] \$ '
[1] $ true
[2] $ false
[3] $ N=0
[1] $ true
[2] $

If you want to produce a clear output for either a screenshot or pasting the text, mis-typing happens, although you still need to re-type, but if you don’t need to start a new shell, just a quick a reset the variable, that might be more preferable, at least for me.

The only drawback I am aware of is if you just press without entering anything, the Command Number would not be increased, but the method above would, which may not be you want if you are super picky.