Is it possible to display page feed symbols differently in Vim? - vim

One of the nice things about Vim is that one can insert a page feed symbol (Ctrl-L in Insert mode), which delegates the printer to start printing the following on a new page. It shows as ^L in text.
Is it possible to make this symbol show as something else, for example as
----------------- new page here -----------------
so it is somewhat more visible while scrolling through pages of text?
That is, without rebuilding Vim from source.

If you do not use folding extensively when editing those files
containing page feed symbols, you can use one-line folds to mark
them out.
Using the foldexpr option, it is possible to increase the fold level
of the lines that contain page feed symbol. (Herein, for the sake of
efficiency of evaluating foldexpr, I assume that page feed symbols
are always the first characters on their lines.) To achieve the
desired effect of a screen-wide separator, these folds can be made
auto-closable.
The following function configures folding according to the idea
described above. Call it (manually or by means of an autocommand)
to enable page feed symbol folding in the current buffer.
function! FoldPageFeed()
setl foldmethod=expr
setl foldexpr=getline(v:lnum)[0]==\"\\<c-l>\"
setl foldminlines=0
setl foldtext='---\ new\ page\ '
setl foldlevel=0
set foldclose=all
endfunction
Resulting separators appear as the text --- new page followed by
a continuing string of filling characters to the right of the window
(see :help fillchars, under the fold: item).

I don't think you can. There is a non-text highlight group which could help.
Another way (a bit ugly) would be to write some autocommand to expand ^L to ---- new page ---- and vice versa when on InsertLeave, BufRead and BufSave.
Anyway, the answer of your question is no, if you just want to change the display, and probably yes with a nasty plugin.

If it could, I would be uncomfortable knowing that Vim, my heavily trusted tool and friend, could misrepresent a text file.
Of course, you could always execute this command (perhaps as a macro) to do the same thing:
:%s/^L/----------------- new page here -----------------/

If you defined your own highlight group in Vim to be just the ^L symbol, then you could have a custom highlighted background for all lines that contain that character.
It’s not quite a ---- new page here ----, but it would make page breaks easily visible when scrolling through large amounts of text.
I don’t know enough Vim to actually tell you how to set the highlight group though…

Related

get the current statusline from buffer

