How to eliminate the text "[n] lines folded" - vim

I would like a folded line to display only "-" characters: no text at all.
I have tried defining foldtext per examples here and in help:. I was able to eliminate the first line contents, which were extremely annoying and completely illogical to my thinking, but it still displays
---"3 lines folded"-------
for example.
(For me it is visually distracting and irrelevant...partly defeats the purpose of folding for me, which is to HIDE the collapsed section, not to HIGHLIGHT it, which is what this redundant message does.)

You do not say what you tried that did not work, but with
:set foldtext='---'
I see a line of dashes (full width) in place of the fold. If you want even less distraction than that, try
:set fillchars+=fold:\ foldtext='\ '
(There are two spaces after the first backslash.)

You can use following settings in your .vimrc:
set foldtext=EmptyFoldText()
function! EmptyFoldText()
return '-'
endfunction
Works fine on my vim.

Related

What's wrong with gVim copy paste between windows?

I'm new to vim and I'm using gVim on Windows. I have two windows open in the same gvim instance and I'm just trying to copy some code from one to another. For some reason when it pastes it replaces the contents of some code with literally this:
list.forEach(function(name){...}------------------------------------------
Obviously, my real code does not have ... or a ton of dashes. What the hell is happening?
The dashes (----------) give it away: The block of code has been folded. (You probably also see the line with different colors (depending on your colorscheme).) Depending on the filetype, folding can be manual or automatic. For your (JavaScript?) code, it's the latter. So when you paste a block of code, Vim automatically detects the block and folds it.
There's a whole lot of commands and options around folding. Read more about it at :help folding. If you find this too confusing (for now), turn it off via
:set nofoldenable
Just press zo on the dashes(-----)
Aside: Use zf on selected (v) lines to fold lines.
zo - Open
zo - Fold

Vim highlight every Nth line numbering?

My line numberings in Vim are blue (background) and white (foreground) but this is not very clearly especially for large files.
I would like to have every fifth line numbers background in darkblue and every tenth line numbers foreground in red, so that one can distinguish between 5 and 10 lines of code easily without counting or having to focus at the line numbering.
How can I make this happen? Unfortunately I did not find any plugin doing this..
You cannot make different hl(highlighting) on the line number column. You can somehow highlight the text in the line however. But I guess this is not what you are looking for, you just want to see some flag/highlighting on the line number column. Then the sign might be the closest to your requirement.
source this codes: (I just randomly picked color, you can adjust them as you like)
hi FifGroup term=bold ctermfg=red
hi TenGroup term=bold ctermbg=darkgreen
sign define fifth texthl=FifGroup text=->
sign define tenth texthl=TenGroup text=>>
fun! PlaceLineSign()
for i in range(1+line('$'))
if i =~ '5$'
execute 'sign place '.i.' line='.i.' name=fifth buffer='.bufnr('%')
endif
if i =~ '0$' && i>0
execute 'sign place '.i.' line='.i.' name=tenth buffer='.bufnr('%')
endif
endfor
endf
fun! RemoveLineSign()
sign unplace *
endf
nnoremap <F6> <c-u>:call PlaceLineSign()<cr>
nnoremap <F7> <c-u>:call RemoveLineSign()<cr>
then you can press <f6> to display those flag, and <F7> to hide them.
Note, there is one problem with "sign", if you displayed the signs, and change the line numbers, i.e. removed/added new lines, the "sign"s won't change correspondingly. But hide and display again should go.
It looks like:
You can use my DynamicSigns plugin. It defines a SignExpression command, that works similar to the foldexpression. In your case, you could simply do:
:SignExpression v:lnum%10==0?'Line1':v:lnum%5==0?'Line2':''
Advantage of using my plugin is, it takes care of adjusting the line numbers automatically, when you add or delete lines. Note: depending on the size of your file, this might slow down Vim. But this is a problem, many sign plugins have in common, since there is no VimL API for accessing signs.
I'm also thinking about the same thing but unfortunately there's no easy way to do it (unless using sign support as other answers suggested, at the cost of slowing down).
The best close thing native to vim is LineNrAbove/Below. However, it's defined in vim source itself: https://github.com/vim/vim/blob/f3fa18468c0adc4fa645f7c394d7a6d14d3d4352/src/drawline.c#L1156-L1167; It should be easy to extend to highlight groups like every k-th relative line but it I don't think in the foreseeable future that it would be included in vim. I believe this is the only usable option because evaluating is done in the vim core which is fast than doing so at vim script.

Using nowrap option, how can I make overlong lines visually recognizable?

I often edit files with overlong lines. Therefore, I would prefer to :set nowrap to visually retain the structure better. But using nowrap, I live in fear of not seeing a part that is essential to whatever I edit (or worse, could change the meaning dramatically).
Is there any way to accentuate non-wrapped overlong lines (I didn't find anything from a look at the help)?
Check the help for listchars:
extends:c Character to show in the last column, when 'wrap' is
off and the line continues beyond the right of the
screen.

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 make vim syntax highlight a whole line?

I'd like to have vim highlight entire lines that match certain patterns. I can get all the text in a line to highlight (by doing syn match MyMatch "^.*text-to-match.*$"), but it always stops at the end of the text. I'd like to to continue to the end of the term, like highlighting CursorLine.
I've tried replacing $ with a \n^, hoping that would wrap it around. No change. (I didn't actually expect this would work, but there's no harm in trying.) I also tried adjusting the syn-pattern-offset (which I read about here: http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-pattern). Long story short, adding he=he-5 will highlight 5 fewer characters, but he=he+5 doesn't show any extra characters because there aren't characters to highlight.
This is my first attempt at making a vim syntax and I'm relatively new to vim. Please be gentle and include explanations.
Thanks!
(edit: Forgot to include, this is a multiline highlight. That probably increases the complexity a bit.)
It's not very adaptive as the filename (buffer) and line to full row highlight needs to be explicitly identified, but apparently the sign command can be used:
It is possible to highlight an entire
line using the :sign mechanism.
An example can be found at :help
sign-commands
In a nutshell:
:sign define wholeline linehl=ErrorMsg
:sign place 1 name=wholeline line=123 file=thisfile.txt
Obviously, you should pick a higlight
group that changes the color of the
background for the linehl argument.
source: Erik Falor, vim mailing list
From the documentation on syn-pattern:
The highlighted area will never be
outside of the matched text.
I'd count myself surprised if you got this to work, but then again, Vim is always full of surprises.
could also try
:set cursorline
:set cursorcolumn
change colors like this:
:hi cursorline
:hi cursorcolumn
using the usual term=, ctermfg=, ctermbg= etc
see this answer
VIM Highlight the whole current line

Resources