tags:

views:

286

answers:

2

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?

+1  A: 

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.

CodeGoat
Interesting, however, I need the tabs for make files... Is there a way to center with spaces and not remove all tabs? Ty!
ccook
Actually, a slight modification and I have found a nice method. Create a border above and below the center. Something like '80i*<ESC>' create the title line, ':center' and create the footer bottom border with '80i*<ESC>'.
ccook
Hmm... maybe someone knows a better way. All I can think of is to create a macro that turns on expandtab, does center, then turns it off. If you've never used macros it's basically 'q'<buffer><sequence of commands>'q', then you use it with '@'<buffer>. <buffer> is a single letter. So typing the following: "qa:set expandtab<enter>:center<enter>:set noexpandtab<enter>q" will build the macro. Then @a to repeat it.
CodeGoat
Thank you again! Can the macro be defined in a file such as .vimrc?
ccook
Usually the macros are saved in a viminfo file, so next time you open vim they still work.
CodeGoat
mouviciel's answer seems much better. I'd comment there but I don't have enough rep.
CodeGoat
Thank you CodeGoat, I'm trying to get his method working.
ccook
+2  A: 

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 @ ...

mouviciel
I'm a bit new to vi, could you show how to use the macro as well?
ccook
I have edited my answer. Hope this helps.
mouviciel
Thank you mouviciel. Another rather noobish question... How would one go entering £ in vi?
ccook
You can use any other character than £. Choose the most convenient for you.
mouviciel