How to make displayed text different from actual text in the file - vim

Is there anyway to have vim display something that is different from the actual text in a file?
For example, is it possible to make it so that when a file contains:
link
Vim displays
[link]
Thanks in advance.

Yes, it is possible. Writing a vim syntax file is a little arcane, so you might look first to see if someone has already written something that does what you want. For example, try http://vim.wikia.com/wiki/Patch_to_conceal_parts_of_lines#Using_Conceal. (If the fragment gets lost, then scroll down to the section on "Using Conceal". A quick search on Google and on GitHub did not turn up anything better.) The starting point in the docs is
:help conceal
or back up a little and start reading at :help :syn-arguments.
This feature was quite controversial when it was first introduced. Initially, Bram refused to include it, but now the syntax file for vim help files in the standard distribution uses the feature.
I did some experimentation. Try this. You can always add syntax rules; the latest wins. So as long as this gets :sourced after the regular HTML syntax file, you should be good. Remember to set 'conceallevel' to something other than the default, and that 'concealcursor' matters for the current line.
syn region htmlLink matchgroup=htmlLinkTag start="<a\>\_[^>]*>" end="</a>" contains=#Spell,htmlTag,htmlEndTag,htmlSpecialChar,htmlPreProc,htmlComment,javaScript,#htmlPreproc
syn match htmlLinkTag "<a\>\_[^>]*>" conceal cchar=[
syn match htmlLinkTag "</a>" conceal cchar=]
I am a little afraid that this might highlight too much with the htmlLinkTag group.

Related

vim automatic hard wrap for fortran with line-continuation

I'm a Fortran programmer who uses both free-form and fixed form. Since I have to mix them, usually I write code in a common form between free and fixed format, so in this way I can tell to vim that all my files are in the free format.
Vim is great in doing things like autoindentation, but I would like to type and let vim automatically wrap my code, and placing the Fortran continuation character & at column 73 (or greater), and at column 6 in the new line. Is it possible, or does it exist a plugin for this?
Currently I'm using textwidth=72 in fortran files to hard wrap the lines.
Thanks in advance.
One way to make vim insert text when going to a new line is to use formatexpr. Set it so to capture the line and replace it with itself with & and new line appended, when at/beyond a given column. In this case you are handling line breaks and textwidth does not apply. I didn't yet get to test some simple code for it, but here is a related example.
Another way would be to write general code so that when in a given column it inserts & and <CR>.
However, making any such approach respect Fortran-specific exclusions (comments, for one thing) will make it more complicated. The best solution would be to find suitable existing option(s) for Fortran, but I haven't so far.
This is a comment on indentation in general. It should allow you to directly set up a desired rule for a new line. Here is one standard set of files that set up a lot of indentation rules and features.
The usual entry point is this vim script, which requires another standard set of files. The link given on that page for the other files is broken though, so here is where to find them: unpack this zip file (found on this page), right into your ~/.vim/ directory. It will create subdirectories indent/, syntax/, and ftpplugin/, or put files into them if they exist, so be careful if you have stuff there already.
Then you can put the first script linked above into .vim/after/indent/. In this file, there are specific calculations of where to put the cursor when a new line is entered. Find the right place(s) and change to your desired indent, or preferably set up a snippet from it in another file (so not to change this file). In this case you also need to set things up so that it overrides settings from the first file.
A useful resource is indentexpr (or :help indentexpr).
Here is also a tutorial on that.
These are comments on syntax in general, posted initially. They contain items of help related to what you want and should be generally useful, but probably have not much to say about adding &.
There are plugins for fortran. Here is the syntax file, with many things to tweak.
This may already be on your system. (It was on mine.) Thus I would go through and pick and choose things to add to your .vimrc. Here are a few options that are directly related
syn match fortranContinueMark display "&"
syn sync linecont "&" minlines=20
Here are paragraphs that seem to me relevant in their entirety
if (b:fortran_fixed_source == 1)
if !exists("fortran_have_tabs")
"Flag items beyond column 72
syn match fortranSerialNumber excludenl "^.\{73,}$"lc=72
"Flag left margin errors
syn match fortranLabelError "^.\{-,4}[^0-9 ]" contains=fortranTab
syn match fortranLabelError "^.\{4}\d\S"
endif
syn match fortranComment excludenl "^[!c*].*$" contains=#fortranCommentGroup
syn match fortranLeftMargin transparent "^ \{5}"
syn match fortranContinueMark display "^.\{5}\S"lc=5
else
syn match fortranContinueMark display "&"
endif
if b:fortran_dialect != "f77"
syn match fortranComment excludenl "!.*$" contains=#fortranCommentGroup,#spell
endif
Then a block of syn match statements follow for common cpp-like settings, and then
"Synchronising limits assume that comment and continuation lines are not mixed
if exists("fortran_fold") || exists("fortran_more_precise")
syn sync fromstart
elseif (b:fortran_fixed_source == 0)
syn sync linecont "&" minlines=20
else
syn sync minlines=20
endif
By your question it appears that you know how to set up .vimrc but here are a few comments.
Syntax support need be enabled with appropriate enable and autogroup statements, for example
syntax enable
" au BufRead,BufNewFile *.f90 FileType=fortran
au FileType fortran setlocal ...
Here are some common formatting options that I have for fortran
autocmd FileType fortran setlocal formatoptions=croql comments=:/!/
There can also be a t among options, for textwidth
Here are some specific settings I have, which I see in this syntax file with far more sophistication
let fortran_free_source=1
" Said to need fortran.vim and/or fortran support packages (they work)
let fortran_do_enddo=1
let fortran_more_precise=1
Standard vim help is of course extensive, but try :help fortran -- it has a number of useful settings right up front and is not overwhelming at all. Also see ft-fortran-syntax from help.
See this post with some troubleshooting if things aren't working right. Here is another useful post, even as it appears unrelated by its title.

