Kim Schulz asked on November 12, 2006 12:30 PM
  >
  > Hi,
  >
  > I am currently looking into different tricks for formatting
  > text, code, etc. in Vim. I guess most users know the format-
  > paragraph command gqq or the reindent entire code 1G=G But are
  > there any other neat tricks - which ones are your favorites?

  Here's what I have in my vimrc (some long lines might get wrapped
  by the email system);

  "Some mappings related to use of Text::Autoformat:
  
  "Using embedded perl:
  "-------------------
  if(has('perl'))
    command! -range=% Format :  
                      \  perl {
                      \          use Text::Autoformat;
                      \          my $n_line1 = VIM::Eval('<line1>');
                      \          my $n_line2 = VIM::Eval('<line2>');
                      \          my $foo= join "\n", $curbuf->Get($n_line1 .. 
$n_line2);
                      \          my $tw=VIM::Eval('&textwidth');
                      \          $tw or $tw = 72;
                      \          my $ft=VIM::Eval('&filetype');
                      \          $ft = ($ft eq 'mail') ? 1 : 0;
                      \          my $formatted = autoformat($foo, {right=>$tw, 
all=>1, mail=>$ft});
                      \          my @formatted_lines = split '\n', $formatted;
                      \          $curbuf->Delete($n_line1, $n_line2);
                      \          $curbuf->Append($n_line1-1, @formatted_lines, 
'');
                      \       }
    
    "Shortcut for formatting the paragraph around the cursor:
    nnoremap =p   {}:'`,Format<cr>
  
    "Format the stuff over the _previous_ motion:
    nnoremap <F4>  :'`,Format<cr>y<esc>
  
    "Format the visual selection:
    vnoremap <F4>  :Format<cr>
  endif
  
  "Using external (rather than embedded) perl:
  "------------------------------------------
  
  "Via the ={motion} command:
  nnoremap = :execute  'setlocal equalprg=perl\ 
                                    \-MText::Autoformat\ -e\ 
                                    \\"{autoformat{all=>1'.
                                    \',\ right=>'
                                    \.(&tw?&tw:72).
                                    \',\ mail=>'.
                                    \(&ft=='mail'?1:0).
                                    \'};}\"'
                                    \<cr>=
  
  "Shortcut for formatting the range .,$:
  nnoremap <F3>  :execute  '.,$!perl -MText::Autoformat -e 
             \                 "{autoformat{all=>1, right=>' . 
             \             (&tw ? &tw : 72) . 
             \             ', mail=>'.
             \             (&ft=='mail'?1:0).
             \             '};}"'<cr>
  

Reply via email to