Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VI to create text headers

Tags:

vi

Is there a way to easily create a text header in VI? I would like to create a header such as the one below without having to count characters for the purpose of centering.

# ********** a centered title **********

Any vi voodoo to be had? A better method?

like image 251
ccook Avatar asked Dec 09 '25 15:12

ccook


2 Answers

You can center the current line by using :center (in vim). Then use 'R' to fill in the asterisks after the fact. Still not fast or magical, but at least it saves you from counting characters.

So the steps would be:

  1. Type the title
  2. :center
  3. '0' back to beginning of line
  4. 'R' overwrite mode, put in your asterisks over the spaces

Upon further testing, you need to have "set expandtab" turned on, otherwise centering will use tabstops and you end up replacing them with single characters which undoes the centering.

like image 123
CodeGoat Avatar answered Dec 12 '25 10:12

CodeGoat


Here is a macro to add in .exrc file for old vi:

map £ A ^[80A£§^[080lD:s/§//g^M$byw0Pa ^[080lD:s/£/*/g^M0R# ^[^M

(Typing ^[ is obtained with the sequence CTRL-V followed by ESC. Typing ^M is obtained with the sequence CTRL-V followed by ENTER)

The idea is to:

  • fill the end of the line with a two-character pattern (£§, chars that are unlikely to appear in your code) repeated 80 times (that is, 160 chars)
  • remove everything on the line over 80 chars
  • remove half of the pattern (§ are removed and £ remain)
  • copy remaining pattern to the begining of line
  • replace pattern with whatever is convenient

To use the macro, put the cursor on the line to be centered and just type £. You can choose any character, e.g., @ by changing the macro name: map @ ...

like image 45
mouviciel Avatar answered Dec 12 '25 10:12

mouviciel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!