Vim Tips Wiki
(Assign tip id + convert to TipNew template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=1562
 
|previous=1562
 
|next=1564
 
|next=1564
|created=May 7, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=Pressel
 
|author=Pressel
Line 13: Line 13:
 
Here is a convenience function to help Perl programmers when writing new routines.
 
Here is a convenience function to help Perl programmers when writing new routines.
   
Put the cursor on a word, then type <tt>\ns</tt> (assuming the default backslash leader key) to create a new subroutine. The subroutine is given the name of the word under the cursor, and is placed at the "bottom" of the file. If any <tt>__xxx__</tt> tokens are used (such as <tt>__DATA__</tt> or <tt>__END__</tt>), the subroutine is inserted before that token. Otherwise, it is appended to the end of the file.
+
Put the cursor on a word, then type <code>\ns</code> (assuming the default backslash leader key) to create a new subroutine. The subroutine is given the name of the word under the cursor, and is placed at the "bottom" of the file. If any <code>__xxx__</code> tokens are used (such as <code>__DATA__</code> or <code>__END__</code>), the subroutine is inserted before that token. Otherwise, it is appended to the end of the file.
   
 
<pre>
 
<pre>

Latest revision as of 06:33, 13 July 2012

Tip 1563 Printable Monobook Previous Next

created 2008 · complexity basic · author Pressel · version 7.0


Here is a convenience function to help Perl programmers when writing new routines.

Put the cursor on a word, then type \ns (assuming the default backslash leader key) to create a new subroutine. The subroutine is given the name of the word under the cursor, and is placed at the "bottom" of the file. If any __xxx__ tokens are used (such as __DATA__ or __END__), the subroutine is inserted before that token. Otherwise, it is appended to the end of the file.

nnoremap <Leader>ns :call Newsub()<CR>
function! Newsub()
  let word = "sub " . expand("<cword>") . "{}"
  let ln = search("__.*__", 'nW')
  if ln == 0
    call append('$', word)
  else
    call append(ln-1, word)
  endif
endfunction

Comments[]