Vim Tips Wiki

Tlgrok/Form Feeds and Vertical Tabs

< User:Tlgrok

Revision as of 16:08, March 1, 2009 by Tlgrok (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
1,616pages on
this wiki

"Form feed" and "vertical tab" are two control characters found in the ASCII character coding scheme, with the the character codes 12 and 11 respectively. They are both meant to format a printed output: "form feed" represents a page break, and "vertical tab" is an instruction for the printer to move a certain number of rows down. These characters are both ignored by most modern terminals. Vim display "form feed" as ^L, and "vertical tab" as ^K. In Vim's insert mode, to insert a form feed press Ctrl-l; to insert a vertical tab, press Ctrl-k Ctrl-k Ctrl-k (that's Ctrl-k three times - you might want to use a mapping).

Important note: Throughout the rest of this tip, ^L shall be used to represent a form feed, and ^K a vertical tab. These are not the characters '^' and 'L' or 'K'; these are Vim's representation of the form feed and vertical tab control characters.``

Despite not longer being used in formatting printing output, these control characters still have their uses. Probably the most important thing about them is that, in many programming languages, such as C and C++, they are considered whitespace. Thus, the following is a perfectly valid C statement:


Below we'll examine example uses of these control characters.

Form feeds are useful right out of the box Edit

Vim treats form feeds in the first column as section delimiters (see :help section. If you're writing a plain-text treatise, for instance, you may use form feeds to delineate chapters. You may jump to start of the current or previous section by typing [[ in Normal mode, and to the start of the next section using ]].

As said above, C and C++ compilers treat form feeds as whitespace, so they may easily be used, uncommented, to group together related functions. The following appears in the GNU Coding Standards:

Please use formfeed characters (control-L) to divide the program into pages at logical places (but not within a function). It does not matter just how long the pages are, since they do not have to fit on a printed page. The formfeeds should appear alone on lines by themselves. [1]

Vertical tabs can be made useful Edit

While Vim doesn't have any special method to handle vertical tabs, they can be made useful using some scripting. Particularly, vertical tabs can be used as placeholders in template- or abbreviation-generated text. For instance, say you've got an abbreviation which produces the following:

for (;;) {};

There are obviously places in the above where additional text should be filled in. We can use a vertical tab as a placeholder, producing code which looks like the following:

for (^K;^K;^K) {^K};

(If you're using a color terminal, Vim will color the '^K's differently than the rest of the text, making them easier to spot.)

The neat thing is that, vertical tabs being whitespace, the above is perfectly valid C code, and, if we did not yet replace it, a placeholder can remain in place when a file is compiled.

To easily jump to the next placeholder, I use the following, which you can add to your .vimrc:

   let g:replace_placeholder = 1
   function <SID>FindPlaceHolder (forward, allow_replace)
       let l:place_holder = nr2char(11) "vertical tab
       let l:flags="W"
       if (!a:forward)
           let l:flags.="b"
       let l:match=search(l:place_holder, l:flags)
       if (!l:match)
           echo "No more placeholders in requested direction."
       if (a:allow_replace && g:replace_placeholder)
           normal! x
   inoremap <silent> <S-Left>  <C-o>:call <SID>FindPlaceHolder(0,1)<CR>
   inoremap <silent> <S-Right> <C-o>:call <SID>FindPlaceHolder(1,1)<CR>
   nnoremap <silent> [h :call <SID>FindPlaceHolder(0,0)<CR>
   nnoremap <silent> ]h :call <SID>FindPlaceHolder(1,0)<CR>

This defines two sets of bindings:

  • In Normal mode, '[h' and ']h' jump to the previous or next placeholder.
  • In Insert mode, Shift-<Left> and Shift-<Right> do the same thing. The global variable g:replace_placeholder determines whether the target placeholder should be automatically removed whenever a jump takes place. (The variable is declared global in case you want to put the above code in a plugin.)

Using these bindings, it is easy to jump to location where text needs to be filled in.

Note Edit

For consistency, you can use the mapping <S-Left> and <S-Right> in Normal mode as well. This is a matter of personal preference.

Around Wikia's network

Random Wiki