Make gvim markdown highlight ignore internal markers

I recently upgraded to gvim 7.3 and was pleased to find markdown highlighting. I also noticed that it treats "internal" _(underscore) as a marker. For example:
I want gvim to display emphasis here
but not_here
gvim actually displays the last line in my example as 'but not_here". It looks like SO's markdown interpretation is closer to what I want.
I do not say that gvim is "wrong" because I do not know what the correct markdown implementation is. However, is there a way to configure it so that the markers should be treated as normal text if they are surrounded by non-whitespace?
The runtime files (especially if you use the old Vim 7.3.000 / 046 installer found on vim.org) aren't updated frequently. Most plugin authors publish more recent releases elsewhere, and they are only occasionally picked up by Vim.
In Tim Pope's repository, you'll find a newer version (that you can install into your ~/.vim directory) that doesn't show the problem; instead, it even highlights the single underscore character as an error.
I have found a solution, which works in the things I have tested sofar.
Copy the %vim%/syntax/markdown.vim file into %/.vim/syntax/markdown.vim and change line 63 into:
syn region markdownItalic start="\s_\S\#=" end="\S\#<=_\|_\S\#=" keepend contains=markdownLineStart
Restart vim and it should match *this* and _this_ but not_this.
EDIT: Changed information, thanks to #ZyX
I had these issues when documenting code in markdown files.
The solution I used was to put the offending sections in a codeblock with four spaces or in a code span with surrounding back ticks (`).
Try to use Github flavor markdown: https://github.com/jtratner/vim-flavored-markdown

Plugin name for showing indentation guide in gVim

Anybody, any idea??
which plugin is showing the indentation guide in the image below. Downloaded from http://leetless.de/images/vim/pyte.png
.png
Thanks
A similar effect could be achieved with:
set list
set listchars+=tab:\│┈
Maybe with another filler character.
See :help 'list' and :help 'listchars'.
But…
As I was writing that answer it appeared to me that the answer was probably in the colorscheme's author's ~/.vimrc.
I think that you should really work on sharpening your deduction skills. It takes about 30 seconds to 1 minute to find that information by yourself:
Go to the site where you get that pic from: http://leetless.de
Look around for something Vim-related. The navigation is generally the first place to go and what do you find? "Vim themes" at http://leetless.de/vim.html
That image illustrates the first "theme" featured, that's a good sign. But let's read the introduction text (emphasis mine, typos his):
If you are curious what some other things are (like the indetation markers) and how they work, take a look at my .vimrc. Note that the encoding is broken with that file so the "set lcs" part is probably not copy-pasteable, follow the instructions in the comments above that line in order to find out how you can make your own unicode-lcs.
Wow! It looks like you are getting closer to the truth. Beware of the Cigarette Man!
Follow the link and do a search for lcs.
Done.
Not sure what you mean by "indentation guide", but I don't think any plugin is involved.
The first thing I see that you might be referring to is the characters at the beginning of lines indicating where there are tabs. That can be done by doing setting the listchars option to an appropriate value, turning the list option on, and selecting a color for the SpecialKey highlight group.
The other thing you might be referring to is highlighting of the column that currently contains the cursor. That can be done by turning on the cursorcolumn option. The color used for that can be set with the CursorColumn highlight group.
There's also a plugin, vim-indent-guides
Here's a nice plugin for vim: https://github.com/Yggdroot/indentLine

extend/modify vim highlighting for all filetypes at once?

How do I extend/modify vim highlighting for all filetypes at once?
I have certain relatively simple patterns which I'd like highlight differently, that can occur in any filetype. So rather than adding something like the below to every conceivable filetype I might use (~/.vim/syntax/python.vim, .../css.vim, .../html.vim, ...) is there some way I can define it once for all filetypes?
syn match SpecialComment "#[#\-+].*" containedin=Comment
syn match Comment "\* .*$"hs=s+1 containedin=SpecialComment
update:
As suggested I saved my changes to ~/.vim/after/filetype.vim, with the result that it works in Cream but not stock Gvim or Vim. The actual code I'm using here, a sample python file to test against here, and the desired result:
You could try putting those two lines in ~/.vim/after/filetype.vim. That should get sourced after any of the top level syntax files. It's possibly not the 'correct' place to put it, but it should work.
filetype.vim seems to be sourced BEFORE the syntax files, so it gets overwritten by the default syntax file. Therefore, I'd recommend you create a new file called something like:
~/.vim/after/common_syntax.vim
with the highlight lines that you're interested in. Then, add this to ~/.vim/after/filetype.vim:
if !exists("after_autocmds_loaded")
let after_autocmds_loaded = 1
au BufNewFile,BufRead * source ~/.vim/after/common_syntax.vim
endif
This will cause the file to be sourced once the file has been read.
P.S. Responding to the comment in your sample code: "why can't we use plain ol 'comment' group instead of 'pythoncomment' etc. ?", it's because the syntax highlight group is pythonComment, which is merely coloured in the same way as Comment. If your syntax is unique enough for it not to be a problem, you could just do containedin=ALL. If it is close, but not quite unique, you could do containedin=ALLBUT,conflictgroup where conflictgroup is the highlight group you want to steer clear of.

Vim: C++ symbols' color

VIM: Is it possible to change the color of these symbols:
~!%^&*()-+=[]{},.<>?:/;
like Visual Studio does?
The C/C++ syntaxes are defined in syntax/c.vim and syntax/cpp.vim. If you're using Linux, the main syntax directory is in /usr/share/vimXX/, where XX is the version (e.g. mine are in vim72). I don't know about installation directories on other OSes, but I'm sure you can find it. I'd suggest making a copy of these and placing them in your user vim directory (for example, in Linux, $HOME/.vim/syntax/c.vim and so on). You can then add whatever you like.
The C++ syntax sources the C syntax, so any symbols you want highlighted in both should go in c.vim, and anything for C++ only should be in cpp.vim.
To get syntax highlighting for specific symbols, you'll need to use a syntax match statement, something like:
syn match cUserSpecialCharacter display "[~!%^&*()-+=[\]{},.<>?:;]"
syn match cUserSpecialCharacter display "/[^*/]"me=e-1
syn match cUserSpecialCharacter display "/$"
I called it cUserSpecialCharacter since cCharacter and cSpecialCharacter are already used. The second and third matches are a bit of a kludge to highlight '/' without it matching comment prefixes, which would then override the comment highlighting and break everything. The "display" option tells Vim that it doesn't need to look for this match if it's not going to be displayed - see :help syn-display for an explanation if you like!
Once you've defined a syntax match, you can link it to a highlight group, for example:
hi def link cUserSpecialCharacter cCharacter
This will put it in with the already defined cCharacter group, so it'll get whatever highlighting that gets - in this case, Character. You can see a nice list of highlight groups at the bottom of c.vim for examples. If you really want, you can also hardcode a highlight by doing something like:
hi cUserSpecialCharacter term=reverse ctermfg=15 ctermbg=1 guifg=#ffffff guibg=#800000
(Arbitrary example - my current highlighting for the Error group.) See :help hi for more information on this, or simply :hi to see the list of defined highlighting - plenty of examples. I'd recommend against doing this, though, since it won't change with color schemes.
Yes, you need to edit the C color theme in vimfiles/colors/c.vim I don't know all the theme options one can use, but I'm sure they are documented on http://vim.org/

Resources