I've been using Vim as my text editor, but now that I'm using it to write Python code, I'd like it to hightlight the syntax like this:
How do I turn on syntax highlighting in Vim? How do I get it to stay on by default for programming files?
Turning On Syntax Highlighting:
Open the file that you'd like to have syntax highlighting enabled for with vim filename.py.
Press esc, then type :syntax on and hit enter.
Syntax highlighting should now be enabled!
Note: This will only turn on syntax highlighting temporarily. In other words, if you quit working on that file and open it again, or open a different file with vim, syntax highlighting will be off by default. If you'd like to make syntax highlighting permanent, you need to do some extra steps.
Enabling Syntax Highlighting by Default:
Change directory to your home directory by typing cd into the terminal.
Type vim .vimrc, which will edit the .vimrc file if you have it, or create it if you don't.
Append syntax on to the file as a new line, then hit esc, type :wq, and hit enter.
Syntax highlighting will now be enabled by default for files ending in common programming language extensions such as .py.
If you would like to turn off the syntax highlighting for a particular file, you can do that on a case by case basis.
Turning Off Syntax Highlighting for a Specific File:
Open the file that you'd like to have synta highlighting disabled for with vim filename.py.
Press esc, then type :syntax off and hit enter.
Syntax highlighting should now be disabled!
Related
This happens even with an empty .vimrc
When on a file with a .html extension, if it is currently in this state:
<p>
t
</p>
and I put my cursor over the "t", press a (append)
<p>
t_
</p>
and then type "{", the line will indent left and result in this:
<p>
t{_
</p>
with my cursor still in insert mode at "_"
I've tried looking up "indent" and "{" in various vim help files but I can't find anything on why it automatically indents the line to the left.
Is there any way to disable this auto left indent?
Your indenting is being handled by a function called HtmlIndent(). If you open vim with vim --clean testIndent.html from the command line, then type :set indentexpr? in vim and hit enter, you will see indentexpr=HtmlIndent(). See :h indentexpr to read more about what this setting means. As for what the function does, you would need to look at where it is defined. You can type :e $VIMRUNTIME/**/indent/html.vim into vim and (before hitting enter) hit tab. Vim should automatically fill in the location of vim's html indent file. For me on Ubuntu with vim 8.2, this is at /usr/local/share/vim/vim82/indent/html.vim. The HtmlIndent() function is defined there.
That is the first part of the answer. The second part answers why your indenting is triggered by typing the { character. HtmlIndent() determines what the indent of a line should be, but the indentkeys setting triggers a line's proper indent to be evaluated and set when certain keys are typed. See :h indentkeys. If you open vim again with vim --clean testIndent.html and type :set indentkeys?, you will see something like indentkeys=o,O,<Return>,<>>,{,},!^F. The { character in that setting is causing vim to evaluate, through HtmlIndent(), what it believes is the proper indent for the line you are on every time you type {.
Given this, you have several options. You could look for someone who has made their own addition to vim's html indenting settings. You could simply disable this automatic indenting. You could attempt to modify vim's HtmlIndent() function. One very simple option for you is to remove the { character from the indentkeys setting. You can do this with :set indentkeys-={. Then do :set indentkeys? again to verify that { has been removed. You could remove } the same way. Note that this will only prevent vim from automatically re-indenting a line every time you type the {, } characters. It will not actually change vim's opinion on what that line's indent should be, so in other cases where vim evaluates and sets a line's indent, it will still do so in a way you disagree with.
I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.
When editing a text in markdown, I don't want to highlight bibliography entries. This can be achieved with the following command:
:syn match CitNoSpell '\[#[^[:space:]]\+\]' contains=#NoSpell
However, if I enter this command to .vimrc, it is ignored. I assume that is because spell file is loaded after vimrc has been read, and this definition is not kept.
How should I force vim to ignore this pattern? I prefer it to stay in .vimrc, as I synchronize the file across multiple systems, but another solution would also be welcome.
As the ~/.vimrc is loaded first (before any files), the syntax of an opened file is set only later, and syntax scripts :syntax clear any existing syntax stuff, including your definition.
The right place for your customization would be the :help after-directory; i.e. ~/.vim/after/syntax/markdown.vim, as this will be sourced after $VIMRUNTIME/syntax/markdown.vim.
If you insist on configuring this within your ~/.vimrc, you can try the following autocmd, which has to be put somewhere after :syntax on:
autocmd Syntax markdown syn match CitNoSpell ...
PS: For consistency, as you're tweaking the Markdown syntax, your added syntax group should also start with the syntax name, i.e. markdownCitNoSpell.
it seems my vim encounters a problem, and it cannot recognize its own filetype, when i edit ~/.vimrc, the syntax isn't highlight while i've set 'syntax on', but when i edit .c file, it has syntax highlight. And another problem is that it seems my VIM is running inside a shell when i edit ~/.vimrc, i can even sroll to my previous commands! And when i edit other file type, it doesn't have the problem. here is the screenshot:
This has bugged me for a long time, and try as I might I can't find a way round it.
When I'm editing text (specifically latex, but that doesn't matter) files, I want it to auto-wrap at 80 columns. It does this, except if I happen to be in the middle of a parenthetical clause, it indents the text which is very annoying. For example, this works fine
Here is some text... over
two lines.
but this doesn't
Here is some text... (over
two
lines
If anyone can tell me how to turn this off (just for text/latex files) I'd be really grateful. Presumably it has something to do with the fact that this is desired behaviour in C, but I still can't figure out what's wrong.
:set nocindent
The other options do nothing, and the filetype detection doesn't change it.
There are three options you may need to turn off: set noai, set nosi, and setnocin (autoindent, smartindent, and cindent).
This may be related, when pasting from gui into terminal window, vim cannot distinguish paste modes, so to stop any odd things from occuring:
set paste
then paste text
set nopaste
I had similar issues trying to paste xml text, it would just keep indenting. :)
gvim, the gui version of vim, can detect paste modes.
You can have a look at the autoindent option :
autoindent - ai
Copy indent from current line when starting a new line (typing
in Insert mode or when using the "o" or "O" command). If you do not
type anything on the new line except and then type or
, the indent is deleted again. When autoindent is on,
formatting (with the "gq" command or when you reach 'textwidth' in
Insert mode) uses the indentation of the first line. When
'smartindent' or 'cindent' is on the indent is changed in specific
cases. The 'autoindent' option is reset when the 'paste' option is
set. {small difference from Vi: After the indent is deleted when
typing or , the cursor position when moving up or down is
after the deleted indent; Vi puts the cursor somewhere in the deleted
indent}.
From the official Vim documentation
filetype plugin indent on
This switches on three very clever
mechanisms:
Filetype detection. Whenever you start editing a file, Vim will try to
figure out what kind of file this
is. When you edit "main.c", Vim will
see the ".c" extension and
recognize this as a "c" filetype.
When you edit a file that starts with
"#!/bin/sh", Vim will recognize it as
a "sh" filetype. The filetype
detection is used for syntax
highlighting and the other two
items below. See |filetypes|.
Using filetype plugin files Many different filetypes are edited with
different options. For example,
when you edit a "c" file, it's very
useful to set the 'cindent' option to
automatically indent the lines. These
commonly useful option settings are
included with Vim in filetype plugins.
You can also add your own, see
|write-filetype-plugin|.
Using indent files When editing programs, the indent of a line can
often be computed automatically.
Vim comes with these indent rules for
a number of filetypes. See
|:filetype-indent-on| and
'indentexpr'.
:set noai
sets no auto indent tt may be smartindent though. Check out the doc and see if you can find something more
http://www.vim.org/htmldoc/indent.html