Auto insert befores in VIM

I would like to quickly change the document

00 01 10 11 

to look like

 000 001 010 011 100 101 110 111 

Of course, I can do it by brute force (as shown above), but I would like to do it quickly, because I find that I do a lot of them. So far, my approach has been to pull and insert 4Y 4j 4P

 00 01 10 11 00 01 10 11 

Then is there a command to insert 0 before each line 4 times? I know this is something like 4, 0, i, 0, esc ~~ I want it to be 4 rounds 0 (jump to the left of the line), insert mode, insert 0, command line mode.

thanks

+4
source share
4 answers
  • Using visual block mode:

     y3j <Cv>3j I0<Esc> '> p <Cv>3j I1<Esc> 

    You can start with Appendix 1 and use P to skip '> :

     y3j <Cv>3j I1<Esc> P <Cv>3j I0<Esc> 
  • Using Ex commands only:

     :,+3y<CR> :,+3norm! I0<CR> :put :<Up> "use commandline history to save some typing <Backspace>1<CR> 
  • Using a combination of substitution and normal mode commands:

     y3j :,+3s/^/0<CR> p :<Up> <Backspace>1<CR> 

If you do so much, writing this in a macro is probably the best strategy. At this point, how the actual filling is done is actually not relevant, as the only thing you do is @x , and the macro is likely to be instantaneous anyway.

What you do while recording doesn't really matter, here is an example:

 qx y3j :,+3s/^/0<CR> p :,+3s/^/1<CR> q 

Suppose you have this:

 [0]0 01 10 11 

Pressing @x results in:

 000 001 010 011 100 101 110 111 

Your macro is stored in register x and will be available for the next Vim session.

And I'm sure someone can come up with a good liner.

change

Step-by-step explanation of the macro above:

  • qx , start writing to register x (this can be any available register).

  • y3j , y3j current line and the next three.

  • : , no explanation is required.

  • ,+3 , this is the range we are working on. A more correct way to determine this range would be .,+3 . The start line and end line of the range are separated by a coma.

    The start line is the current line that we can omit to preserve some typing, so we are left with a coma, followed by the end line, expressed relative to the current line +3 .

  • s/^/0<CR> , this is a simple substitution.

    ^ in the search pattern means "start of line". It does not match the first character, so it is great for situations like this, where we want to add something to the string.

    So basically we add a line with 0 . It may be what you want.

    When you perform a range swap, substitution is performed on each row of the range. Here we have four lines, so each line is added using 0 .

  • <CR> , is, well ... the <Enter> key used to perform the substitution.

  • P , insert the four lines that we pulled before the current line.

  • :,+3s/^/1<CR> , as before, but with 1 .

  • q , completion of recording.

And to answer your comment, here is how you do it:

 00 01 10 11 

in it:

 00 0.0 01 0.0 10 0.0 11 0.0 

You need to use another "anchor". ^ is the "beginning of the line", $ is the "end of the line", so the substitution becomes:

 :,+3s/$/ 0.0 
+4
source

I believe you mean <cv>(motion)I

 CTRL-V Start Visual mode blockwise. Note: Under Windows CTRL-V could be mapped to paste text, it doesn't work to start Visual mode then, see |CTRL-V-alternative|. I Insert text before the first non-blank in the line [count] times. When the 'H' flag is present in 'cpoptions' and the line only contains blanks, insert start just before the last blank. 

In your example, where the cursor is on the top / left, this could become something like this

 <cv>3jI0<esc> 
+2
source

You can create a macro q a I 0 Esc j , and then run it 4 @ a .

But what you want to achieve is the gray code. The search may have some script already written for this (I cannot find it).

EDIT: If you can use not-only-vim, then you can do this:

 :%!ruby -e 'puts (0..7).map { |i| i.to_s(2).rjust(3, "0") }' 

Which will replace the entire document with the generated numbers. You can also change the length and range.

+2
source

You can also accomplish this with the visincr plugin , as suggested here . In this case, you will do it as follows:

  • Insert 0 into an empty string.
  • Make a visual block that spans 8 lines, including 0.
  • Run :IIB 1 0 .

Result:

 000 001 010 011 100 101 110 111 
0
source

All Articles