How can I use emacs string-insert-rectangle operation to add a vector of numbers to a series of lines? For example, I've got this shortened version of a bunch of text entries in my emacs buffer:
element01 8 111111111011010000100000001100101000001111101111011111111000
element01 8 111101101010101001111111111000111110111111011110100101010111
element01 8 111111011001001110111010111111100111010110101011111010110011
element01 8 111111111111111111111111010111101101011101011111000001100000
element01 8 111100111111011111100110110000001011110101000011111011111101
element01 8 111001001011000000011100000101001001100101101011101101011011
element01 8 111011111101101111111111111101101010111110111011111101011011
element01 8 101101111101101111000110111101111010111011101111001101001011
element02 6 110101110101100101100101000111010101110111001001101111111011
element02 6 111001011001001011101110111100111101101011111111111011111101
element02 6 101111100111010111111010010101111101111111101101111011111011
element02 6 111101111111111100111110110111101011111001001101101100111111
element02 6 111111010111101111010011110111001100001000101010111111111101
element02 6 111110111001101111111100111011110000011011100100100111111010
element03 13 110011011111111111101011100111111110011111110100111010011111
element03 13 100011101000111110101101000000000001110110110011110110111101
element03 13 101100011100011111110111110110101101111111110110110100101111
element03 13 111111011110101110101011010111110000010111111011100100011111
element03 13 011100110110110111100101110101111110111100101110010111110011
element03 13 100111111111100100111110110110111111111101011101110110110111
element03 13 101111111111110101110110111011111110111101110110111111111111
element03 13 111110010111110110101111110110111111111110101111111101110011
element03 13 100111111101110110110110111110111010111110110011111111110111
element03 13 110100110111110110110100111010110100110110110110110101111111
element03 13 011111011010111101101001011100111110010111111011111101011010
element03 13 011101111110010000111000000101101010111110100010110110110110
element03 13 110100110110110010101010100011100011000000110011011100110100
element03 13 010101101010110010111100101001001010111001100111110000011011
[...]
And I want to add a column between the second and third column that will look like this:
element01 8 id1 111111111011010000100000001100101000001111101111011111111000
element01 8 id2 111101101010101001111111111000111110111111011110100101010111
element01 8 id3 111111011001001110111010111111100111010110101011111010110011
element01 8 id4 111111111111111111111111010111101101011101011111000001100000
element01 8 id5 111100111111011111100110110000001011110101000011111011111101
element01 8 id6 111001001011000000011100000101001001100101101011101101011011
element01 8 id7 111011111101101111111111111101101010111110111011111101011011
element01 8 id8 101101111101101111000110111101111010111011101111001101001011
element02 6 id9 110101110101100101100101000111010101110111001001101111111011
element02 6 id10 111001011001001011101110111100111101101011111111111011111101
element02 6 id11 101111100111010111111010010101111101111111101101111011111011
element02 6 id12 111101111111111100111110110111101011111001001101101100111111
element02 6 id13 111111010111101111010011110111001100001000101010111111111101
element02 6 id14 111110111001101111111100111011110000011011100100100111111010
element03 13 id15 110011011111111111101011100111111110011111110100111010011111
element03 13 id16 100011101000111110101101000000000001110110110011110110111101
element03 13 id17 101100011100011111110111110110101101111111110110110100101111
element03 13 id18 111111011110101110101011010111110000010111111011100100011111
element03 13 id19 011100110110110111100101110101111110111100101110010111110011
element03 13 id20 100111111111100100111110110110111111111101011101110110110111
element03 13 id21 101111111111110101110110111011111110111101110110111111111111
element03 13 id22 111110010111110110101111110110111111111110101111111101110011
element03 13 id23 100111111101110110110110111110111010111110110011111111110111
element03 13 id24 110100110111110110110100111010110100110110110110110101111111
element03 13 id25 011111011010111101101001011100111110010111111011111101011010
element03 13 id26 011101111110010000111000000101101010111110100010110110110110
element03 13 id27 110100110110110010101010100011100011000000110011011100110100
element03 13 id28 010101101010110010111100101001001010111001100111110000011011
[...]
How can I use something like string-insert-rectangle in emacs to add this new third column with increasing number count?
PS: I know I could do this with a bash/perl/python/etc script, in this question I am asking if this can be easily done with emacs.
I think the simplest solution is to mark the first character of the original third column in the first line, move point to the same character of the last line, and then type:
C-uC-xrNRET id%d
RET
rectangle-number-lines is an interactive compiled Lisp function in `rect.el'.
It is bound to C-x r N.
(rectangle-number-lines START END START-AT &optional FORMAT)
Insert numbers in front of the region-rectangle.
START-AT, if non-nil, should be a number from which to begin counting. FORMAT, if non-nil, should be a format string to pass to `format' along with the line count. When called interactively with a prefix argument, prompt for START-AT and FORMAT.
The regexp-replace and macro techniques are both superb general-purpose tools to know, but rectangle-number-lines is pretty much custom-built for this very question.
Edit: I hadn't noticed at the time, but it turns out that this is a new feature in Emacs 24. Earlier versions of Emacs will translate that sequence to C-x r n
(lower-case n) which runs an entirely different function.
You can use query-replace-regexp
directly, by adding a new column with the match count \#
.
The matches look for 3 columns separated by spaces, which will be stored in submatch strings \1
to \3
. The replaced string adds a new column using the match count.
Version 1 (simpler, but starts at 0):
M-x query-replace-regexp RET
^\(\w+\)\ +\(\w+\)\ +\(\w+\)$ RET
\1 \2 id\# \3 RET
Note I used spaces for matching and replacing. You can use tabs instead.
Version 2 (uses lisp to customize the row count with the +1
function):
M-x query-replace-regexp RET
^\(\w+\)\ +\(\w+\)\ +\(\w+\)$ RET
\,(format "%s %s id%d %s" \1 \2 (+1 \#) \3) RET
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With