syn match don't work in vimrc - syntax-highlighting

If I added to my vimrc this
highlight whitespaceEOL term=reverse ctermbg=Grey guibg=Grey
syntax match whitespaceEOL /\s\+$/
autocmd! filetype zsh,sh,python,vim,c,cpp syntax match ColorColumn /\%>80v.\+/
don't work, only work if I make
syntax clear
but I lost other syntax

you may want to look into the contained and contains keyword.
try adding contained=ALL at the end ef syntax match lines.

Related

Here document gives EOF error in Ruby IO

The following code give two errors which I am not able to resolve. Any help would be appreciated:
random.rb:10: can't find string "TEMPLATE" anywhere before EOF
random.rb:3: syntax error, unexpected end-of-input
Code:
id = 2
File.open("#{id}.json","w") do |file|
file.write <<TEMPLATE
{
"submitter":"#{hash["submitter"]}",
"quote":"#{hash["quote"]}",
"attribution":"#{hash["attribution"]}"
}
TEMPLATE
end
From the documentation (emphasis mine):
The heredoc starts on the line following <<HEREDOC and ends with the next line that starts with HEREDOC
Your code doesn't contain a line starting with TEMPLATE. If your text editor (or IDE) supports regular expressions in searches, try ^TEMPLATE.
You can either remove the spaces or if you want to keep them, change <<TEMPLATE into <<-TEMPLATE. The addition of - instructs the Ruby parser to search for an (possibly) intended TEMPLATE like you have in your code.

Add end of line comment to vim syntax

Not every command in Vim allows you to add end-of-line comments. Sometimes the " is valid as an argument, so it would be ambiguous. However, if you insert a pipe, the command is ended and you can insert a comment. So you can actually achieve reliable end of line comments in vim thusly:
noremap ' ` |" Use single quote as alternate range key
Neat, right? But the syntax/vim.vim file doesn't recognize this as an end of line comment. How do I tell Vim to recognize this syntax?
I found this in syntax/vim.vim:
syn match vimLineComment +^[ \t:]*".*$+ contains=#vimCommentGroup,vimCommentString,vimCommentTitle
I tried adding something like this to my ~/.vimrc, but there is no effect. VimScript is hard. :/
syntax match vimLineComment '|".*$+'
Any ideas?
you cannot use in-line comments for maps
:h map-comments
you will see:
*map-comments*
It is not possible to put a comment after these commands, because the '"'
character is considered to be part of the {lhs} or {rhs}.
I hope this answers your question.
hack
Okay, you may have good reason to do that.
Only define syn match vimLineComment is not enough, you have to overwrite the vimMapRhs syntax. so these two lines will make |"foo bar highlighted as comment:
syn match vimMapRhs '.*\ze|\s*".*'
syn match vimLineComment '|\s*".*$'
this may change the "comment" highlight, but I don't recommend to do it.
Background
vim 7.3 all platforms
vimscript language (used in vim files)
Problem
The vimscript language supports comments, but end of line comments do not always work predictably, because end of line comments can be mistakenly interpreted by vim as part of the command.
Adding end of line comments is problemmatic in vimscript, because it does not work with all commands.
Solutions
1) use the pipe character :help :bar to create a separate Ex command
this is the solution enumerated by #sidewaysmilk
2) simply add the comment below the relevant vimscript command on the next line
3) use the execute command (see :help :execute )
Pitfalls
Solution 1) is a somewhat unconventional use of the pipe (aka :bar)
Not all commands support the pipe character (see e.g. :help :execute)
Solution 2) may not be desirable for the readability of the vimscript, and it does not directly solve the issue in the OP
searching for this feature on the internet is tricky because it turns up links related to comments in general-purpose programming contexts, unrelated to vimscript
See also
Vim help links (enter these directly into vim Cmdline-mode):
:help vim-script-intro | /comments for some commands
:help :bar
:help Command-line-mode
Web links:
https://en.wikipedia.org/wiki/Vimscript
https://duckduckgo.com/?q=vim+vim-script-intro+comments
+1 fot "This is not "neat" at all":
noremap ' ` |" Use single quote as alternate range key
My preference is to use end of line comment (without adding |) where vimL allows.
The pain point is:
It's hard to remember when vimL allows that. (so some people never uses end of line comment in vimL, which may narrow his choice of formating)
Inspired by the OP, we can utilize the syntax highlight. (But I don't know how to implement yet)
Below is some information which seems to be needed:
Relevant lines in the syntax.vim:
/home/linuxbrew/.linuxbrew/Cellar/neovim/0.6.1/share/nvim/runtime/syntax/syntax.vim
syn region vimString start="^\s*\\\z(['"]\)" skip='\\\\\|\\\z1' end="\z1" oneline keepend contains=#vimStringGroup,vimContinue
syn match vimComment excludenl +\s"[^\-:.%#=*].*$+lc=1 contains=#vimCommentGroup,vimCommentString
syn match vimComment +\<endif\s\+".*$+lc=5 contains=#vimCommentGroup,vimCommentString
syn match vimComment +\<else\s\+".*$+lc=4 contains=#vimCommentGroup,vimCommentString
syn region vimCommentString contained oneline start='\S\s\+"'ms=e end='"'
hi def link vimCommentString vimString
" end of line comment
syn match vimLineComment +^[ \t:]*".*$+ contains=#vimCommentGroup,vimCommentString,vimCommentTitle
hi def link vimLineComment vimComment
syn match vim9LineComment +^[ \t:]\+#.*$+ contains=#vimCommentGroup,vimCommentString,vimCommentTitle
hi def link vim9Comment Comment
syn match vimCommentTitle '"\s*\%([sS]:\|\h\w*#\)\=\u\w*\(\s\+\u\w*\)*:'hs=s+1 contained contains=vimCommentTitleLeader,vimTodo,#vimCommentGroup
hi def link vimCommentTitle PreProc
syn match vimCommentTitleLeader '"\s\+'ms=s+1 contained
syn match vimContinue "^\s*\\"

