Vim Tips Wiki

Create new subroutines

1,624pages on
this wiki
Add New Page
Talk0 Share
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)
    call append(ln-1, word)


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki