the dollar sign `$` cannot be removed from iskeyword [duplicate] - vim

In my vimrc, I have included a script (say, otherscript.vim, which I need to include for work reasons) that says:
autocmd FileType php setlocal iskeyword+=$
and I don't want this behaviour. So, sometime later in the vimrc, I say:
autocmd FileType php setlocal iskeyword-=$
(I also tried using set instead of setlocal.) But, when I open a php file, iskeyword still contains the $ symbol in it. I am using vim 7.2. The output of ':verbose set iskeyword' is
iskeyword=#,48-57,_,192-255,$
Last set from /path/to/otherscript.vim
The output of ':scriptnames' is:
...
7: /usr/share/vim/vim72/ftplugin.vim
8: /home/yogeshwer/.vimrc
...
74: /path/to/otherscript.vim
...
Can somebody help me how I can revert the changes to 'iskeyword' made by the other script? Thanks a bunch.

I like to avoid autocmds when I can and use the after directory structure.
$ mkdir -p ~/.vim/after/{ftplugin,syntax,indent}
$ echo 'setlocal iskeyword-=$' >> ~/.vim/after/ftplugin/php.vim
This sets up a basic after directory in your user-specific vim config folder. Whereas ~/.vim/ftplugin/$FILETYPE.vim would be used in lieu of vim's standard $FILETYPE.vim file, files in an after directory get executed after, allowing you to override or change the behavior of your ftplugins, syntax definitions, and indent commands.
As an additional example to show you how these work, I'll include part of my local after/syntax/python.vim file here. I like all the "structural punctuation" of my code to stand out when I read it, so I do this:
syn match pythonParen /[()]/
syn match pythonBrack /[][]/
syn match pythonCurly /[{}]/
hi def link pythonParen Paren
hi def link pythonBrack Brack
hi def link pythonCurly Curly
I've also got an after/indent/php.vim file that was supposed to fix some of the annoying indent issues I ran into with the indent behavior when switching in and out of <?php ?> regions in a template file, but the code is a mess and never really worked in the first place, so I won't reproduce it here. I mention it only to give you an example of what can be done with the after hooks.

Related

Vim code formating with astyle (formatprg in vimrc)

