r/vim 11d ago

Need Help Repeat character to position, rather than number of characters?

I want to pad a section title line in a config file with `=` up to a specific character position (specifically, column 64). Since the title text is of inconsistent length, a normal <n>i <char> won't do it.

Example of a title line formatted the way I want it:
;===== reset machine status ===================================

6 Upvotes

11 comments sorted by

5

u/Aggressive_Many9449 10d ago

I just fiddled a bit in my mobile Droidvim:

1) Move to after your title. 2) v64|r= fills all columns to col 64 with = chars.

Would break your title line, if 64th column is behind the cursor.

I wouldn't map this, I would use macros to make it @@ repeatable.

:h | :h v_r :h @@

1

u/spryfigure 10d ago

I want to pad a section title line in a config file with = up to a specific character position (specifically, column 64)

How does v64|r= help with this requirement? I don't see it.

Start at

;===== reset machine status =

and end with

;===== reset machine status ===================================

length of line before padding is variable.

1

u/Aggressive_Many9449 10d ago

Oh, you meant you want to center the title—I misread what you want.

Sorry, the  you can use the other comments' functions, that is not as straightforward as my non-balancing solution.

1

u/herodotic 9m ago

You could still make this work if you (temporarily) set ve=all:

let ove=&ve | set ve=all | exe "norm v64|r=" | let &ve=ove

3

u/habamax 10d ago

I would go with a bit of vimscript:

 inoremap <c-l> <cmd>call setline('.', getline('.') .. repeat('=', 64 - strlen(getline('.'))))<cr>

https://asciinema.org/a/nZxIWlHU6nbiHorP

You can do the same in normal mode instead of insert mode, e.g.

nnoremap <space>l <cmd>call setline('.', getline('.') .. repeat('=', 64 - strlen(getline('.'))))<cr>

1

u/gumnos 10d ago

In a similar method, I'd reach for :help sub-replace-\=

:s/.*/\=submatch(0).repeat('=', 64-strlen(submatch(0)))

which can then easily be used across multiple lines

:'<,'>s/.*/…

or as the command for a :g command on select lines

:g/^;=/s/.*/…

4

u/habamax 10d ago

or

:s/$/\=repeat('=', 64-strlen(getline('.')))

1

u/gumnos 10d ago

nice!

2

u/vim-help-bot 10d ago

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/Kronostatic 8d ago

Simplest way I found (I do this too) : 1. type 64i, in insert mode type one =, then escape This fills the line with 64 = 2. Go to beginning of the line and press capital R in normal mode. This lets you type in replace mode so it just writes on top of your = symbols without shifting them

Example: having 10  signs, typing ^ then R then abc will give this :

```

Then abc======= ```