LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 07-22-2006, 06:27 PM   #1
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Rep: Reputation: 30
Vim plugin for hiding block comments


I am looking for a vim plugin that could hide or shrink block comments. For example, a block comment would be something like this:

/* ------------------------------------------------------------------------------------------------
Function: void print(string s)
Descript: prints input string message to the standard output and if logging is enabled, writes the messages to the log file
Programmer: John Doe
Version: 0.0.1
Called By: int main(int argc, char* argv[])
Calls To:
Input: standard 128-key keyboard
Output: writes to similarity vector, color-frequencies array
Parameters:
Returns: true if there were no problems processing the images, false if there was a problem with the image dimensions.

0.0.1 06/14/2006 C.D.
Wrote the function
------------------------------------------------------------------------------------------------ */
void print(string s)
{
// code
}

I would like the plugin to make it look something like this:
/* ------------------------------------------------------------------------------------------------
void print(string s)
{

This feature is available in Kdevelop and some other text editors that will shrink your blocks of code/comments. It's really quite handy. I often have to use vim to program on the console and this would be very useful to have available.

Does anyone know of such a vim plugin?
 
Old 07-22-2006, 07:18 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
You don't need a plug-in, the feature is built-in. It's called folding, and folks have examples for folding comment blocks here.
 
Old 07-24-2006, 07:12 PM   #3
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by macemoneta
You don't need a plug-in, the feature is built-in. It's called folding, and folks have examples for folding comment blocks here.
Wow, thanks a lot of the information. I have got the folding working very well. I have just one more question.

Currenty, I can fold anything I have my cursor over and cause all folds to re-open in the document. Is there a way to everything in the document with standard comment patterns and { } fold automatically with a key command?
 
Old 07-24-2006, 10:12 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
You can map the folding to an unused key, using the ':map' command. You can, of course, predefine the commands in your vim profile (~/.vimrc and ~/.gvimrc).
 
Old 07-24-2006, 10:28 PM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
Here's another way...

:set foldmethod=marker
:set foldmarker=/*,*/

Now all block comments are folded, automatically.
 
Old 07-27-2006, 02:15 PM   #6
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by macemoneta
Here's another way...

:set foldmethod=marker
:set foldmarker=/*,*/

Now all block comments are folded, automatically.
That is great, but is there a way to bind that to a key and not have it happen on startup? Also, can it be done for braces '{,}'?
 
Old 07-27-2006, 02:34 PM   #7
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 326Reputation: 326Reputation: 326Reputation: 326
Replace the open/close comment markers with braces in the foldermarker to have it work for them. You can use the foldexpr to define multiple folds.

You can bind a key to the 'zi' command, to alternately open close all folds. For example:

:map q zi

Google on "vim fold" will find many examples and creative uses.
 
Old 07-27-2006, 04:26 PM   #8
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Original Poster
Rep: Reputation: 30
Thanks again for the help. Sorry to keep bothering you but I still cannot get the last bit of functionality that I want.

So, thus far I have got vim to fold either fold all comments on startup, but not allow me to manually unfold or fold things individually (though I can toggle on/off for all folds). Here is my current setup if I want to have everything folded automatically:

:set foldmethod=marker
:set foldmarker=/*,*/
:map f v%zf " manually close a fold
:map F zD " manually open a fold
:map <F3> zi " toggle open/close folds
:map <F4> zn " open all folds

Now with these settings, I cannot manually fold individual things but not cause everything in the document to fold on key command. This is what I would like to do...

:set foldmethod=marker
:set foldmarker=/*,*/
:map f v%zf " manually close a fold
:map F zD " manually open a fold
:map <F3> zi " toggle open/close folds
:map <F4> zn " open all folds
 
Old 03-30-2014, 11:44 AM   #9
defaultuser
LQ Newbie
 
Registered: Mar 2014
Posts: 1

Rep: Reputation: Disabled
tiny step to folding

