I need to annotate a text file, so I would like to add a | pipe character to the end of each line to put my annotations relatively close to the original text. Each line is up to 72 characters in length. How might I move to the 74th character of the line, even if the line itself is shorter (i.e. add spaces if needed)?

My current solution is to simply add 72 spaces to the line, then to move to position 74 and delete the remaining spaces. Is there not a more elegant method in VIM?

My current (inelegant) macro:


I'm currently using VIM 8.0.133 on CentOS 7.3.

2 Answers 2


You could also enable 'virtualedit' option and directly jump to the column you're interested in.

A very similar question as been asked lately on SO: https://stackoverflow.com/questions/41964261/how-do-i-put-the-character-in-6th-column-and-80th-column-in-vi/41964372#41964372

  • 2
    Thank you, this seems to be what the virtualedit option was designed for. For future googlers, the command is :set virtualedit=all to enable and :set virtualedit= to disable.
    – dotancohen
    Feb 9, 2017 at 11:57

Here's one way to do it without having to edit and restore 'virtualedit':

:execute "normal " . string(73 - strwidth(getline('.'))) . "A " | normal A|

This runs two commands:

  1. The part before the bar | calculates the number of spaces required (73 - strwidth(getline('.'))) and then uses that as a count to a normal mode command that appends a single space, thus appending the correct number of spaces,.

  2. The part after the bar simply appends a pipe character |.

  • 1
    len(getline('.')) won't give a correct result if there are multi-bytes characters in the line. Beside, you could have simply appended repeat(' ',73-strwidth(getline('.'))).'|' -- repeat() ignores negative lengths. BTW, virtcol('$')-1 also returns the number we are looking for, col('$') returning the number of bytes Feb 9, 2017 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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