how to activate vim folding markers? - vim

I have inherited some c++ code with vim-based folding markers like this:
// CONSTRUCTORS/DESTRUCTORS /*{{{*/
Foo::Foo()
{
}
Foo::~Foo()
{
}
/*}}}*/
What do I need to put into my .vimrc to enable folding toggles like zm and space-bar?
With my current settings, when I hit space bar inside or zm, vim does nothing.

The default keybindings for folding are za or zm (though zm I think only closes folds, while za toggles them), so you should add the following lines to your .vimrc:
set foldmethod=marker to enable folding triggered by markers (the {{{ things in your code)
nnoremap <space> za to enable space to trigger the fold in normal mode.
But! if you're not sure if you want to enable folding in other files, you could use autocmds, like so:
autocmd FileType vim,c++,txt setlocal foldmethod=markerand that will ensure that folding only works in vim, c++, and text files.
By the way, what you've posted is only one kind of folding mentioned by vim guru Steve Losh in this article. Read it to learn more about folding. It's super cool.

If you only have a few files or if you just wish to control option(s) on a per file basis you may want to use modeline. My intro to folding came when I download a z-shell script and when I opened it, was surprised to find everything folded. Found something like this at the end of the file:
# vim:ts=4:sw=4:ai:foldmethod=marker:foldlevel=0:
Change commenting to match your code type, and insure there is a space before the word vim. As always a good place to start: :help modeline and :help folding. You may have to add set modeline to your .vimrc file if modeline was not set at build time.

OK, after googling around for a bit, I found this which seems to work:
set foldmethod=marker
nnoremap <space> za

Related

How can I have *both* Markdown header folding and indent folding in Vim

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.

Vim plugin "auto-pairs" change automatic indent size?

I'm not sure if this is the right place to ask about this, but I figured it couldn't hurt to ask here. I am using a plugin called auto-close so that I don't have to close my own parentheses. It has a very nice feature that does the following:
This is a great feature, but I don't like how far it indents for me.
I have the following line in my .vimrc:
" for filetype "js", tab = insert 4 spaces, backspace will delete all 4
autocmd Filetype javascript setlocal expandtab softtabstop=4
In editing a javascript file, it automatically did an 8-space indentation instead of a 4-space indenation, as I've specified in my .vimrc. Can anybody help me figure out how I can make it automatically indent 4-space tabs instead of 8-space tabs? I can't find it in the documentation either. Thanks!
If you get shiftwidth=8, softtabstop=0, tabstop=8, that means that your autocmd FileType didn't take effect. You'd have to troubleshoot that.
I would recommend putting any settings, mappings, and filetype-specific autocmds into ~/.vim/ftplugin/{filetype}_whatever.vim (or {filetype}/whatever.vim; cp. :help ftplugin-name) instead of defining lots of :autocmd FileType {filetype}; it's cleaner and scales better; requires that you have :filetype plugin on, though. Settings that override stuff in default filetype plugins should go into ~/.vim/after/ftplugin/{filetype}.vim instead. The change of indent settings would fit the latter, after directory location.

How to fold all code between {{{ and }}} in vim

I have a lot of code like this
#{{{
code here
#}}}
#{{{
more code here
#}}}
etc...
I want to fold all of them instead of going to each #{{{ line and type zfa{.
I read the vim documentation and tried typing set foldmethod={{{ but I got the error E474: Invalid argument: foldmethod={{{ .
What do I press on my keyboard to tell vim to fold all code between #{{{ and #}}}?
And what do I press to expand them all again?
The foldmethod option is not for setting the fold marker.
You can set up fold markers by doing something like this:
set foldmethod=marker
set foldmarker={{{,}}}
As for vim's folding shortcuts, this article on the vim tips wiki is very helpful.
http://vim.wikia.com/wiki/Folding
For more help on folding, you can do :help folding inside of vim.
For help on the keyboard shortcuts for folding, do :help fold-commands
In your vimrc add the option set foldmethod=marker. Then to fold you type zm and to unfold type zr.

Is there anyway to dynamically enable / disable the highlighting in Vim?

Ok so basically the way Vim highlights searches displeases me. Basically you do a search, then you have to type /asdf or have a shortcut like this in your vimrc:
nn <silent> <leader><space> :noh<CR>
Which is what I have. But it's still too much mental work. Basically, when I do a search, I want highlighting to enable (like it does now) but if I do anything other than cycle through the searches (with n/N) then I want highlighting to turn off. That's basically my workflow, so I'm wondering if I can automate it. Also if I search, do something other than n/N (which should turn highlighting off) and then press n/N again, it should re-enable.
Any ideas?
That's difficult. One idea is
:autocmd CursorHold * call feedkeys(":noh\<CR>")
(One needs to use feedkeys() because :nohlsearch is ineffective in functions and autocmds.) This clears the highlighting whenever you pause the cursor for some seconds. You can add other triggers like InsertEnter or CursorHoldI.
What does not work is CursorMoved, because the searches and n / N jump as well. You would need to overload those commands, store the cursor position after the jump, and modify the autocmd to only clear the highlighting when the position is different.
What I do: I have Enter mapped to :nohlsearch; it's quick and easy to reach.
you can turn it on or off with:
:set hls
or
:set nohls
I have F7 mapped to set hls!:
noremap <F7> :set hls!<CR>

How to set the default to unfolded when you open a file?

In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?
set foldlevel=99
should open all folds, regardless of method used for folding. With foldlevel=0 all folded, foldlevel=1 only somes, ... higher numbers will close fewer folds.
You can put this in your .vimrc:
au BufRead * normal zR
It declares an automatic command (au), triggered when a buffer is read (BufRead), matching all files (*) and executes the zR (opens all folds) command in normal mode.
set nofoldenable
Adding this to your .vimrc will temporarily disable folding when you open the file, but folds can still be restored with zc
In .vimrc add an autocmd for BufWinEnter to open all folds automatically like this:
autocmd BufWinEnter * silent! :%foldopen!
That tell vim to execute the silent :%foldopen! after opening BunWinEnter event (see :h BufWinEnter). The silent %foldopen! will execute foldopen on the whole buffer thanks to the % and will open all folds recursively because of the !. Any eventual error message will be suppressed by silent. (You could get error messages like E490: No fold found if the buffer actually didn't contain any fold yet)
Note: You could use BufRead instead of BufWinEnter but then if the file has a modeline that enables the folding that will override this autocmd. I mean BufRead autocmds run before the modeline is processed and BufWinEnter will run them after. I find the later to be more useful
You can add
set foldlevelstart=99
to your .vimrc file, and it will start editing any new file with all folds open.
If you want a way to have it display unfolded as soon as it is opened, you can use set foldlevelstart=99 as a lot of answers explained.
But, if you just want to see them unfolded, you can just press zi and it will unfold everything. Another, zi will close them back.
You could map it to keys to enable it.
For example,
nmap ,f :set foldmethod=syntax<CR>
Then while in normal mode hit the ",f" key combination
You can open unfolded file when you put set nofoldenable into your .vimrc file.
autocmd BufReadPost * silent! :%foldopen!
This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.
The set foldenable method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.

Resources