Hi,

I've been trying to do the same thing, you can switch the foldmethod to manual after having folded up all comments as you now know how to by doing

:set foldmethod=manual

In manual fold mode you can open close all the folds you like as documented in the vim folds help page.
vimdoc. sourceforge. net/htmldoc/ fold. html#fold-commands
// the forum is preventing me from posting entire urls for fear of spam

I'm still trying out how to combine the automatical creation of comment folds with that of folds for functions.
 
Old 03-31-2014, 05:54 PM   #10
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 249

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by defaultuser View Post
Hi,

I've been trying to do the same thing, you can switch the foldmethod to manual after having folded up all comments as you now know how to by doing

:set foldmethod=manual

In manual fold mode you can open close all the folds you like as documented in the vim folds help page.
vimdoc. sourceforge. net/htmldoc/ fold. html#fold-commands
// the forum is preventing me from posting entire urls for fear of spam

I'm still trying out how to combine the automatical creation of comment folds with that of folds for functions.
I wrote these Vim functions years ago and stuck them in my vimrc. They are bound to keys and work as follows:

F10 toggles folding and closing all possible code regions. It uses the markers '{,}'. (This won't work for a language like Python for example, but will work for many others.)

I have also bound the Space key or the 't' key in Navigation mode of Vim to toggle folding and unfolding where the cursor is. Finally I have also bound the 'T' key (that's shift+t) in Navigation mode to unfold levels of folding incrementally at the current cursor position.

Code:
"-----------------------------------------------------------------------------
" Folding function & folding key bindings
"-----------------------------------------------------------------------------

" Fold automatically
nnoremap <silent> <F10> :call DoFoldComments() <CR>
function! DoFoldComments()
  " NOTE: There will be folding problems if you have a class definition and
  " non-class functions in the same file
  set foldmethod=marker
  set foldmarker={,}
  if &foldlevel > 1
    if search("^class ") > 0 || search("^public class ") > 0 || search("^namespace ") > 0
      set foldlevel=1
    else
      set foldlevel=0
    endif 
    set foldenable
    echo "Folding comments"
  else
    set foldlevel=99
    echo "Unfolding comments"
  endif
endfunction

" Fold manually - toggle manually close a fold
noremap <silent> <space> :call DoToggleFold()<CR>
noremap <silent> t :call DoToggleFold()<CR>
function! DoToggleFold() 
  let s:line = getline('.')
  if s:line =~ "{" || s:line =~ "}" || s:line =~ "/*" || s:line =~ "*/"
    set foldmethod=manual
    if foldlevel('.') == 0 "Do nothing, just avoid causing a problem
    elseif foldclosed('.') < 0
      . foldclose
    else
      . foldopen
    endif
  elseif foldlevel('.') == 0
    normal! l
  else
    if foldclosed('.') < 0
      . foldclose
    else
      . foldopen
    endif
  endif
endfunction

" Unfurl/Open a folded code portion
noremap <silent> T :call OpenFold()<CR>
function! OpenFold()
  if getline('.') =~ '^sub\s*' && foldlevel(line('.')+1) > 0
    . foldopen
    normal! j
  elseif foldlevel(line('.')) > 0
    normal v%zo
  endif
endfunction
Dunno if that helps for automatic folding (with key binding).
 
  


Reply

Tags
block, code, comment, plugin, vim


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"HOWTO Change Colour of Comments in VIM editor" manikantha Linux - General 8 07-16-2013 03:36 AM
LXer: Cream for Vim - Making Vim more user friendly LXer Syndicated Linux News 0 06-17-2006 11:54 PM
VIM insert before a visual block... pld Linux - Software 0 06-22-2005 11:36 AM
vim - comments formatting alexLinux Programming 0 12-24-2003 04:22 PM
vim plugin - taglist doesn't work correctly milod Programming 0 07-19-2003 08:13 AM


All times are GMT -5. The time now is 08:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration