Why am I having trouble to embed "set syntax" into a file? - vim

I have read the help and examples, but still don't know what I'm doing wrong.
When I manually enter :set syntax=javascript, I get the syntax highlighting I want.
But when I edit the first line of my file to read:
/* vim: set syntax=javascript: */
nothing happens.
When I split that line into 3 lines:
/*
# vim: set syntax=javascript:
*/
I get some limited syntax highlighting, not as good as with manual command. I can write syntax=anything there and it makes no difference.
(Vim version 7.4.160, Centos 7)

Lines of the format:
/* vim: set syntax=javascript: */
are called modelines in Vim. In order for Vim to process them, you must have the modeline option toggled on.
To see if modeline is enabled, run this ex command:
:set modeline?
If it returns nomodeline, you can enable it by adding the following in your ~/.vimrc:
set modeline
Vim will look for a modeline in the first 5 lines of the file, by default. You can set the number of lines that will be searched with e.g.:
set modelines=10
See :help modeline for details.

Related

Inconsistent initialization

MacVim 8.0:
If there is no .vimrc file, set shows:
:set
--- Options ---
autoindent scroll=18 ttymouse=xterm2
helplang=en showmatch wrapmargin=10
langmenu=none ttyfast nowrapscan
backspace=indent,eol,start
fileencodings=ucs-bom,utf-8,default,latin1
pythondll=/usr/local/Frameworks/Python.framework/Versions/2.7/Python
pythonhome=/usr/local/Frameworks/Python.framework/Versions/2.7
If I create an empty .vimrc file, set shows:
set
--- Options ---
helplang=en scroll=18 ttymouse=xterm2
langmenu=none ttyfast
backspace=indent,eol,start
fileencodings=ucs-bom,utf-8,default,latin1
pythondll=/usr/local/Frameworks/Python.framework/Versions/2.7/Python
pythonhome=/usr/local/Frameworks/Python.framework/Versions/2.7
This differs from the first case in that autoindent, showmatch, and wrapscan are missing. Why is there a difference? These options do not seem to be set in /Applications/MacVim.app/Contents/Resources/vim/vimrc.
Starting vim with -u file is supposed to use file as the startup file. So if I remove the empty .vimrc--so now I have no .vimrc--and use -u emptyfile, where emptyfile is an empty file, I would expect that set would show what is listed immediately above. However, set shows something very different:
:set
--- Options ---
helplang=en scroll=18 ttyfast ttymouse=xterm
fileencodings=ucs-bom,utf-8,default,latin1
iskeyword=#,48-57,_,192-255
Why is this different from the previous case?
(Why do I care? Because I'm trying to figure out why an initialization file executes properly when it's .vimrc, but not when I run it with -u.)
Without a vimrc at any of the expected locations, Vim sources:
$VIM/vimrc
$VIMRUNTIME/defaults.vim
...
This is new in Vim 8.0. Before that version, Vim would only source $VIM/vimrc in this scenario. The reasoning behind that new feature was allegedly to provide more useful defaults to infrequent users without asking them to write their own vimrc.
See :help defaults.vim.
With a vimrc at one of the expected locations (assuming ~/.vimrc but it can be ~/.vim/vimrc), Vim sources:
$VIM/vimrc
$HOME/.vimrc
...
This is the optimal scenario for frequent users, experienced or not: you get the basic behavior plus all your fancy stuff.
When pointing Vim to a specific vimrc with -u, Vim only sources that specific vimrc.
This is the absolutist scenario that gives you complete control over Vim's settings. I wouldn't recommend it to inexperienced users.
I have found part of the answer. (I'll post it here for anyone with a similar question, but I don't expect upvotes for either the question or the answer.)
help initialization says, among other things:
If Vim was started with "-u filename", the file "filename" is used.
All following initializations until 4. are skipped. $MYVIMRC is not
set.
Most of what is between that point and 4. was not relevant to my situation; these are steps that occur only in special circumstances. However, one item between that point and 4. is:
b. For Unix, MS-DOS, MS-Windows, OS/2, VMS, Macintosh, RISC-OS and Amiga
the system vimrc file is read for initializations. The path of this
file is shown with the ":version" command.
So the system initialization file is not run when -u is used. (This still doesn't fully answer my first question, which mentions the system initialization file on my system.)

Make .vimrc changes being picked up by Vim only and for me only

I have a $HOME/.vimrc file which I have configured for many options like below but not limited to.
set foldmethod=indent
set foldnestmax=10
set mouse=a
set number
These all work fine if I open a file with vim however if I open with vi or view the file none of the config work, which is fine but I get errors for some.
line 104:
E538: No mouse support: mouse=a
line 205:
E518: Unknown option: foldmethod=indent
Press ENTER or type command to continue
After searching quite a while I was able to remove some of these.
if has('mouse')
set mouse=a
endif
silent! set foldmethod=indent
However this is just suppressing them. And the linenumbers are still being displayed with vi or view which look pretty bad and many people login to the box will be suddenly mystified by the yellow line numbers.
How to make vi not take up these config.
I powerbroker to the linux box. Can I make .vimrc setting just for me.
vi and vim are the same executable.
I'd suggest you change the name of your vimrc:
$ mv ~/.vimrc ~/.myvimrc
and start Vim with:
$ vim -Nu ~/.myvimrc
You could add an alias to ~/.bashrc (or whatever works in that system) to ease your workflow:
alias myvim='/usr/bin/vim -Nu ~/.myvimrc'
As vi does not support all options of vim, What I do is to set alias for vi to vim in my rc file
alias vim='vim -p'
alias vi='vim -p'
The -p is not really required. but I kinda like tabbing enabled by default on vim. So every time, if you hit vi or vim, it behaves the same.
You can make this permanent, if you like, by adding the lines to ~/.bashrc, assuming you are using bash. You need to source ~/.bashrc to take immediate effect without logging off.

How to turn on syntax highlighting in VIM 7.3 OSX

System = OSX 10.9.4
I am trying to turn on syntax highlighting in vim while using the terminal. However, I am unable to get it to work properly.
Things I've tried:
located the vimrc file and added the following code:
set ai " auto indenting
set history=100 " keep 100 lines of history
set ruler " show the cursor position
syntax on " syntax highlighting
set hlsearch " highlight the last searched term
filetype plugin on " use the file type plugins
Located vimrc under directory:
cd /usr/share/vim/
The interesting thing is that once I add the code to the vimrc using vim, followed by exiting (x), and re-opening the file again, syntax is correctly highlighted in the vimrc.
However, when I try to make a new vim file called "test", copy the same code, save and exit. Re-open it, the syntax is not highlighted at all.
It appears that syntax highlighting only works when I open the actually vimrc file---and not when I try to create a new one or open another file that should have syntax highlighting.
I've also tried to create a .vimrc (exact copy) under the ~/ (directory). No success.
Made a new file called "test" and tried turning it on while active as well:
vim test
"then within vim"
:syntax on
I am really confused as to why this partially works.
Any help is much appreciated.
Cheers!
p.s. I have followed these instructions as well from: http://geekology.co.za/article/2009/03/how-to-enable-syntax-highlighting-and-other-options-in-vim
*I am aware of macvim, but would like a solution for the native vim in terminal. Thanks.
NEVER do anything in $VIM as it will work only by luck, cause unexpected behaviors and likely be overwritten next time Vim is updated.
What you have put in /usr/share/vim/vimrc should be in ~/.vimrc.
filetype on and syntax on are the bare minimum you need in your ~/.vimrc for syntax highlighting to work.
$ vim ~/.vimrc gives you syntax highlighting because the file is recognized by Vim as a vim file. Filetype detection is mostly dependent on file extensions so you can't expect any syntax highlighting in a file called test.
$ vim test.py, on the other hand, should give you syntax highlighting.
If the filetype is not detected, you can force it with :set filetype=python.
You most probably want to enable indentation along with syntax highlighting, so add these to lines to ~/.vimrc
filetype plugin indent on
syntax on
Steps with screenshots can be found here
http://osxandiosdaily.com/how-to-enable-vim-syntax-highlighting-on-mac-osx/
Inside of your file, enter command mode and enter the command
:syntax on

Set working in file but not in .vimrc

When I put set scroll=10 in my .vimrc it does not work, but it does work when I enter the command in command mode.
I have many other commands in my .vimrc, what reason could there be for this set to not be getting through? It is the last line in my .vimrc.
:verbose set scroll?
should tell you which plugin script has last modified that option. If you don't find the culprit this way, you could workaround by setting it at the last possible moment by putting this in your ~/.vimrc
:autocmd VimEnter * set scroll=10

Vim: `set formatoptions` being lost?

I've got set formatoptions=cqn in my vimrc, but for some reason it doesn't stick. It seems like Vim is reverting to the default (fo=tcq) at some point… But I can't figure out why. Running -V100/tmp/log just gives me:
formatoptions=tcq
Last set from ~/.vimrc
With no useful context.
So, is there any way to make formatoptions stick? Or do I just need to create an autocmd to reset it each time a new file is loaded?
Edit
Using :verbose set formatoptions shows this:
formatoptions=tcq
Last set from ~/.vimrc
However, the only reference to fo or formatoptions in my ~/.vimrc is set formatoptions+=cqn.
This behavior is because of C file plugin in VIM. Since file plugin is loaded after loading .vimrc, the settings in .vimrc are overwritten.
The solution given by David Wolever seems to be the best option.
Add the following line in .vimrc:
autocmd BufNewFile,BufRead * setlocal formatoptions+=cqn
...instead of the normal set formatoptions command.
I ran across this problem too. I had project-specific configurations something like
autocmd BufRead,BufNewFile project/*.c setlocal formatoptions-=cro
However, set fo? showed formatoptions=croql. Turns out, I needed BufWinEnter instead of BufRead:
After a buffer is displayed in a window. This
can be when the buffer is loaded (after
processing the modelines) or when a hidden
buffer is displayed in a window (and is no
longer hidden).
Does not happen for :split without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.
So this works
autocmd BufWinEnter,BufNewFile project/*.c setlocal formatoptions-=cro
According to the vim documentation on formatoptions:
NOTE: This option is set to the Vi
default value when 'compatible' is
set and to the Vim default value when
'compatible' is reset.
So if the value of compatible is changing along the way, that could be causing the issue you're seeing.
I does sound like some file either sourced from your .vimrc or plugins are changing that value.
Something to try to pinpoint it is
start vim without sourcing anything, use
vim -u NONE
Using NORC skipps .vimrc but loads plugins
Check :help --noplugin to read about various startup-options that controls the sourcing.
--noplugin Skip loading plugins. Resets the 'loadplugins' option.
{not in Vi}
Note that the |-u| argument may also disable loading plugins:
argument load vimrc files load plugins ~
(nothing) yes yes
-u NONE no no
-u NORC no yes
--noplugin yes no
Perhaps this might be useful as well (from help: :set):
When 'verbose' is non-zero, displaying an option value will also tell where it
was last set. Example: >
:verbose set shiftwidth cindent?
< shiftwidth=4 ~
Last set from modeline ~
cindent ~
Last set from /usr/local/share/vim/vim60/ftplugin/c.vim ~
perhaps... :-)
Edit
Are you using compatible? From help: formatoptions
NOTE: This option is set to the Vi default value when 'compatible' is
set and to the Vim default value when 'compatible' is reset.
Found in /usr/share/vim/vim74/ftplugin/vim.vim:
" Set 'formatoptions' to break comment lines but not other lines,<br>
" and insert the comment leader when hitting <CR> or using "o".<br>
setlocal fo-=t fo+=croql
Remove it. Then all things done.

Resources