Enable Vim Syntax Highlighting By Default - vim

I know how to turn syntax highlighting on and off in vim by running this in the editor:
:syntax on/off
But I want syntax highlighting to be enabled by default, so I don't have to turn it on every time I run vim.
How do I do this?

Edit your $HOME/.vimrc (Unix/Linux/OSX) or $HOME/_vimrc (Windows) to include the following line:
syntax on
EDIT
If your syntax highlighting doesn't work when you start Vim, you probably don't have a
$HOME/.vimrc or $HOME/_vimrc (known collectively as vimrc from now on). In that case, you have two options:
Create an empty vimrc.
Copy vimrc_example.vim as your vimrc (recommended, thanks #oyenamit). You can find vimrc_example.vim in the runtime directory.
The location of the runtime directory varies between operating systems:
On my system (Arch Linux, and Mac, thanks #totophe), it's in /usr/share/vim/vim73.
On Windows, it's in \Program Files\Vim\vim73.

Also, to highlight the syntax of a Specific File TYPE (or programming language extension), you can use following commands, while file is already opened in Vim and you want to try syntax highlighting on the fly:
:set filetype=php
OR shortcut:
:se ft=php
Above commands will change the syntax-highlighting for currently opened file as it should be for PHP code.

Uncommenting the "syntax on" in vimrc file.
Move to the directory,
cd /etc/vim/
vim vimrc
now search "syntax" and uncomment it. Save it and reopen the file in vim.

For anyone that gets here because of TurnKeyLinux using vim-tiny which doesn't have the syntax module enabled try this article to install full vim
http://www.turnkeylinux.org/forum/support/20140108/solved-bash-command-not-found-after-replacing-package
tl;dr
# apt-get remove vim-tiny
# apt-get install vim
# hash vim
# vim

I also found that this is one of the lessons in vimtutor.
To find it, you can type command vimtutor in your Terminal (I used on Mac), and scroll down to see if there is a lesson called CREATE A STARTUP SCRIPT (for me it was Lesson 7.2), where it describes how to set up an initial vimrc file.

In my $HOME/.vimrc I load a color scheme (solarized) and found that I need to place syntax on after I load the plugin. If it's before loading the plugin it doesn't work.
" this turns syntax highlighting on by default
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'lifepillar/vim-solarized8'
syntax on
" this does not turn syntax highlighting on by default
syntax on
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'lifepillar/vim-solarized8'

To Find the vimrc_example.vim as suggested in answers above
Command : sudo find /usr -iname "vimrc_example.vim"

Related

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

vimrc commands not applying with cygwin and cscope combination on windows 7

I have installed cygwin and cscope on my windows 7 machine. I created a .vimrc file in $HOME directory with the following settings:
syntax on
set hlsearch
set ic
When i ran cscope, selected a file and tried to find some text, it is not highlighting(it get highlighted when using vim/vi on individual file).
I added "alias vi=vim" too in my $HOME/.bashrc file.
Please help me resolving this and understanding why it is happening, as i think cscope should open files using vi/vim.
You can check with the :scriptnames command which ~/.vimrc configuration and plugin files are loaded.
I doubt that csope is going to use your alias. It's probably using the EDITOR or VISUAL environment variables. Make sure those are set to /usr/bin/vim and not /usr/bin/vi. On Cygwin, vi and vim are two different executables. vi is configured to be mostly vi-compatible, while vim has most of the vim features turned on.

vim "syntax on" does not work

Here is my .vimrc
1 syntax on
2 set ts=4
3 set number
4 set smartindent
5 set shiftwidth=4
However, I tried to edit HelloWorld.java and HelloWorld.c. Both have pure regular black font. No any highlighting!
I also tried :syntax on after the vim is open, but no luck.
\>vim -version
VIM - Vi IMproved 7.3 (2010 Aug 15)
\>cat /etc/*-release
openSUSE 11.4 (x86_64)
VERSION = 11.4
CODENAME = Celadon
When you edit the file, are you using
vim filename
This can matter. In some server configurations, if you do vi filename you get vim, but it's a very stripped down version of vim that is very much like the original vi (which does not, among other things, do syntax coloring). On a system configured in this way, if you instead type vim filename, you get the full featured vim.
I just worked through this with a person who was on a server that had the vim-minimal package installed as well as another vim package. I suspect (but did not verify that) the vim-minimal package installed its executable as /bin/vi.
The difference was very clear when you looked at the actual files (i.e. ls -l /bin/vi vs ls -l /usr/bin/vim)--one was about ten times the size. Both of them were actually vim, same version number and everything, but the /bin/vi one was compiled with very few features enabled.
To make it even more confusing:
vi existing.pl
opened the .pl file, gave no syntax coloring
vi [enter]
gave the vim splash screen, and from there
:e existing.pl
opened the file with syntax coloring on.
A comment from Jan Wilamowski suggests checking by doing:
vi --version
If that shows that the syntax feature was not compiled in, try
vim --version
and see if it is compiled in there.
You'll need to install the vim-data package on openSUSE for vim syntax colouring to work.
Sounds strange, I know that this is not pulled in by default with the vim package but AFAIK it's for people who want to create tiny base installs.
Package vim-data contains the runtime files.
Also make sure your remote environment has an appropriate TERM variable set TERM=screen-256color, TERM=xterm, TERM=xterm-256color should all work just fine with ssh and ssh with screen/tmux.
If all above have been done and you see some underlines and bold instead of actual colors... this might work for you:
export TERM=xterm-color
in your .vimrc, I don't see filetype setting. you could try to add:
filetype plugin indent on
to your vimrc.
if you don't have set nocp, add this line too.
if you read :h filetype
:filetype on
Each time a new or existing file is edited, Vim will try to recognize the type
of the file and set the 'filetype' option. This will trigger the FileType
event, which can be used to set the syntax highlighting, set options, etc.
For some strange reason on MacOS, 'syntax on' must be the first line in your .vimrc file. The line appears to be ignored if placed elsewhere in the file.
One item not mentioned is :set syntax=<type>, e.g. :set syntax=markdown.
This has been successful in instances where other techniques above were not.

Vim commands :Explore :Sexplore :Hexplore doesn't work in cygwin

3.3 in cywing 2.721, the installation was made using cywing, every thing works but when I try to use the following command.
:Explore
vim said E492: Not an editor command
also neither :Sexplore or :Hexplore works.
is there any way to activate this functionality?
This is in machine with windows xp.
Well I solved reading the
:help usr_01.txt
It said that is necessary to run this command !cp -i $VIMRUNTIME/vimrc_example.vim ~/.vimrc inside vim, it just copy a .vimrc to home user.
I close and opened vim and :Explore, Hexplore, Vexplore worked.
I had the same problem as well. Since it's a fresh install of Vim you don't have a .vimrc (or _vimrc on Windows). A vimrc is Vim's configuration file, and once you have one Vim will no longer try to be compatible with Vi (usually what you want).
So all you have to do is create a vimrc file and you should be ready to :Explore all you want. Below are locations you can put your vimrc file, and the filename to use for it. (depending on your system)
Unix/Linux/OSX: $HOME/.vimrc or $HOME/.vim/vimrc
MS-Windows: $HOME/_vimrc, $HOME/vimfiles/vimrc
or $VIM/_vimrc
sources:
* :help 'compatible'
* :help 'vimrc'
I also recommend following atomsfat's answer as well to give you a simple vimrc to start out with.
Those commands are provided by the netrw plugin. Check :scriptnames to see if that plugin is loaded.

How to turn-off a plugin in Vim temporarily?

I have multiple plugins in Vim and some of them modify the default behavior of Vim. For example I use Vimacs plugin, which makes Vim behave like emacs in the insert mode alone. Sometime I want to turn off the Vimacs plugin without moving the vimacs.vim out of the plugins directory. Is there a way to do it?
You can do this if you use a plugin manager like Vundle or Pathogen, which will keep the plugin in its own directory underneath the ~/.vim/bundle/ directory.
In that case, just find out the runtimepath of the vimacs plugin with the following command:
set runtimepath?
Let's say it's ~/.vim/bundle/vimacs.
Then, put this command in your .vimrc:
set runtimepath-=~/.vim/bundle/vimacs
To load vimacs, just comment that line out and relaunch Vim (or source your .vimrc).
See which variable vimacs check on start. On the begin of the script file find something Like if exists('g:vimacs_is_loaded").... Then set this variable in your .vimrc or while start vim with vim --cmd "let g:vimacs_is_loaded = 1".
In case you are using pathogen, this post gives a better answer, in my opinion. Since I have frequent need to disable snippets when using latex, also added this in my ~/.config/ranger/rc.conf:
map bs shell vim --cmd "let g:pathogen_blacklist = [ 'ultisnips', 'vim-snipmate' ]" %f
This way, whenever I want to open a file with snippets disabled, it is easy.

Resources