Reading vim plugin - strange notation and navigation - vim

Im reading great tpope rails.vim and what does it mean:
" }}}1
" Abbreviations {{{1
exactly here: https://github.com/tpope/vim-rails/blob/master/autoload/rails.vim#L3921
Is it for better navigation?
This file is quite huge, how to navigate on it properly - using ctags?

These are so called foldmarkers. Vim 6 introduced code folding and the triple braces are the default string to mark the beginning and the end of a fold. In addition, if you prepend the opening mark {{{ with text, it'll show in the collapsed line as a header. This is only one way to fold code. Being a manual method, it is easily controlled and thus preferred by many.
See :h folding and :h fold-marker.

Related

Vim folding breaks colorscheme [duplicate]

I'm using vim for LaTeX and I'm using latex-suite. It gives me nice syntax highlighting and folding, but in large files syntax highlighting gets "confused". If I open all folds, the syntax highlighting turns OK. I would like it to "just work" all the time though.
I seem to recall an option that would increase the number of lines that is used as basis for determining syntax highlighting but I cant find it.
I don't edit LaTeX, but perhaps you want ":syn sync fromstart"? Just be warned that this can significantly slow down Vim since it forces Vim to do syntax highlighting parsing for the whole file rather than a section of the file. See ::help :syn-sync".
Ctrl+L in normal mode forces a redraw and often fixes syntax colour problems.
zRzMzx (i.e., expand all folds, contract all folds, fold to show current line) sometimes fixes syntax highlighting problems related to folds
10 years later, this is still somehow an issue. Similarly as Jeromy, I suggest pressing zRzMzzza which stands for
open all folds
close all folds
open (toggle) the fold I'm on
center buffer on this line
It looks like we need to learn to live with this

How to make vim with tree representation for indents, similliar to result of terminals "tree" command?

I'm looking for some way to connect indets into tree structure similliar to the one used by terminal tree command.
Something that interprets the indents in same way tree interprets file system.
Alternatively for sublime text or another text editor.
Edit: Apologies for the broadness of question, to specify what i wanna do is>
Rather then replacing the actuall text i just want it to interpet the indents into the tree structure while retaining the actuall file should retain it's indents.
You've asked a broad question (and did not show any research effort so far), so I all I can do is answering it broadly as well (for Vim):
permanent change
For a permanent change of the actual text, all you need is :substitute. A start would be
:%substitute/ \ze\S/└── /
To make this more beautiful, another pass could turn └ into ├ by comparing previous and current line; :substitute or :global can do this.
just visualization
If you don't want to actually manipulate the buffer contents, but just affect the visual appearance, :set list and the 'listchars' option come to mind. Unfortunately, though this can display spaces and tabs, it does so uniformly; i.e. you cannot just apply it to the "last" part of the indent. You have a chance to implement this with :help conceal; this can translate a (sequence of) character(s) to a single (different) character. This is based on syntax highlighting. You could define matches for fourth last space before non-whitespace and conceal that as └, and third and second last space before non-whitespace and conceal as ─, for example.
or a hybrid
Another approach would be a combination: Use (easier) modification with :substitute, but undo this before writing (with :autocmd hooks into the BufWritePre and BufWritePost events). If this is purely for viewing, you could also simply :setlocal nomodifiable or :setlocal buftype=nowrite to disallow editing / saving.

Display some special character as a linebreak in Vim

I would like to display some (arbitrary) special character as linebreak <CR> in vim.
So far I tried misusing (certainly extreme misuse:) the non-breakable space typing
:set list listchars=nbsp:<CR>
which does not work, seemingly because the command does not accept the <CR>.
Is there anything which I can use here for <CR>? \r didn't work either.
Note that I don't want to edit the text file. The goal is to have blocks of lines (some related code) treated as a single line with respect to vim actions but displayed as multiple lines. The special character (to be defined) would be used only to save this block structure in the file replacing the linebreak \r in these cases.
Given the wider context of the problem that you have provided in a
later comment, I would suggest the following solution. Group dependent
lines of code in folds by indentation, language’s syntax, or markers.
All of these three methods are automatic and do not require manual
creation of folds. Which one to choose depends on the language you
use. See :help foldmethod, and feel free to comment this answer if
you need any help with folding.
Unless the syntax of the language you use has extensive support in
Vim, the most convenient methods would be using fold markers or
defining a custom expression to calculate fold level of each line.
The former method implies surrounding every group of lines to fold
with special text markers (which could be enclosed in a comment not
to break the syntax rules of the language). By default, those markers
are {{{ and }}}; see :help fold-marker and :help foldmarker
to find out how to change them. Use
:set foldmethod=marker
to enable this mode of folding.
Defining an expression to calculate fold level for every line is an
even more flexible method. It allows to use any logic (that can be
expressed in Vimscript) to determine the fold level. For example, to
fold groups of lines that start with a single space use the following
settings:
:set foldmethod=expr
:set foldexpr=getline(v:lnum)[0]=='\ '
See :help fold-expr for further details on customizing the fold
expression.
When the lines that depend on each other are grouped into folds, you
can easily pass the contents of any particular fold to a filter
program. Move the cursor to a line inside a target fold, then type
[zV]z to select the entire fold, followed by !, and enter the
command to run. To save typing, you can define the mapping
:nnoremap <leader>z [zV]z!
If the command is always the same, you can include it in the mapping:
:nnoremap <leader>z [zV]z!cat -n<cr>
Substitute the cat -n portion above—my example command—with the
appropriate command in your case.
I think you might want check out this vimcasts episode. May not be exactly what you want, but could get you there. Good luck!
My solution, in the end, was to insert non-breaking spaces and linebreaks in the respective places in the file. :set list listchars=nbsp:$ can then be used to display these 'special linebreaks'. Their presence allows interpreting code to identify the blocks of lines separated by this combination as related lines.
Of course this doesn't answer the question. The answer, according to my best knowledge now, is that neither :set list nor :wrap can be used to achieve this.

How do I use VIM effectively for editing English text?

As a programmer by heart, if not by profession, I increasingly rely on, nay live in VIM for most editing-related tasks. What tips can you offer for using (almost) everyone's favorite editor for editing general-purpose text, say, an article? I mean plain text, with minimal markup using Markdown or RST; I'm not looking for support for LaTeX or for entering mathematical formulae.
I enable soft-wrapping when I'm editing most text files:
:set wrap
If you decide to do the same, then you'll want to know about gj and gk in normal mode, to move by screen lines instead of physical lines. I use them so often I remapped the up and down arrow keys to them instead of k and j.
Whether you're editing hard- or soft-wrapped files, you'll get a lot of mileage out of gqap (or its cousin gwap) to re-wrap a single paragraph with hard newlines, and vipJ to join all the lines of a hard-wrapped paragraph back into a single line.
You might also want to consider including a in formatoptions, to enable automatic paragraph formatting:
:set formatoptions+=a
When you're doing all this wrapping and unwrapping, it's nice to keep Vim from mangling numbered lists:
:set formatoptions+=n
In fact, I'd suggest reviewing all the formatoptions and adjusting them to your particular preferences:
:help fo-table
More info:
:help gj
:help gk
:help gqap
:help auto-format
:help formatoptions
Spell checking:
:setlocal spell spelllang=en_us
" ]s moves to the next mispelled word
" [s moves to the previous mispelled word
Set a thesuarus for Vim
Use par to format text
It's not very well maintained, but the Vim-Outliner project makes Vim into a killer outliner for plain text writing. You can download v0.34 here (there's a more recent version, I think, but I'm not sure where to get it):
http://www.vimoutliner.org/postnuke-phoenix-0.7.2.3/html/modules.php?op=modload&name=Downloads&file=index&req=viewdownload&cid=4
I really enjoyed this blogpost about writing better with latex. You could use vim-latex :) It's more about writing better, than just editing english text though.
http://matt.might.net/articles/shell-scripts-for-passive-voice-weasel-words-duplicates/
Use insert abbreviations:
iabbr teh the
iabbr i I
iabbr definately definitely
Edit, another tip:
set wrap nolist linebreak
This tells vim to wrap lines that are too long with "visual" newlines rather than adding an actual newline character to the file. The 'list' option must be off because it automatically disables the 'linebreak' option.

How can you control folds by an external file in Vim?

I have been searching a solution which puts the fold marks and codes to an external hidden file.
This way you could have permanent folds without extra fold signs.
How can you control folds by an external file in Vim?
That's a very general question. What is "an external file" in vim, really ? Vim is, after all, a collection of "external" files. So if I define my folding preferences in my vimfiles, is that "an external file" solution ?
You could link, for example, define regex mechanisms of folding and source them from an external file.
But I think, what you ment is, "can I define an external file, so I can have by project custom folding, so that everyone who uses vim after I give them my files, will have the same folding" ? Yes, I guess you could do that by extrapolating from the method above.
But remember, vim has several methods of folding:
manual - where you manually define folds (this is nice, but it leaves your code with lots of curly brackets, but it is "portable")
indenting - where indending defines folds
expression (which I mentioned)
syntax - defined by syntax highlighting
marker - you already know that
... in the end, it will all come down to those few settings in your vimrc.
My advice: DO NOT CHANGE vim's indenting, syntax, etc. files. Normally, you will not even need to change those (unless you're using a language vim has no support for, which I doubt). So, define your settings and preferences in your vimrc and vimfiles directory, and just give your coleagues preferences which they need (usually just those from vimrc) to have the same folding behaviour.
Vim can be made to atuomatically remember folds in a file by putting these two lines in ~/.vimrc
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
Use manual method of making folds. This is not the same as marker as implied above. Use :mkview to save folds and :loadview to reload them.

Resources