- Start by making a
*.vimfile 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
functionkeyword following the function name e.g
function Test()
echom "Test!"
endfuctionPlease 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 onenablescindentoption.this not only enables C and C alike languages indentation, but also includes other filetypes detection.:let b:did_indent = 1will 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 = 1which will disable any previous indentation for this file.
autoindentboolean (default off) copy indent from the current line when starting a new line. It uses the indent from the previous linesmartindentis likeautoindentbut also recognizes some C syntax to increase/reduce the indent where appropriatecindentworks 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 withshiftwidthvalue, default [8].tabstopdefault [8]. It is not recommended to change default value. Usesofttabstopinsteadsofttabstopset 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].expandtabboolean (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.
indentexpris the most flexible of the other three described in basic file indentation:- when
indentexpris 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
-1is 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. -
setlocalSets the value of a variable, but only within the current scope. -
indentexprVim calls the function identified by this option to calculate the required indent for a line. -
indentkeysIf 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. -
shiftwidthThe 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.
letcommand assigns a value to a variable e.glet {variable} = {expression}v:lnum(lnumber-variable) line number for the indentexpr. Thevprefix means variable predefined by Vimb:did_indentbuffer-local variable indicates that current buffer already has script-based indenting enabled
usr_25.txtsection 25.3usr_30.txtsection 30.5 Tabs and spacesusr_41.txtWrite a Vim scriptindent.txtoptions.txteval.txtExpression evaluationhelp indent():help internal-variablesexplains 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