I'm trying to use astyle as my code formatter in Vim. However, I can't seem to find how to tell vim which options to use for astyle.
The following is in my vimrc:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\
If given no options in command line mode, astyle will try to find an options file named .astyle. However this does not seem to work here. (Ie: when I format in vim: gqG the result is totaly different than if I had called astyle from the command line !)
This is my .astyle options file:
--style=allman
--mode=c
--attach-classes # -xc
--attach-closing-while # -xV
--indent-classes # -C
--keep-one-line-blocks # -O
--keep-one-line-statements # -o
--align-pointer=name # -k3
So I tried to simply specify the options in my vimrc like in this answer:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ --style=allman --mode=c -xc -xV -C -O -o -k3
Saved, sourced, reloaded vim entierly: no change. The options seem to not take effect. When I format with gggqG the result is still not what my options ask for... Can anyone see why this is not working ?
(Note: I want to be able to format using gq and I don't mind reformatting the whole file entierly each time, unlike in this question)
EDIT:
After a few stupid mistakes I've escaped all the spaces as suggested in #romainl comment. However there seems to be an error comming from vim which I cannot interpret:
/bin/bash: -c: line 1: syntax error: unexpected end of file
shell returned 1
E485: Can't read file /tmp/vHXZmnp/3
Since no one is answering, I'll share what I managed to come up with.
Following the advice given in the comments I escaped all the spaces in the sequence of options passed to astyle. However, this leads to bash trying to interpret the options, failing and kindly telling us so: see question edit.
The solution that seems to work is to create a system wide astyle options file and pass the path to that using command line arguments instead of giving it the options directly.
(This can probably also be done with a project options file but requires having an options file at the root directory of every project)
Taking my options file .astylerc in my home directory this gives:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ --options="/home/myusername/.astylerc"\
Note that a relative path did not work, but it is possible to write the path to the options file using a 'pseudo relative' path using the $HOME environment variable as mentioned in the astyle documentation

vimrc how to invoke unix find?

i want to set the tags variable to the set of all gotags files i generated in specific folder(s) using exuberant Ctags. (gotags is nothing but the tags file renamed).
i put following lines in my .vimrc file.
set tags+=/usr/local/go/src/gotags
set tags+=`find /home/vimal/gowork/src -name gotags`
but it doesnt work and i get the following error
$ vi ~/.vimrc
Error detected while processing /home/vimal/.vimrc:
line 157:
E518: Unknown option: /home/vimal/gowork/src
Press ENTER or type command to continue
how can i fix the error and set the tags variable with the value: list of all the gotags files under one directory tree.
Inventing new syntax tends not to work that well in practice. Use system() to run external commands from Vim, not backticks. Also set in Vim is weird, it doesn't evaluate RHS the way you expect. Most of the time it's a lot simpler to use let &option = ... instead of set option=....
Anyway, to answer your question, you don't need to run find(1) for that, plain Vim functions are enough for what you want:
let &tags = join(extend([&tags, '/usr/local/go/src/gotags'],
\ findfile('gotags', '/home/vimal/gowork/src', -1)), ',')

Vim - How to insert a backslash at the start of a line in a new file using autocmd and a template file

I followed this guide to automatically insert different header templates into new files of different types based on the file extension:
http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/
It works great! I have a custom header for python source files that gets inserted automatically when I open a new .py file.
I want to do a similar thing so that a basic LaTeX template is inserted when I open a new .tex file...
Except I can't get it to work...
My ~/.vimrc says this:
autocmd bufnewfile *.tex so /home/steve/Work/tex_template.txt
and my tex_template.txt says this:
:insert
\documentclass[a4paper,12pt]{article}
.
but when I open a new file like this:
vim test.tex
(where test.tex does not exist already)
I get this:
"test.tex" [New File]
Error detected while processing /home/steve/Work/tex_template.txt:
line 2:
E492: Not an editor command: :insertdocumentclass[a4paper,12pt]{article}
Press ENTER or type command to continue
The problem appears to be with the backslash at the start of the line because if I delete the backslash from tex_template.txt the the new file opens up with documentclass[a4paper,12pt]{article} in it. Except I need the backslash because otherwise it's not a tex command sequence.
If you look at :help :insert it says this:
Watch out for lines starting with a backslash, see
line-continuation.
Following the link to line-continuation explains that the \ is a continuation character which can be overridden by passing the C flag to cpoptions.
It should work if you change your template as follows:
:set cpo+=C
:insert
\documentclass[a4paper,12pt]{article}
.
:set cpo-=C
You might want to consider using a snippets engine like vim-snipmate or (my favorite) ultisnips. With those you can insert snippets of text everywhere, not just at the beginning of a file.
As a bonus, these snippets can e.g. substitute variables and even run commands. The following is my snippet (for ultisnips) set to produce the header for a TeX file;
snippet hdr "File header for LaTeX" b
% file: `!v expand('%:t')`
% vim:fileencoding=utf-8:ft=tex
%
% Copyright © `!v strftime("%Y")` ${1:R.F. Smith} ${2:<my#email>}. All rights reserved.
% Created: `!p snip.rv = fcdate(path)`
% Last modified: `!v strftime("%F %T %z")`
$0
endsnippet
This will automatically fill in the file name and the time when the file was last modified. It fills in my name and e-mail with default values but gives me the opportunity to override them. The fcdate function is a piece of Python code that I wrote to retrieve the birthtime of a file.
I have the hdr snippet defined for several different filetypes, and a general one that is used for all other files. If I type hdrtab at the beginning of a line, the appropriate snippet is expanded.

Colorizing the output of :make, :grep, etc., in Vim

When building my application using the :make command in Vim, the output is not colorized. I have configured the makefile to use clang as the C compiler, and when running make outside of Vim or when running :!make, clang's output is colorized. :set makeprg returns makeprg=make, just for reference.
I have the same issue with grep: when running :grep, the output is not colorized; when running :!grep, it is. I have tried using the --color option with :grep, to no avail. :set grepprg returns grepprg=grep -n $* /dev/null.
I've read through VIM Unix commands printed in color and also How to color my vimgrep result patterns. The former seems to have the opposite problem (i.e. :!command output not colorized); the latter doesn't have any alternative to dropping down to the shell, which I don't feel is a "correct" fix for the issue.
The problem is that when Vim runs other commands via :make or :grep, those commands don't get a terminal for their standard output -- in the sense that for them isatty(STDOUT_FILENO) is false -- because Vim is capturing the output on its way to being displayed on the terminal. On the other hand, when you use :!make or :!grep, standard output is just going to the terminal.
Clang by default and grep --color=auto (which is probably how you have it aliased) use the terminalness of stdout to decide whether to colourise their output. This is convenient in that you get colourful output on your terminal but capture just the text when you redirect output to a file -- all without needing to add extra command line options.
So what you want to do is override these commands' usual smarts so that they always colourise their output.
For grep, you can use --color=always when it is run via :grep within Vim:
:set grepprg=grep\ --color=always\ -n\ $*\ /dev/null
and depending on your colour settings and version of grep this will work well enough.
For clang, you can change your Makefile to use clang -fcolor-diagnostics so as to force colourisation or more flexibly add an extra variable to $(CC) that will be overridden when run via :make within Vim:
:set makeprg=make\ EXTRA_CFLAGS=-fcolor-diagnostic
However (at least with clang 3.0 and vim 7.3) you will find that clang's style of colourisation prevents Vim from picking out filenames and line numbers from the diagnostics, so doing this wrecks the advantage of using :make rather than :!make.
You may be able to teach Vim to pick out the filenames etc from the surrounding ANSI escape sequences that do the colourisation by adding more entries to Vim's errorformat option to match the colourised clang-style diagnostics. (And similarly with grepformat if your grep colourisation colours the filenames or linenumbers.)
When you run :grep or :make (as opposed to :!grep or :!make),
the output is not only shown in the terminal,
but also sent to the quick-fix window, from which it is processed.
You can access the quick fix windo using the vim-command :copen.
The quick fix window is essentially a text file that is opened in read-only mode.
Like in any other text file, colors are not supported in the quick fix file.
Instead, they are represented with escape characters like [01;34m.
Therefore, producing colorized output from make (or grep) will
mess up the output as it is shown in the quick-fix window, even if you can get vim to process it,
and send the cursor to the selected error/warning/find message.
The question whether the output is colorized now becomes a little subtle: I suggest that the
terminal output should remain uncolored, but that the quick-fix output should be colorized.
The color scheme in the quick fix window is not defined by any color-indications in the file itself, but in the syntax highlighting
of the quick fix window, defined in the file qf.vim (/usr/share/vim/vim81/syntax/qf.vim on my computer).
The color scheme defined in qf.vim does not add much color to the quick fix window, but the syntax hightlighting
scheme may be extended by creating the file ~/.vim/syntax/after/qf.vim. I use cmake in combination with the
gnu and/or the intel compilers, and get nice-looking results with the following contents for ~/.vim/syntax/after/qf.vim:
syn match qBuilt "Built target *" nextgroup=qTarget
syn match qTarget ".*$" contained
syn match qEnteringLeaving ": \(Entering\|Leaving\) directory *" nextgroup=qdSeparator
syn match qdSeparator "'" nextgroup=qdName contained
syn match qdName "[^']*" contained
syn match qbProgress "\[ *[0-9]*%\]"
syn match qBuild "Building .* object"
syn match qWarn "warning\( *#[0-9]*\|\):"
syn match qError "error\( *#[0-9]*\|\):"
syn match qRemark "remark\( *#[0-9]*\|\):"
hi def link qTarget Constant
hi def link qError Error
hi def link qWarn Error
hi def link qRemark WarningMsg
hi def link qEnteringLeaving Keyword
hi def link qBuild Keyword
hi def link qBuilt Keyword
hi def link qdName Include
hi def link qbProgress Special

Removing a character from 'iskeyword' in vim

In my vimrc, I have included a script (say, otherscript.vim, which I need to include for work reasons) that says:
autocmd FileType php setlocal iskeyword+=$
and I don't want this behaviour. So, sometime later in the vimrc, I say:
autocmd FileType php setlocal iskeyword-=$
(I also tried using set instead of setlocal.) But, when I open a php file, iskeyword still contains the $ symbol in it. I am using vim 7.2. The output of ':verbose set iskeyword' is
iskeyword=#,48-57,_,192-255,$
Last set from /path/to/otherscript.vim
The output of ':scriptnames' is:
...
7: /usr/share/vim/vim72/ftplugin.vim
8: /home/yogeshwer/.vimrc
...
74: /path/to/otherscript.vim
...
Can somebody help me how I can revert the changes to 'iskeyword' made by the other script? Thanks a bunch.
I like to avoid autocmds when I can and use the after directory structure.
$ mkdir -p ~/.vim/after/{ftplugin,syntax,indent}
$ echo 'setlocal iskeyword-=$' >> ~/.vim/after/ftplugin/php.vim
This sets up a basic after directory in your user-specific vim config folder. Whereas ~/.vim/ftplugin/$FILETYPE.vim would be used in lieu of vim's standard $FILETYPE.vim file, files in an after directory get executed after, allowing you to override or change the behavior of your ftplugins, syntax definitions, and indent commands.
As an additional example to show you how these work, I'll include part of my local after/syntax/python.vim file here. I like all the "structural punctuation" of my code to stand out when I read it, so I do this:
syn match pythonParen /[()]/
syn match pythonBrack /[][]/
syn match pythonCurly /[{}]/
hi def link pythonParen Paren
hi def link pythonBrack Brack
hi def link pythonCurly Curly
I've also got an after/indent/php.vim file that was supposed to fix some of the annoying indent issues I ran into with the indent behavior when switching in and out of <?php ?> regions in a template file, but the code is a mess and never really worked in the first place, so I won't reproduce it here. I mention it only to give you an example of what can be done with the after hooks.

Resources