0

For example, I wish to change

1
1
2
3
5
8
13

to

 1
 1
 2
 3
 5
 8
13

Where this is not necessarily at the start of the line. How can I do this?

  • If you want to do some complex columns manipulations you might want to check csv.vim by Christian Brabandt – statox Oct 8 '18 at 10:56
2

Move your cursor to the first single-digit number, the first 1 in this case. Type Ctrl-v. Then move your cursor to the last single-digit number, the 8 in this case. There are several ways to move the cursor. 5j will work. Then type

I <Esc>

where <Esc> is the Esc key and there is a single space between the I and the <Esc>. That inserts a space in front of the visually-selected block. See

:help 10.5
:help CTRL-V
:help visual.txt
  • I would probably add one more use case with set virtualedit=block and A <Esc> if group of lines have different lengths. – Maxim Kim Nov 12 '18 at 8:53
1
:set number 
<Enter>
<ESC>
:1,6s/^/ /

Turn numbering on to make it easier to see what lines need to be changed.
In colon mode, enter the number of the first line, comma, and the last line to be changed.
Follow with s for substitution,
/^/ the ^caret searches for the beginning of the line,
/ / and insert a space (there is a space between the slashes).

  • You're searching for the start of the line but the question states "this is not necessarily at the start of the line". – Rich Nov 12 '18 at 15:50
0

In case the lines with two chars and the lines with one char is interleaved, such as

1
2
13
3
8
13

The visual block selection will not work. However, the following method still works:

:%s/\ze.\%2c$/ /

Where

  1. \ze marks the end of match so that only the part before it is to be substituted (with a space) (see :h /\ze)
  2. . takes away one char
  3. \%2c matches the second column
  4. \%2c$ means the end of line is at the second column, so that the line only contains one char
  • An alternative is :g/\%2c$/s/^/ / where you use :global to distinguish the pattern to search and the pattern to be replaced. – Liu Sha Nov 12 '18 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.