Galadude
Galadude

Reputation: 253

Yank column number in Vim

I'd like to be able to easily make pretty code titles in vim, by making a macro. I'd like them to like something like this:

################################################################################
### Preamble                                                                 ###
################################################################################

To make these I'd like to start with a line with just:

Preamble

Then the macro will make the surrounding hashes and spaces. To do this, I need to somehow yank the number for characters in the title. So in the case of preamble; I'd like to copy 8, its length, to some register.

Any ideas how to do this?

Upvotes: 0

Views: 145

Answers (2)

Jim K
Jim K

Reputation: 13790

Instead of counting the length, I would paste using Replace mode, as @FDinoff said. First, yank the following line into a register, for example the t register: "tyy

###                                                                          ###

Next, grab the Preamble line without the endline: 0vg_d

Then, paste our line from t and move to the appropriate spot: "tP4l

Finally, paste Preamble using Replace mode: RCtrl+r"

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172520

I would not recommend this style, as it's maintenance requires high(er) effort (and not everybody is using a powerful editor like Vim, or has your macros), but you can do this with the strdisplaywidth() function:

:echo strdisplaywidth(getline('.'))

Older Vim versions don't have this; strlen() is a replacement that will only handle normal ASCII letters.

Oh, and before you ask, you can create the header lines with repeat('#', num)

Upvotes: 2

Related Questions