I have tried using the foldmethod = syntax but then if folds parentheses of functions and almost anything else but not multi-line comments. There is no foldlevel that does what I want. I want it to fold only multi-line comments. Unfortunately this function doesn't work anymore.
I also tried doing it as for single-line comments, but then it folds everything, althoug I have exaped the asterisk.
autocmd FileType cpp setlocal foldmethod=expr foldexpr=getline(v:lnum)=~'^\\s*//'
autocmd FileType cpp setlocal foldmethod=expr foldexpr=getline(v:lnum)=~'^\\s*/\*'
Normal regular expressions like this also don't work
/\\*.*?\\*/
If you want to only fold multi-line comments, then the following should be enough:
" in after/ftplugin/cpp.vim
setlocal foldmethod=marker
setlocal foldmarker=/*,*/
See :help 'foldmethod' and :help 'foldmarker'.
Related
At my work, I am required to follow the house style for indentation, which goes as follows:
2 spaces when coding html and ruby
tabs when coding javascript, with tabwidth=4 recommended
What is the best way to specify different whitespace preferences per filetype?
there are many ways, but here's a simple, easy to understand way. add these lines to your ~/.vimrc:
autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=4 sts=4 sw=4
Peter's answer is straightforward enough, but unfortunately the options aren't right. You need to use the following options instead:
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 noexpandtab
Also note:
You can make vim show tab characters by using :set list.
Once you have the tab/space options set correctly, you can make vim repair the file (replace spaces with tabs or vice versa) using the :retab! command.
+1 to Peter's answer, but Vim provides another solution as well. If you want to do something more complicated than a single setlocal, like setting up a whole bunch of options, commands, and mappings at once, then vim's filetype plugin feature comes to the rescue.
You need to have filetype plugin on or filetype plugin indent on in your .vimrc, and then to create a plugin for e.g. ruby you can create ~/.vim/ftplugin/ruby.vim. Technically you can use any commands you like in here, to be run when a Ruby file is loaded, but the recommended ones include setlocal, map <buffer>, command -buffer, and defining functions. Lots more information is in the User Guide; if you're pretty familiar with scripting vim then jump to :help 41.11, otherwise read :help usr_40 and :help usr_41.
There's also a nice vim script: DetectIndent which tries to detect the indentation of a file that you open.
It's very handy if you work with many files with different coding style.
I use an autocommand in my .vimrc:
:autocmd BufReadPost * :DetectIndent
To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
:set expandtab
Next step is to control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 2 space for a tab, use:
:set tabstop=2
ref: http://vim.wikia.com/wiki/Converting_tabs_to_spaces
Vim now has built-in Markdown folding, yay!
I want to fold on Markdown headers, and within headers, fold on indents also.
How can I have both Markdown header folding + indent fold method at the same time?
There can only be one of Vim's standard fold methods used within an individual window, but what you ask should still be possible. Here are a few options that I can think of:
You can use the expr method option and create your own custom rules with that. This is probably the only "real" way to get what you want, but it's the most complicated option.
You could also use two windows in a tab, each pointing at the same buffer, and set different fold methods locally for each split.
Finally, you could always hack something together with autocommands. From the Vim wiki page on folding, about having both indent and manual folding:
augroup vimrc
au BufReadPre * setlocal foldmethod=indent
au BufWinEnter * if &fdm == 'indent' | setlocal foldmethod=manual | endif
augroup END
This hack takes advantage of modelines, and Vim's behavior for setting variables before vs. after the modeline is read. Note that you have to have Vim's nocompatible option set for this sort of trick to work.
I'm new to Vim and trying to set my text width for automatic wrapping. Having worked through the tutorial and read the help documentation, I don't understand why the command :set tw=78 isn't doing the trick. My .vimrc file contains the statement:
autocmd FileType text setlocal textwidth=78
, which also has not resulted in wrapped text. Thanks for your patience with a beginner.
You have to format the text after assigning a value to textwidth, use gggqG: gg to set cursor at the beginning of the file, gq to start formatting and G to format until end of file. It is a normal instruction, so I added it before the command.
autocmd FileType text setlocal textwidth=78 | normal gggqG
Setting 'textwidth' isn't enough. You also need to add t to 'formatoptions' (see :help 'formatoptions'). From :help 'fo-table':
letter meaning when present in 'formatoptions'
t Auto-wrap text using textwidth
...
So you may want to use this autocommand:
autocmd FileType text setlocal textwidth=78 formatoptions+=t
Whenever I want to open a new line in Vim with o, it automatically indents (instead of starting at the beginning of the line). Why is that? How can I fix this?
(I don't want to switch off auto-indent, which is great for other file types.)
UPDATE:
It seems to have something to do with the actual text: auto-indenting (=) the following two lines indents the second line (why? -- I would like both lines to start in column 1!)
*in golf: failing to make par is a loss, missing a birdie putt is a foregone gain, not a loss
*negotiations, especially renegotiations: concessions you make cause you much more pain
UPDATE 2 (my .vimrc):
:set cpoptions+=$
:set virtualedit=all
:filetype plugin indent on
:set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
:set fdm=expr
:set gfn=Ubuntu\ Mono\ 11
setlocal autoindent
setlocal cindent
setlocal cinwords=if,else,elseif,do,while,foreach,for,case,default,function,class,interface,abstract,private,public,protected,final
setlocal cinkeys=0{,0},0),!^F,o,O,e
setlocal nosmartindent " don't use smart indent option
You can set options to take effect only for specific files. For example, I have the following in my vimrc:
if has("autocmd")
augroup LISP
au!
au BufReadPost *.cl :set autoindent
augroup END
augroup C
au!
autocmd BufNewFile,BufRead *.cpp set formatprg=c:\\AStyle\\bin\\AStyle.exe\ -A4Sm0pHUk3s4
augroup END
endif
Using this, I can turn on autoindent, or file formatting, or whatever for the files where it makes sense, but not have it on generally, when it might annoy me in other cases. In this case, I turn on autoindent for .cl files, but not necessarily for others.
You could also, in theory, use the same thing to turn off autoindent for .txt files.
At my work, I am required to follow the house style for indentation, which goes as follows:
2 spaces when coding html and ruby
tabs when coding javascript, with tabwidth=4 recommended
What is the best way to specify different whitespace preferences per filetype?
there are many ways, but here's a simple, easy to understand way. add these lines to your ~/.vimrc:
autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=4 sts=4 sw=4
Peter's answer is straightforward enough, but unfortunately the options aren't right. You need to use the following options instead:
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 noexpandtab
Also note:
You can make vim show tab characters by using :set list.
Once you have the tab/space options set correctly, you can make vim repair the file (replace spaces with tabs or vice versa) using the :retab! command.
+1 to Peter's answer, but Vim provides another solution as well. If you want to do something more complicated than a single setlocal, like setting up a whole bunch of options, commands, and mappings at once, then vim's filetype plugin feature comes to the rescue.
You need to have filetype plugin on or filetype plugin indent on in your .vimrc, and then to create a plugin for e.g. ruby you can create ~/.vim/ftplugin/ruby.vim. Technically you can use any commands you like in here, to be run when a Ruby file is loaded, but the recommended ones include setlocal, map <buffer>, command -buffer, and defining functions. Lots more information is in the User Guide; if you're pretty familiar with scripting vim then jump to :help 41.11, otherwise read :help usr_40 and :help usr_41.
There's also a nice vim script: DetectIndent which tries to detect the indentation of a file that you open.
It's very handy if you work with many files with different coding style.
I use an autocommand in my .vimrc:
:autocmd BufReadPost * :DetectIndent
To insert space characters whenever the tab key is pressed, set the 'expandtab' option:
:set expandtab
Next step is to control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 2 space for a tab, use:
:set tabstop=2
ref: http://vim.wikia.com/wiki/Converting_tabs_to_spaces