- Start by making a
*.vim
file with your vim code and 'execute' it by opening vim and 'sourcing' your script file in e.g:source /path/to/your/script.vim
- To define a function in Vim use
function
keyword following the function name e.g
function Test()
echom "Test!"
endfuction
Please note that in Vim function name must start with a capital letter !
- You can overwrite previously defined or an built-in function using
!
operator e.g
function! Test()
echom "This function overwrites previously defined Test()"
endfuction
-
:onoremap b /return<cr>
The first bit of the mapping is some what irrelevant, but the second part says: from/
which is from cursor to the "return" word, which can really be anything. -
b:
for local-buffer -
<cr>
is carriage return: is a control character or mechanism used tto reset a device's position to the beginning of a line of text
In the nutshell file(s) in this directory enables filetype detection. Good explanation of ftdetect folder
is here. Also use :help ftdetect
for source
of information.
I am still yet to comprehend subtle difference between ftplugin and indent directories (files)... My understanding of the ftplugin directory is: It will hold all files that are related to a particular plugin. You could place indentation file into this directory as well, but better practice is to place indentation file into its own directory a.k.a indent. I understand you can have you indentation code in the ftplugin file and it will work just fine.
your syntax file goes into this directory
This directory holds your indent.vim file
In summary indent.vim
file returns a numerical value that tells Vim to indent, un-indent or keep-the-same indentation level of the current line by that number of spaces.
There are four main methods available for indentation, each one overrides the previous if it is enable. Three explained below and 'indentexpr' explained in indent file section.
filetype indent on
enablescindent
option.this not only enables C and C alike languages indentation, but also includes other filetypes detection.:let b:did_indent = 1
will disable indentation. You can create a file in the~/.vim/indent/
directory with your specific file name e.g bds.vim and includelet b:did_indent = 1
which will disable any previous indentation for this file.
autoindent
boolean (default off) copy indent from the current line when starting a new line. It uses the indent from the previous linesmartindent
is likeautoindent
but also recognizes some C syntax to increase/reduce the indent where appropriatecindent
works more cleverly than the other two and is configurable to different indenting styles
>>
use this key to increase the amount of indent in a line. The amoutn of indent added is specified withshiftwidth
value, default [8].tabstop
default [8]. It is not recommended to change default value. Usesofttabstop
insteadsofttabstop
set this value to the desired number. This will be used when you press key. Default for key istabstop
, which is set to default value [8].expandtab
boolean (default off) By setting its 'on' spaces will be used instead of tabs. This options allowsyou to use key but have no tabs in your file only spaces.
In order to write your own indent file, it must set the indentexpr
options.
indentexpr
is the most flexible of the other three described in basic file indentation:- when
indentexpr
is non-empty this method overrides the other ones - It evaluates an expression to compute the indent for each line
- It is used when a new line is created
- The expression must return the number of spcaes worth of indent. If
-1
is returned then current line indent is kept (this means 'autoindent' is used for the indent)
- when
-
getline({lnum})
Returns a string containing the given line's contents. -
indent({lnum})
Returns a number of the indentation level of the specified line. The indent is counted in spaces. -
prevnonblank({lnum})
Returns a number corresponding to the line number of the first line (including and going backwards from the specified line) that isn't merely blank. -
exists("identifier")
Returns true if this identifier already exists. Handy for checking that we're nott about to overwrite something.t about to overwrite something. -
setlocal
Sets the value of a variable, but only within the current scope. -
indentexpr
Vim calls the function identified by this option to calculate the required indent for a line. -
indentkeys
If a line contains any of the strings contained in this option, Vim calls the indent function. In other words, this option dictates when the indentation process is performed. -
shiftwidth
The amount of columns (i.e. the number of spaces, or the equivalent number of tabs) corresponding to one indent level. Often set by the user to suit their own taste.
let
command assigns a value to a variable e.glet {variable} = {expression}
v:lnum
(lnumber-variable) line number for the indentexpr. Thev
prefix means variable predefined by Vimb:did_indent
buffer-local variable indicates that current buffer already has script-based indenting enabled
usr_25.txt
section 25.3usr_30.txt
section 30.5 Tabs and spacesusr_41.txt
Write a Vim scriptindent.txt
options.txt
eval.txt
Expression evaluationhelp indent()
:help internal-variables
explains what are different prefixs to the variable mean
- Thoughtbot - Syntax plugin
- usevim - Syntax highlight
- Vim wiki - Syntax highlight
- Vim wiki - Indenting source code
- IBM - Vim scripting
- How to write indent script vim
- Learn vim the hard way
- Regex quick start
- Regular expression 101
- GO language - syntax highlight plugin
- Interesting article that points to useful Vim scripting resources