1

Most source files I edit have about 40 lines of boilerplate (license, etc) at the start of the file. This is annoying me, because I have to scroll past it every time I load a file.

It seems like it wouldn't be too hard to make an editor automatically skip up to the first non-comment part of a file when it loads. So: are there scripts or plugins for doing this with popular editors? In the first instance I'm interested in vim and emacs, but any others would be interesting too.

j0k
  • 22,600
  • 28
  • 79
  • 90
kdt
  • 27,905
  • 33
  • 92
  • 139
  • I think this is answered for vim in this answer: http://stackoverflow.com/questions/162617/how-can-i-automatically-add-some-skeleton-code-when-creating-a-new-file-with-vim/162671#162671 – Brian Riehman Dec 09 '10 at 19:16
  • @Brian -- no, that question talks about how to automatically insert boilerplate. This one is about how to skip past it. – kdt Dec 09 '10 at 21:33

3 Answers3

3

For GNU/Emacs, try putting the following code into your .emacs file:

(defun skip-file-initial-comment ()
  (interactive)
  (goto-char (point-min))
  (while (looking-at (concat "\\s *" comment-start-skip))
    (forward-comment 1))
  (unless (= 0 (current-column))
    (beginning-of-line 2))
  (recenter 0))

(add-hook 'find-file-hook 'skip-file-initial-comment)
Thomas
  • 17,016
  • 4
  • 46
  • 70
1

This is not a plugin solution but it might help you nonetheless.

If you use { or } in normal mode in Vim it goes up or down one paragraph, i.e. it jumps to the next empty line.

So basically if you open a file with a big license text, most of the time it is considered as a single paragraph, so just typing } once should be enough to move to the interesting part of the code.

If you think } is too cumbersome to type, do not hesitate to remap it to a shortcut you are comfortable with.

It might not be the best solution for this specific case but it is handy command to scroll quickly in files.

Xavier T.
  • 40,509
  • 10
  • 68
  • 97
1

.vimrc: set foldmethod=marker

files you're editing:

# {{{ Boilerplate
# stuff here is version blah blah and with more copyrights than you can ... blah blah
# }}}

Replace the # with whatever comment character(s) you've got in your programming language...

e.g. for C

/* {{{ Boilerplate stuff
 * stuff here is version blah blah and with more copyrights than you can ... blah blah
*/ }}}

OR

// {{{ Boilerplate stuff
// stuff here is version blah blah and with more copyrights than you can ... blah blah
// }}}

The key is the {{{ and }}} to "fold" sections of your code... you can hide this, and it'll appear as "Boilerplate stuff" or whatever comes after the opening "#{{{" brackets.

once a "fold" is closed you can open it by using "zo", and close it by "zc". In VIM fashion, there are quite a few other options to folding, and you can check it out yourself in more details at: http://vim.wikia.com/wiki/Folding .

Chris
  • 11
  • 1