Difference between revisions of "vim"

From Free Pascal wiki
Jump to navigationJump to search
(→‎syntax highlighting: add info for // comment)
(→‎tab replacement: add another note)
Line 41: Line 41:
 
Some people prefer hitting the ↹ key to be directly translated to a number of space characters.
 
Some people prefer hitting the ↹ key to be directly translated to a number of space characters.
 
The <tt>vim</tt> supports this, confer some <syntaxhighlight lang="vim" inline>:help</syntaxhighlight> for details.
 
The <tt>vim</tt> supports this, confer some <syntaxhighlight lang="vim" inline>:help</syntaxhighlight> for details.
 +
The setting
 +
<syntaxhighlight lang="vim" inline>let pascal_no_tabs=1</syntaxhighlight>
 +
will highlight any tab characters as an error.
  
 
Confer <syntaxhighlight lang="vim" inline>:help retab</syntaxhighlight> if you want to <tt>expand(1)</tt>.
 
Confer <syntaxhighlight lang="vim" inline>:help retab</syntaxhighlight> if you want to <tt>expand(1)</tt>.

Revision as of 14:41, 15 June 2021

vim is an editor well-suited to program.

configuration

The vim editor experience can be enhanced for editing Pascal source code files.

syntax highlighting

Files matching *.pas and *.dpr are automatically recognized. Additionally, *.p files are recognized as Pascal source code files, but only if the first 10 non-empty lines contain keywords typical of Pascal.

If the automatisms fail, e. g. for *.inc files, syntax highlighting can be chosen explicitly:

set filetype=pascal

Multiple syntax highlighting definitions can be combined, for example:

set filetype=pascal.doxygen

If you want more built-in data types to have special color, or even the single-line comment to be recognized as such, you will need

let g:pascal_fpc=true

and/or

let g:pascal_delphi=true

margin

The ex-command

set colorcolumn=80

will change the background of the 80th column. Depending on the tabstop you may reach it earlier or later. This is just a visual guidance.

The textwidth variable will actually thwart insertion of lines longer than a certain width.

tabstops

Non-permanent indentation, just for aesthetic reasons, can be a hairy subject. The easiest and most straightforward solution is to use actual tab characters and set their display width with:

set tabstop=5

vim supports indentation using space characters. The indentation level can be recognized by counting spaces at the beginning of lines. How many spaces one indentation level occupies on screen can altered dynamically. Since proper configuration involves interaction of several settings though, we refer to the documentation for more details.

tab replacement

Some people prefer hitting the ↹ key to be directly translated to a number of space characters. The vim supports this, confer some :help for details. The setting let pascal_no_tabs=1 will highlight any tab characters as an error.

Confer :help retab if you want to expand(1).

white characters

Using

set listchars=space:⍽

and then activating the “list mode” :set list, you can display all space characters with a custom symbol. This uses a different color than the actual character used.

navigation

The % key allows you to jump matching parenthesis and curly braces. However, extra precautions have to be taken for begin and end. You have enable the matchit plugin (cf. :help matchit-install):

packadd! matchit

folding

Confer :help folding.

The original editor of this article recommends manual folds using markers:

set foldmethod=marker

This will generate folds on markers. The default markers are {{{ and }}} (confer :help foldmarker). Thus, the following code

// procedure foo(integer, Boolean) {{{
procedure foo(const bar: integer; const able: Boolean);
begin
end;
// }}}

will look like

+--  5 lines: procedure foo(integer, Boolean) ----------------------------------

when folded.

Instead of the default triple curly braces, you can honor Lazarus’ IDE directives:

set foldmarker={%region},{%endregion}

Note, fold markers are static strings. Regular expressions are not supported, thus a fold marker definition like this one is case-sensitive. Fold markers appearing in string literals are still recognized as fold markers. As a workaround you can split the string, of course: 'The IDE directive {%reg' + 'ion} sucks.'

see also