Difference between revisions of "vim"

From Lazarus wiki
Jump to navigationJump to search
(Alextpp moved page vim to VIM: proper casing)
Tag: New redirect
 
(overwrite double-redirects [lack of permissions], expand/correct content)
Tag: Removed redirect
Line 1: Line 1:
#REDIRECT [[VIM]]
+
<tt>vim</tt> is an [[source code editor|editor]] well-suited to program.
 +
 
 +
== configuration ==
 +
The <tt>vim</tt> editor experience can be enhanced for editing [[Pascal]] source code files.
 +
 
 +
=== syntax highlighting ===
 +
Files matching <syntaxhighlight lang="text" inline>*.pas</syntaxhighlight> and <syntaxhighlight lang="text" inline>*.dpr</syntaxhighlight> are automatically recognized.
 +
Additionally, <syntaxhighlight lang="text" inline>*.p</syntaxhighlight> 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 <syntaxhighlight lang="text" inline>*.inc</syntaxhighlight> files, syntax highlighting can be chosen explicitly:
 +
<syntaxhighlight lang="vim">set filetype=pascal</syntaxhighlight>
 +
 
 +
Multiple syntax highlighting definitions can be combined, for example:
 +
<syntaxhighlight lang="vim">set filetype=pascal.doxygen</syntaxhighlight>
 +
 
 +
=== margin ===
 +
The ex-command
 +
<syntaxhighlight lang="vim">set colorcolumn=80</syntaxhighlight>
 +
will change the background of the 80<sup>th</sup> column.
 +
Depending on the <syntaxhighlight lang="vim" inline>tabstop</syntaxhighlight> you may reach it earlier or later.
 +
This is just a visual guidance.
 +
 
 +
The <syntaxhighlight lang="vim" inline>textwidth</syntaxhighlight> 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:
 +
<syntaxhighlight lang="vim">set tabstop=5</syntaxhighlight>
 +
 
 +
<tt>vim</tt> 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 <tt>vim</tt> supports this, confer some <syntaxhighlight lang="vim" inline>:help</syntaxhighlight> for details.
 +
 
 +
Confer <syntaxhighlight lang="vim" inline>:help retab</syntaxhighlight> if you want to <tt>expand(1)</tt>.
 +
 
 +
=== white characters ===
 +
Using
 +
<syntaxhighlight lang="vim">set listchars=space:⍽</syntaxhighlight>
 +
and then activating the “list mode” <syntaxhighlight lang="vim" inline>:set list</syntaxhighlight>, you can display all space characters with a custom symbol.
 +
This uses a different color than the actual character used.
 +
 
 +
=== navigation ===
 +
The [[%|<syntaxhighlight lang="text" inline>%</syntaxhighlight>]] key allows you to jump matching parenthesis and curly braces.
 +
However, extra precautions have to be taken for [[Begin|<syntaxhighlight lang="pascal" inline>begin</syntaxhighlight>]] and [[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]].
 +
You have enable the <syntaxhighlight lang="vim" inline>matchit</syntaxhighlight> plugin (cf. <syntaxhighlight lang="vim" inline>:help matchit-install</syntaxhighlight>):
 +
<syntaxhighlight lang="vim">packadd! matchit</syntaxhighlight>
 +
 
 +
=== folding ===
 +
Confer <syntaxhighlight lang="vim" inline>:help folding</syntaxhighlight>.
 +
 
 +
The [[User: Kai Burghardt|original editor]] of this article recommends manual folds using markers:
 +
<syntaxhighlight lang="vim">set foldmethod=marker</syntaxhighlight>
 +
This will generate folds on markers.
 +
The default markers are <syntaxhighlight lang="text" inline>{{{</syntaxhighlight> and <syntaxhighlight lang="text" inline>}}}</syntaxhighlight> (confer <syntaxhighlight lang="vim" inline>:help foldmarker</syntaxhighlight>).
 +
Thus, the following code
 +
<syntaxhighlight lang="pascal">
 +
// procedure foo(integer, Boolean) {{{
 +
procedure foo(const bar: integer; const able: Boolean);
 +
begin
 +
end;
 +
// }}}
 +
</syntaxhighlight>
 +
will look like
 +
<syntaxhighlight lang="text">
 +
+--  5 lines: procedure foo(integer, Boolean) ----------------------------------
 +
</syntaxhighlight>
 +
when folded.
 +
 
 +
== see also ==
 +
* Wikibook: [https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim Learning the <tt>vi</tt> editor]
 +
 
 +
[[Category: Tools]]
 +
[[Category: Tutorials]]

Revision as of 13:11, 2 February 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

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.

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.

see also