开发者

Scripts/plugins for skipping source file boilerplate in popular editors?

开发者 https://www.devze.com 2023-01-30 06:03 出处:网络
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.

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.


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)


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.


.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 .

0

精彩评论

暂无评论...
验证码 换一张
取 消