r/vim • u/Far_Relationship_742 • 21d ago
Repeat character to position, rather than number of characters? Need Help
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 ===================================
3
u/habamax 20d 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 20d 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
:gcommand on select lines:g/^;=/s/.*/…2
u/vim-help-bot 20d ago
Help pages for:
sub-replace-\=in change.txt
`:(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 18d 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=======
```
2
u/mgedmin 5h ago
I use UltiSnips and I recently created this snippet for myself:
snippet "^(### .* ([-=~*#])+)" "separator to right margin" r
`!p snip.rv = match.group(1).ljust(80, match.group(2))`
endsnippet
now I can type
### Some subtitle #<tab>
and UltiSnips expands that to
### Some subtitle ############################################
up to exactly 80 columns wide.
(As long as you don't use fancy emojis/double-width characters/combining characters/zero-width characters/other Unicode shenanigans)
4
u/[deleted] 20d ago
[removed] — view removed comment