I have a plugin that has some functionality that adds some string to the statusline. I have a series of functional tests that verify that certain actions produce some output, and would like to verify that, if set, the statusline will change after some trigger.
I haven't been able to find something that will allow me to capture (dump?) the rendered contents of the statusline. Both the :help statusline and :help status-line didn't hint to anything that might allow this.
Is there a way to actually do this?
You're looking for :help screenchar(). With it, you can capture any visible screen cell controlled by Vim (including status lines), and it's main purpose is testing. Its use is a bit clumsy, because you have to query each position individually, and have to explicitly convert back to characters / strings. For example, if the statusline is the second-to-last line, this will get it's first character:
:echo nr2char(screenchar(&lines-1, 1))
And this will obtain the entire statusline (assuming you don't have any vertical window splits):
:echo join(map(range(1, &columns), 'nr2char(screenchar(&lines-1, v:val))'), '')

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 configure Vim word wrap options vimrc

I'm trying to configure Vim so that it always wraps at x-number of columns automatically, at all times such as in this other editor that I provided a screenshot of-
Use :set wrap and :set textwidth=N . For permanent effect add the wrap settings to the vim start-up file .vimrc
set wrap
set textwidth=X
To apply the wrapping to an existing file, move to the start of the file (you can use gg to do this). Then type gqG to apply the re-formatting to the entire file.
Also setting a margin may be useful:
set wrapmargin=0
If you want hard wrapping, i.e. real reformatting of the text by inserting newline characters, the 'textwidth' setting (as outlined in suspectus's answer) is the key. You need to reformat existing text, e.g. via the gq command.
For soft wrapping, i.e. where long lines just appear to be broken in the editor window, :set wrap will always fill the entire available window space; you cannot restrict that to take less. Either resize the Vim window, or add a padding buffer to the right that restricts the available space to the current window. For the latter, my LimitWindowSize plugin may be helpful.
Judging from the second screenshot, you seemed to want soft-wrap. This answer suggests a way to achieve a soft-wrap effect .
I'm generalizing the solution a bit compared to the one I referred to, but basically
use :set nuw to see how many columns the line numbers are occupying, let's call this nuw.
then use :set columns=x where x = (the number of columns you want + nuw).
Note that the columns is automatically reset to window width once you resize the window, so this manual setting is not persistent.

How to create a syntax file for text files?

I wish I could format pieces of text in standard text files.
I would like to select a phrase or a word, invoke a simple command, and format it with these formatting:
bold
italic
underline
red font color
yellow background color
Is that possible?
(something like syntax highlighting in vim files but in my case with selected text)
I know there is a Txtfmt plugin in vim, I tried it 2 times the last years but uninstalled it every time.
This is why:
There is no way to visual select text and format it, you have to insert a begin code in the command-line write the text and insert an end code in the command-line.
You cannot copy the text because Txtfmt plugin inserts hidden formatting codes in the text
It is too complicate to insert a begin code and end code in the command line (after invoking a general command) and the codes are too complicated
There needs to be a kind of markup inserted into your text. Vim operates on plain text, it has no separate meta data layer like a word processor. With syntax highlighting and the new conceal feature, you can partially make Vim appear to be WYSIWYG, but actually you're still operating on the raw text.
If you're fine with that limitation and want to stick with Vim for your editing tasks, I'd suggest to use an existing markup solution instead of inventing a new one from scratch. By now, there are several syntaxes to choose from, with Markdown probably a favorite. (It's used here on Stack Overflow, too!) With an existing syntax, you can leverage all the existing plugins / mappings / commands, and even some external tools (e.g. for converting to HTML).
Here is probably the simplest possible implementation of what I believe you want. It allows custom strings to be highlighted in the current buffer using the colour/style you describe. The custom strings you choose to be highlighted cannot be saved between Vim sessions.
Firstly, define the highlight group SpecialTxt for your bespoke highlighting:
au ColorScheme * hi SpecialTxt guibg=yellow guifg=red gui=bold,underline,italic
(note that this has to be done via an autocmd so that loading a new colourscheme doesn't overwrite it).
Now you can select any regular expression to inherit this colour using:
call matchadd( 'SpecialTxt', [YOUR_REGEX], 1 )
Or you could write a simple command to set the match:
command! -nargs=1 Special :call matchadd('SpecialTxt',"<args>",1)
Or create a map so that double clicking on a word sets it to this colouring:
nnoremap <silent> <2-LeftMouse> :call matchadd('SpecialTxt',expand('<cword>'),1)<CR>
Hope that helps. Apologies if it is not quite what you are after.

Is it possible to not display a ~ for blank lines in Vim/Neovim?

Is it possible to not display a ~ for blank lines in Vim?
This confuses Mac Vim's scrollbar, and I quite don't like these tildes.
:hi NonText guifg=bg
That command should set the color of non text characters to be the same as the background color.
Vim 8.x:
You can now change the color just for the end of the buffer ~:
highlight EndOfBuffer ctermfg=black ctermbg=black
See changelog for Vim 8.x.
As jamessan said, you can’t disable them. The scrolling behavior isn’t specific to MacVim, either — it works the same way in the terminal and in gvim:
Instead of seeing this as a problem, what you should do is learn to see this as part of Vim’s flexibility. For example, you can use the zt command to scroll the current line to the top of the screen, regardless of where in the file it is. This can make it easier to write macros that do some work and then scroll back to where you were. The commands <C-E> and <C-Y> are made simpler because of this, as is the 'scrolloffset' option.
If you must, retrain your brain to think of Vim’s scrollbar as mapping to which line is on top, instead of which screenful is visible.
For NeoVim, you can set the fillchars value for eob to a space character and that will effectively hide it. (This might not work for plain Vim).
In Lua (Nvim 0.5+):
vim.wo.fillchars='eob: '
In VimScript:
set fillchars=eob:\
Note: Calling the above will override your fillchars value for other items as well (if set), so use this as a reference to set multiple values together:
set fillchars=eob:\ ,fold:\ ,vert:\│
Or use set fillchars+=... to append it your existing values.
You can't disable them, but you can change your colorscheme such that the NonText highlight group is colored the same as the Normal highlight group. However, this affects more than just the end of document tildes.
I doubt that it's actually "confusing" MacVim's scrollbar and if it is, then that's a bug in the patching that MacVim does.
The tilde ~ characters are meant to remind the user that those lines are not part of buffer content.
The above highlight trick will hide the ~ character, but it is still there. For some terminals, this may not even work. If you happen to be a Neovim user, you can use fillchars option to change the end of buffer symbol like this:
set fillchars=fold:\ ,vert:\│,eob:\ ,msgsep:‾
This will use space instead of ~ for end of buffer, effectively hiding the annoying ~.
You may also be interested in discussions here.

Resources