Vim can fold Ruby code, but not comments.
After adding this in .vimrc to change foldmethod to comments, i can no longer fold code.
autocmd FileType ruby,eruby
\ set foldmethod=expr |
\ set foldexpr=getline(v:lnum)=~'^\\s*#'
How can i configure Vim to fold both comments and code?
In my recent Vim 7.3.823 snapshot, the $VIMRUNTIME/syntax/ruby.vim (version 2009 Dec 2) has both folding for Ruby constructs and comment blocks.
Just put
:let g:ruby_fold = 1
into ~/.vimrc. (And make sure you don't have a variable named ruby_no_comment_fold.)
You could use foldmethod=marker and add {{{ / }}} markers (or other markers of your choosing) to indicate where folds begin and end.
You could also modify the file which defines ruby syntax highlighting to adjust what it considers as eligible for folding with foldmethod=syntax.
A third option would be to develop a more complex routine for use with foldmethod=expr. For example, I use the vim functions defined here to define how ruby code should be folded. It automatically defines folds for modules, classes and methods along with any comment lines that immediately precede those; and it supports the standard fold markers for folding other sections. It gets used with foldexpr=ruby#MethodFold(v:lnum).
Further information on how fold expressions should behave can be found by doing :help fold-expr. There's also a nice vimcast about that.
Setting foldmethod to indent will fold lines based on indent level,
regardless of whether the line is a comment or code.
:set foldmethod=indent
:help fold-indent
I think you are looking for
set foldignore=#
If you want to fold block comments (like /* .... */ in multiple line), watch my other post in vi.stackechange
Related
I am trying to fold multiple lines in vim with :set foldmethod=marker I have done :set foldmarker={,} is there any way such that we can fold function that has starting braces='[' and end braces=']' and also fold a function with starting braces='{' and end braces='}' like in vscode we can fold any function with any starting and ending braces.
From :help folding:
There are six methods to select folds:
manual manually define folds
indent more indent means a higher fold level
expr specify an expression to define folds
syntax folds defined by syntax highlighting
diff folds for unchanged text
marker folds defined by markers in the text
marker won't help for the reason given under :help 'foldmarker':
The marker is a literal string (a regular expression would be too slow).
See :help fold-marker.
diff is irrelevant.
See :help fold-diff.
syntax may help, depending on the language and how the syntax script for that language was written.
See :help fold-syntax.
expr is the most powerful method because it lets you decide exactly what to fold and how to fold it.
It is also the hardest to set up, for obvious reasons. See :help fold-expr.
indent is a pretty dumb method, which makes it very memory efficient and versatile. You could try that one because it doesn't care about braces at all.
See :help fold-indent.
And then there is the manual method, which lets you define folds with motions.
This lets you do things like zfi{ or zf12j.
See :help fold-manual.
I'm having problems with auto indentation in vim while programming in lisp.
My .vimrc had the following settings for tabs:
set noexpandtab
set autoindent
set softtabstop=0
set shiftwidth=8
set tabstop=8
When I type (if <enter> in insert-mode, a new line is created with an indentation of two spaces.
None of my settings say anything about two spaces, so why don't I get a tab?
What setting can I use to change the indentation while in insert-mode?
Thanks in advance!
Update:
Thanks for the answers, the settings are not overwritten.
It has to do with the "default lisp indenting".
In the :help lisp it says something about changing the p flag in
cpoptions. This is what it says in the help for the cpoptions flags:
p - Vi compatible Lisp indenting. When not present, a slightly better algorithm is used.
Setting it does change the indent to one space instead of two spaces.
Still not sure how to change this to something else though.
Looks like this is two-space indentation is a hard coded behavior of :set lisp mode which ignores shiftwidth.
We can trace this to the source code which contains a hard-coded amount += 2; increment statement.
It's that way for a good reason; it has come to be the predominant way of writing Lisp.
As I wrote this answer, I peeked at samples of source code the following projects (all Lisp or Lisp-like languages):
Steel Bank Common Lisp (SBCL);
Clozure Common Lisp (CCL);
GNU Emacs;
Clojure;
GNU Guile;
Racket;
and GNU CLISP.
I did not spot a single example of anything but two-space indentation! With two-space indentation, you are in good/large company. You might as well get used to that; even if you somehow get Vim to follow your way, if you upstream anything into anyone's Lisp project, you will likely have to reformat.
Now, I have seen Lisp code using four-space indentation, like in very old code bases and some books.
By the way, sometimes you see if indented like this:
(if (condition)
(then)
(else))
This may happen where indentation is four spaces, but I'm referring situations when this is alignment, and not four space indentation. This formatting is, of course, standard for function arguments, but controversial for operators such as if. Some people like it that way in their code bases. For instance, this institution's randomly found coding style guide recommends this way of writing if, while also recommending two-space indentation.
Vim will do the above if you remove if from lispwords.
The :set lispwords=... parameter contains a comma-separated list of identifiers which follow operator-like indentation, meaning two spaces rather than function-like alignment: second and third lines align with argument. By default, if is found in lispwords.
Lisp mode also applies two space indentation to a function (i.e. form not listed in lispwords, if there is no argument):
(gobbledygook 1 2
2 3)
(gobbledygook
1)
That's also "canonical". Sometimes this comes in handy if you're trying to conform to some maximum line length; a function call running past the limit can sometimes be made to fit by moving all its arguments down, and just giving them two space indentation.
Each filetype can have its own settings in vim. So your .vimrc values can be overwritten in filetype plugins. To learn the current values of the settings for lisp filetype open the lisp file and run the following commands in vim command line:
:set noexpandtab?
:set autoindent?
:set softtabstop?
:set shiftwidth?
:set tabstop?
If they are different from the ones in .vimrc, then your values are overwritten in some plugin.
In order to overwrite them again with your custom values, create the ~/.vim/after/ftplugin/lisp.vim file with the required values:
set noexpandtab
set autoindent
set softtabstop=0
set shiftwidth=8
set tabstop=8
I would like to know how to use vim modelines in a Markdown document. Is it possible, or does modelines only recognise certain comment markers?
I have tried using this as the first line of my file:
<!-- vim: set ft=markdown -->
I also tried all these suggestions here with no luck.
Your modeline syntax is off. Add a colon at the end, and it will work:
<!-- vim: set ft=markdown: -->
Modeline doesn't care about comment markers. There are two different modeline formats:
[text]{white}{vi:|vim:|ex:}[white]{options}
This form does not use the keyword set, does not require a final colon, and most importantly, does not allow random text after options (such as -->). This means, any paired comment markers must necessarily use the second form:
[text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text]
This form requires the use of set keyword, requires terminating options with a colon (:), and allows the terminating colon to be followed by random text.
I apply some markdown related settings according to the autocmd + filetype.
In my vimrc I have:
if has("autocmd")
autocmd FileType markdown vmap <Leader><Bslash> :EasyAlign*<Bar><Enter>
endif
Not sure that it's the most traditional way.
How can i do that gvim will auto tab back when I write the word 'end'
(like it does when Im write '}')
I add this rows to gvimrc
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
Thanks.
You can use a plugin like vim-endwise to achieve this. This plugin will de-indent the current line when you type end accordingly. Not only that, if for example you are using Ruby, then the plugin will insert end appropriately, whenever you are starting a method or a condition and so on, like def, if, etc.
More Information: https://github.com/tpope/vim-endwise
If you are writing in one of the languages that vim recognizes, then chances are good that someone has already written an indentation plugin. Try
:filetype indent on
:e foo.???
:help :filetype-indent-on
OK, so first of all: don't use smartindent. Use filetype-specific indent rules. Vim has built-in support for many file types, or you can define your own using 'indentexpr', see :help 30.3 and :help indent-expression.
When using the 'indentexpr' option, another option called 'indentkeys' defines when Vim will automatically adjust the indent.
I have tried the usual approaches, and have read :help tex.vim
(see : http://vimdoc.sourceforge.net/htmldoc/syntax.html )
I've taken a brief look at syntax/tex.vim, but can't see how to disable it without rebuilding vim without folding. I'm sick of hitting 'zE'.
Lines I've tried in my .vimrc:
set foldlevel=manual
set foldlevelstart=99
let g:tex_fold_enabled=0
Just noticed that there are variables to control folding in vim-latex-suite, at least as of v1.6 of the plugin. The functionality is documented here:
http://vim-latex.sourceforge.net/documentation/latex-suite.html#latex-folding
In short you should be able to change three global variables to get rid of all folding:
:let Tex_FoldedSections=""
:let Tex_FoldedEnvironments=""
:let Tex_FoldedMisc=""
That should get rid of all folding. If you want to disable some folding but not all then you can control things by setting the appropriate values for each variable, as described in the documentation link above. Hope that helps.
What about
autocmd Filetype tex setlocal nofoldenable
The folding functionality all seems to located in folding.vim file of latex-suite distribution. This file is referenced in line 825 of my main.vim file in the latex-suite folder of the ftplugin folder. That line reads:
exe 'source '.fnameescape(s:path.'/folding.vim')
Comment out that line and, as far as I can tell, it strips out all the folding in latex-suite plugin. I don't think it affects anything else, but I haven't checked.