Vim: how to automatically highlight each line containing a keyword?

if is a ".log" file, want to make every line containing keyword "dog" in the file to be in red, and make every line containing keyword "cat" in the file to be in yellow. This should be done automatically when I open a ".log" file in vim. Is there any way to do this?
First, define the colors as highlight groups:
:hi Dogs ctermbg=red guibg=red
:hi Cats ctermbg=yellow guibg=yellow
You can add (window-local) highlighting via the :match commands or the matchadd() function. By matching the entire line containing the keyword, you'll get all highlighted. The :autocmd installs that for your log files (though I would prefer to use Vim's filetype detection instead of hard-coding the file pattern).
:autocmd BufWinEnter *.log call matchadd('Dogs', '^.*dog.*$') | call matchadd('Cats', '^.*cat.*$')
The benefit of :match is that is doesn't interfere with syntax highlighting (which would be an alternative). The downside is that the highlighting will persist when you view a non-log buffer in the same window. (You can fix this by adding more autocmds, but it's not trivial.)

How to add comments to an Exuberant Ctags config file?

What character can I use to put comments in an Exuberant Ctags .ctags file?
I would like to add comments with explanations, and perhaps to disable some regexps.
But I can't find any comment character which ctags-exuberant accepts!
I keep getting the warning:
ctags: Warning: Ignoring non-option in /home/joey/.ctags
which is better than an error, but still a little annoying.
I have tried # // /* ... */ and ; as comments, but ctags tries to parse them all!
Here is an example file with some comments which ctags will complain about:
# Add some more rules for Javascript
--langmap=javascript:+.jpp
--regex-javascript=/^[ \t]*var ([a-zA-Z_$][0-9a-zA-Z_$]*).*$/\1/v,variable/
--regex-javascript=/^[ \t]*this\.([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]*=.*$/\1/e,export/
--regex-javascript=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
--regex-javascript=/^\<function\>[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)/\1/f,function/
# Define tags for the Coffeescript language
--langdef=coffee
--langmap=coffee:.coffee
--regex-coffee=/^class #?([a-zA-Z_$][0-9a-zA-Z_$]*)( extends [a-zA-Z_$][0-9a-zA-Z_$]*)?$/\1/c,class/
--regex-coffee=/^[ \t]*(#|this\.)([a-zA-Z_$][0-9a-zA-Z_$]*).*$/\2/e,export/
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*[-=]>.*$/\1/f,function/
--regex-coffee=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]+=.*[-=]>.*$/\1/f,function/
--regex-coffee=/^[ \t]*([a-zA-Z_$][0-9a-zA-Z_$]*)[ \t]+=[^->\n]*$/\1/v,variable/
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
You can't! I looked through the source code (thanks to apt-get source). There are no checks for lines to ignore. The relevant code is in parseFileOptions() in options.c
But sometimes comments are a neccessity, so as a workaround I put a comment in as a regexp, in such as way that it is unlikely to ever match anything.
--regex-coffee=/^(COMMENT: Disable next line when using prop tag)/\1/X,XXX/
The ^ helps the match to fail quickly, whilst the ( ) wrapper is purely for visual effect.
Your comment should be a valid regexp, to avoid warnings on stderr. (That means unescaped /s must be avoided, and if you use any [ ] ( or )s they should be paired up.) See Tom's solution to avoid these restrictions.
As #joeytwiddle points out, comments are not supported by the parser, but there is a work-around.
Example .ctags file:
--regex-C=/$x/x/x/e/ The ctags parser currently doesn't support comments
--regex-C=/$x/x/x/e/ This is a work-around which works with '/' characters
--regex-C=/$x/x/x/e/ http://stackoverflow.com/questions/10973224/how-to-add-comments-to-an-exuberant-ctags-config-file
--regex-C=/$x/x/x/e/
--regex-C=/$x/x/x/e/ You can add whatever comment text you want here.
You can use '#' as the start of comment if you are using Universal-ctag(https://ctags.io).
Given that comments don't work, what about a .ctags.readme file...
For most things you don't actually need a comment, e.g. you don't really need the comment below.
# Define tags for the Coffeescript language
--langdef=coffee
--langmap=coffee:.coffee
I can see however that you might want to add comments explaining some mind bending regex, so for each line that absolutely needs it you can copy paste it into the .ctags.readme file as a markdown file:
Forgive me father for I have regexed
It was purely because I wanted some lovely coffee properties
```
--regex-coffee=/^[ \t]*#?([a-zA-Z_$][0-9a-zA-Z_$]*):.*$/\1/p,property/
```
Keeping .ctags.readme and .ctags in sync
You could have a block at the bottom of the ctags file separated with a line break, then delete this final block.
If you only have the one line break in your .ctags file this sed will delete all the lines after the line break.
Then do some grepping for the --regex lines to append the lines from .ctags.readme into .ctags.
sed -i '/^\s*$/,$d' .ctags
grep "^--regex" .ctags.readme >> .ctags

Gvim Syntax Customization

How can I make Gvim to treat(syntax highlighting) lines starting with # and ; in an assembly file as comment ? Meaning in addition to ; (which is normally the way to represent comment in assembly)
If the file is being recognised as Assembler, it will have it; $VIMRUNTIME/syntax/asm.vim has the line syn match asmComment "[#;!|].*" contains=asmTodo which matches # lines as comments.
Going by the contents of Vim 7.3's $VIMRUNTIME/filetype.vim, .asm, .s, .S, .a, .A, .mac and .lst files will all be recognised as Assembler. If you are having a different extension, look at :set ft to see what it's being recognised. You may need to override it in your ~/.vimrc:
augroup filetypedetect
au BufNewFile,BufRead *.whatever setf asm
augroup END
On you installation, you have a file called syntax\asm.vim which should define color syntax rules for assembly.
On my installation, I got the following line:
syn match asmComment "[#;!|].*" contains=asmTodo
Which means that lines starting by # (or ;) should be considered as comments and it does work on my installation.
You can check in filetype.vim if the file you are working on is recognized as asm.
In your .vimrc or .gvimrc which are vim configuration files, you need :
" Syntax highlighting and filetypes
filetype plugin indent on
syntax on

Resources