How to fold all code between {{{ and }}} in vim - 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.

Related

how to activate vim folding markers?

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

Search vim help for subject under cursor

While working with some Vim scripts, I found myself typing
:help {subject}
quite a bit. I want CTRL+] (jump to definition of keyword under cursor) functionality, but instead of running :tag {ident} I want it to do :help {subject}, where {subject} is the word under the cursor.
Just press K. If you have set a global 'keywordprg', you need to unset it (or set it to the special :help value) in ~/.vim/after/ftplugin/vim.vim:
:setlocal keywordprg=:help
The simplest solution is
nnoremap K :help <C-r><C-w><CR>
To check the documentation of the keyword under your cursor, you can press Ctrl+] to go to its documentation.
If you have enable mouse support in nvim with the following options:
set mouse=a
you can double click the keyword to go to its documentation.
By the way, to go back to previous position in the help file, press Ctrl+O or Ctrl+T.
The above is also true for Neovim.
References
https://vimhelp.org/

vim how to set good indet without syntax highlighting?

I have mapped F8 to :set syntax=off<cr>.
I like editing without syntax highlighting, but if I switch syntax off, my indent rules also switched off. How can I have good indent without syntax highlighting?
If vim autoindent is ok for you , you can
set autoindent
after you "syntax off".
On the other hand, if you use your own index function. Try to run following command, which may help.
set indentexpr=<your function>
Or check the indent function by:
set indentexpr?
and reset it to the result after "syntax off"
What you want is to map that key to
:syntax off
Note that syntax is a command, and off an argument. You were setting the syntax option to the type off and consequentially affecting the indent rules.
Check :h :syntax and :h 'syntax' to read more.

Preventing vim to auto-expand folds

I'm using foldmethod=marker and #{{{ #}}} markers in Python code.
After typing #{{{ vim automatically expands all folds below the cursor.
Is it possible to turn this off?
Try removing block from the "foldopen" option.
:set foldopen-=block
Or maybe the foldclose=all option...
:set foldclose=all
Try adding a match for the # {{{ like # {{{1 and then a # }}}1

Is there any command to toggle enable auto text wrapping?

By default, I think my vimrc setttings enable the auto-wrapping. However, sometimes, I would like to see text lines are not wrapped. Is there any command I toggle the text line wrapping enabled and disabled? This would avoid me to exit and to change settings.
I think what you want is:
:set wrap!
This will toggle line wrapping.
More about using ! (bang) to alter commands can be found at:
:help :_!
In your vimrc, create a function such as this:
:function ToggleWrap()
: if (&wrap == 1)
: set nowrap
: else
: set wrap
: endif
:endfunction
Then map a key (such as F9) to call this function, like so:
map <F9> :call ToggleWrap()<CR>
map! <F9> ^[:call ToggleWrap()<CR>
Whenever you press F9, it should toggle your wrapping on and off.
:set nowrap
There is also the linebreak option that controls whether wrapped text is broken at word boundaries or not.
Add the following to have CTRL+W toggle wrapping. You can change it to some other key if you don't want w to be it.
map <C-w> :set wrap!<CR>
The quickref suggests (no)wrap
I happen to like tpopeā€™s unimpaired plugin, where yow will toggle wrap settings.
For those who want to change the text instead of just visual effect, for example in git commit, just press qt in a roll and press enter. This will properly wrap the current paragraph your cursor is in. The paragraph is only delimited by blank lines. or you can select some area to press qt.
I found this by total accident.

Resources