Display some special character as a linebreak in Vim - 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.

Related

How to change a word in Vim (and all of its other occurrences)

I frequently use the combination c-a-w to change a word in Vim.
Are there any similar means by which one can quickly also change all other occurrences of said word in the specific file?
Use gn option for this purpose, in my case, I have a slightly different version of it
" allows me to use a smarter cgn
nnoremap c* *<c-o>cgn
nnoremap c# #<C-o>cgn
Now when you have to change a word many times, as long as you have not so many of it, because in this case, a classical substitution would be better, just press c* and then press "dot --> ." to change the next occurrencies of it.
If you want to see how awesomeness gn can give us have a look at: Operating on search matches using gn (vimcasts)
You could try:
%s/<CTRL-R><CTRL-W>/NewWord/g
<CTRL-R><CTRL-W> means keep control key pressed and hit R and W.
This copies the word under the cursor to the command line.
See :help c_CTRL-R_CTRL-W.
The main command for replacement of all occurrences is :substitute. Unfortunately, being an Ex command, it doesn't integrate too well with the single-word replacement (e.g. caw) in normal mode: Though you can insert the previously replaced word into the command-line with <C-R>", you still have to enclose it in \<...\> to enforce a whole word match, and also escape any special characters inside the word.
That said, there are plugins that offer help in that area. One of them is my ChangeGlobally plugin, which offers a gc{motion} alternative to c{motion} that then applies the change (or deletion) to other matches in the same line or entire buffer. (The plugin page has links to alternative plugins.)

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.

How to write complex syntax parser in flex/bison and connect it as vim plugin?

How to write complex syntax parser in flex/bison and connect it as vim plugin ?
Need some generic way to make vim colorer (and completion in ideal case) plugins using flex/bison -- especially for some complex languages, can't be covered by dumb vim regexps.
The same method should be used for Eclipse-like "outline" tab -- parse source and make clickable side panel for fast source navigation.
PS: win32 (mingw) .dll preferrably for this build: http://www.vim.org/download.php#pc
So, you want to do the actual parsing of a Vim buffer outside of Vim, by your flex/bison-based parser, and then perform the highlighting within Vim based on that parsing, right?
Well, inside Vim, the highlighting is based on regular expressions (given to the :syntax match or :syntax region commands). There's no way around that (short of extending Vim itself).
The only way that I see is using line / column addressing for parsed elements. Vim regular expression have special atoms that only match at a particular position (:help /\%l, :help /\%v).
In Vim 8, you could trigger your parser (asynchronously in the background) via the new :help channel feature.
Example
Your parser has identified an identifier at line 3, screen columns 14-16. You would synthesize the following Vim command:
:syntax match Identifier "\%3l\%>13v\%<17v."
Critique
This will work for static buffer contents. As soon as you're doing edits, highlighting will probably be trailing behind. Vim applies the regexp-based highlighting as you type, but your external parsing will add some more delay.
For complex syntaxes / large buffers, the number of distinct syntax elements may cause performance degradation within Vim. I'm not sure how efficient the regexp-based addressing is; this would need to be tested.

how to add vim keymap

While programming I am regulary using the following two lines:
sprintf(buff,"%s", __func__);
putrsUART(buff);
Is it possible to set any keyboard shortcut to insert these two lines?
E.g. when I type \sp in command mode, these functions get added at the cursor position in my file. Is this possible? And if so, how do I map my keys?
Thanks in Advance.
You can use abbreviations, which are designed for this.
:abbr spb sprintf(buff,"%s", __func__);
:abbr uart putrsUART(buff);
Use :help abbr for the gory details. Note that you need to type another character after the abbreviated form for vim to recognize them. This comes naturally for these as you will type ENTER as the next character. It is also possible to enter more than one line with abbreviations. Simply use <CR> where you want a new line.
Here's an easy mapping for normal mode that lets you hit \sp (unless you've remapped leader, in which case use that instead of \) in order to insert the sprintf statement.
map <Leader>sp isprintf(buff,"%s", __func__);<Esc>
That being said I think abbreviations are the way to go here
As already mentioned, abbreviations (which I would limit to insert mode (:iabbr), because you probably won't need them in the command-line) are best for simple expansions; you can also define them only for certain filetypes only (via :iabbr <buffer> ...).
Your __func__ looks like a template parameter that you need to adapt each time. You cannot do this via abbreviations, but there are various plugins (many inspired from functionality in the TextMate editor) that offer template insertion with parameter expansion and several advanced features. Check out one of snipMate, xptemplate, or UltiSnips.
Try snip-Mate for inserting regularly used codesnippets. http://www.vim.org/scripts/script.php?script_id=2540
Wrong answer, Sorry:
Try this in your vimrc:
map <c-w> :sprintf(buff,"%s",func)<cr>
This means mapping to Ctrl-W.

How can I share my folds in VIM?

I am in a project with 3 people. We need to have the same folds in Vim for each member. How can I share my folds?
[Feedback]
I understood one important thing: Google ignores signs, such as {{{, so please google "VIM three braces" to find help about the marker-method. It becomes much easier to practise, as you can quickly find relevant information.
In order to use the the marker-method (suggested by Adam Bellaire), please note that you have to set the method:
:set foldmethod=marker
Thanks for your answers!
Probably the easiest way is to just use fold markers (e.g. {{{1), making sure to include the vim:fdm=marker setting in the file itself. For example, here's a shell script which contains both the setting to use fold markers and two levels of fold:
#/bin/sh
# vim:fdm=marker
echo This file contains fold markers.
#Top Level Fold {{{1
echo This is a top-level fold.
#Second Level Fold {{{2
echo This is a second-level fold.
Opening this file in vim will show the first four lines and then a fold, which if expanded will reveal the second fold. Just make sure to put a space between your comment syntax and the vim:fdm=marker line or vim won't see it. For example, in C you could use:
// vim:fdm=marker
Folds in files ?
Well, same settings should result in same folds.
Vim can fold in several ways: manually, by indent, by expression, by syntax, and by markers (by default, I believe are curved brackets, 3 of them).
So if you have the same vim version, and they haven't changed their syntax and indent files, let them check out your vimrc for foldmethod and foldmarker options, and copy them to their vimrc files. That should do it.
I haven't shared VIM folds with someone before, but if you're working on the same machine perhaps you can use VIM sessions, which will save your current state (including folds). Run the following command in VIM:
mks! /path/to/session_file
Then your friend can load up the session file:
vim -s /path/to/session_file
Ancient history, I know, but this may have appeared since the version of vim present in '09, and since I don't have an adequate reputation to comment yet, here we go.
The good news is that saving a view for the file should save manual folds as well, even nested folds.
The bad news is that I found it didn't produce consistent results under vim 7.0 (RHEL 5.5). This may have been fixed in a subsequent update that, sad to say, we aren't allowed to install.

Resources