Difference between revisions of "vim"

From Free Pascal wiki
Jump to navigationJump to search
(→‎tab replacement: add another note)
(use {{keypress}})
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<tt>vim</tt> is an [[source code editor|editor]] well-suited to program.
 
<tt>vim</tt> is an [[source code editor|editor]] well-suited to program.
 +
 +
== basic usage ==
 +
The most important difference to many other editors is that <tt>vim</tt> constantly has a ''mode''.
 +
The mode is like the caps lock key on your keyboard:
 +
By default you are in “lowercase mode” and by pressing the key you can switch (or leave) the “uppercase mode”.
 +
 +
The <tt>vim</tt> has several very useful modes, but to start off you need to know there are
 +
* normal mode, and
 +
* insert mode.
 +
<tt>vim</tt> indicates any modes ''other'' than ''normal'' in the bottom line, e.g. by showing <syntaxhighlight lang="text" inline style="white-space: nowrap;">‑‑ INSERT ‑‑</syntaxhighlight>.
 +
 +
In order to ''enter'' insert mode ''from'' normal mode, you can hit the {{keypress|i}} key (among many other possibilities).
 +
''Then'' you can start typing and editing like usual.
 +
In order to ''leave'' insert mode again, you hit the {{keypress|Esc}} key.
 +
 +
Saving your source code file is done by typing <syntaxhighlight lang="text" inline>:write</syntaxhighlight> in normal mode.
 +
If there is no file name associated with the current text buffer, you will furthermore need to specify a file name (<syntaxhighlight lang="text" inline style="white-space: nowrap;">:write helloWorld.pas</syntaxhighlight>).
 +
Quitting the <tt>vim</tt> can be done by typing <syntaxhighlight lang="text" inline>:quit</syntaxhighlight> in normal mode.
 +
This in fact just closes the currently focused window, so you may have to type it repeatedly (e.g. after you have opened a help window by typing <syntaxhighlight lang="text" inline>:help</syntaxhighlight>).
 +
 +
Opening a specific file is usually done by supplying the file name as an argument on the command line.
 +
It is also possible to use the command <syntaxhighlight lang="text" inline style="white-space: nowrap;">:open /tmp/foobar.pas</syntaxhighlight>.
 +
 +
{{Note|If you like to get a more comprehensive tutorial, start <syntaxhighlight lang="text" inline>vimtutor</syntaxhighlight> which will guide through your first steps. See <syntaxhighlight lang="text" inline>vimtutor(1)</syntaxhighlight> for more details.}}
  
 
== configuration ==
 
== configuration ==
 
The <tt>vim</tt> editor experience can be enhanced for editing [[Pascal]] source code files.
 
The <tt>vim</tt> editor experience can be enhanced for editing [[Pascal]] source code files.
 +
You most probably want to insert any of the configuration settings below into your <syntaxhighlight lang="text" inline>vimrc</syntaxhighlight> rather than typing it out every time you start <tt>vim</tt> (confer <syntaxhighlight lang="vim" inline style="white-space: nowrap;">:help vimrc</syntaxhighlight> for its file system location).
 +
In your <tt>vimrc</tt> normal mode commands are not preceded by a colon, just like shown below.
  
 
=== syntax highlighting ===
 
=== syntax highlighting ===
Line 15: Line 41:
  
 
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
 
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
<syntaxhighlight lang="vim">let g:pascal_fpc=true</syntaxhighlight>
+
<syntaxhighlight lang="vim">let g:pascal_fpc=1</syntaxhighlight>
 
and/or
 
and/or
<syntaxhighlight lang="vim">let g:pascal_delphi=true</syntaxhighlight>
+
<syntaxhighlight lang="vim">let g:pascal_delphi=1</syntaxhighlight>
 +
 
 +
For further explanations see <syntaxhighlight lang="vim" inline style="white-space: nowrap;">:help pascal.vim</syntaxhighlight>
  
 
=== margin ===
 
=== margin ===
Line 23: Line 51:
 
<syntaxhighlight lang="vim">set colorcolumn=80</syntaxhighlight>
 
<syntaxhighlight lang="vim">set colorcolumn=80</syntaxhighlight>
 
will change the background of the 80<sup>th</sup> column.
 
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.
+
Depending on the <syntaxhighlight lang="vim" inline>tabstop</syntaxhighlight> setting you may reach it earlier or later.
This is just a visual guidance.
+
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.
 
The <syntaxhighlight lang="vim" inline>textwidth</syntaxhighlight> variable will actually thwart insertion of lines longer than a certain width.
  
Line 39: Line 66:
  
 
=== tab replacement ===
 
=== tab replacement ===
Some people prefer hitting the key to be directly translated to a number of space characters.
+
Some people prefer hitting the {{keypress|tab}} 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
 
The setting
<syntaxhighlight lang="vim" inline>let pascal_no_tabs=1</syntaxhighlight>
+
<syntaxhighlight lang="vim" inline style="white-space: nowrap;">:let pascal_no_tabs=1</syntaxhighlight>
 
will highlight any tab characters as an error.
 
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 style="white-space: nowrap;">:help retab</syntaxhighlight> if you want to <tt>expand(1)</tt>.
  
 
=== white characters ===
 
=== white characters ===
 
Using
 
Using
 
<syntaxhighlight lang="vim">set listchars=space:⍽</syntaxhighlight>
 
<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.
+
and then activating the “list mode” <syntaxhighlight lang="vim" inline style="white-space: nowrap;">:set list</syntaxhighlight>, you can display all space characters with a custom symbol.
 
This uses a different color than the actual character used.
 
This uses a different color than the actual character used.
  
 
=== navigation ===
 
=== navigation ===
The [[%|<syntaxhighlight lang="text" inline>%</syntaxhighlight>]] key allows you to jump matching parenthesis and curly braces.
+
The [[%|{{keypress|%}}]] 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>]].
 
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>):
+
You have enable the <syntaxhighlight lang="vim" inline>matchit</syntaxhighlight> plugin (cf. <syntaxhighlight lang="vim" inline style="white-space: nowrap;">:help matchit-install</syntaxhighlight>):
 
<syntaxhighlight lang="vim">packadd! matchit</syntaxhighlight>
 
<syntaxhighlight lang="vim">packadd! matchit</syntaxhighlight>
  
 
=== folding ===
 
=== folding ===
Confer <syntaxhighlight lang="vim" inline>:help folding</syntaxhighlight>.
+
Confer <syntaxhighlight lang="vim" inline style="white-space: nowrap;">:help folding</syntaxhighlight>.
  
 
The [[User: Kai Burghardt|original editor]] of this article recommends manual folds using markers:
 
The [[User: Kai Burghardt|original editor]] of this article recommends manual folds using markers:
Line 82: Line 109:
 
Instead of the default triple curly braces, you can honor Lazarus’ IDE directives:
 
Instead of the default triple curly braces, you can honor Lazarus’ IDE directives:
 
<syntaxhighlight lang="vim">set foldmarker={%region},{%endregion}</syntaxhighlight>
 
<syntaxhighlight lang="vim">set foldmarker={%region},{%endregion}</syntaxhighlight>
Note, fold markers are static strings.
+
Note, fold markers are ''static'' strings.
 
Regular expressions are not supported, thus a fold marker definition like this one is ''case-sensitive''.
 
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.
 
Fold markers appearing in string literals are still recognized as fold markers.

Latest revision as of 14:05, 28 August 2022

vim is an editor well-suited to program.

basic usage

The most important difference to many other editors is that vim constantly has a mode. The mode is like the caps lock key on your keyboard: By default you are in “lowercase mode” and by pressing the key you can switch (or leave) the “uppercase mode”.

The vim has several very useful modes, but to start off you need to know there are

  • normal mode, and
  • insert mode.

vim indicates any modes other than normal in the bottom line, e.g. by showing ‑‑ INSERT ‑‑.

In order to enter insert mode from normal mode, you can hit the i key (among many other possibilities). Then you can start typing and editing like usual. In order to leave insert mode again, you hit the Esc key.

Saving your source code file is done by typing :write in normal mode. If there is no file name associated with the current text buffer, you will furthermore need to specify a file name (:write helloWorld.pas). Quitting the vim can be done by typing :quit in normal mode. This in fact just closes the currently focused window, so you may have to type it repeatedly (e.g. after you have opened a help window by typing :help).

Opening a specific file is usually done by supplying the file name as an argument on the command line. It is also possible to use the command :open /tmp/foobar.pas.

Light bulb  Note: If you like to get a more comprehensive tutorial, start vimtutor which will guide through your first steps. See vimtutor(1) for more details.

configuration

The vim editor experience can be enhanced for editing Pascal source code files. You most probably want to insert any of the configuration settings below into your vimrc rather than typing it out every time you start vim (confer :help vimrc for its file system location). In your vimrc normal mode commands are not preceded by a colon, just like shown below.

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=1

and/or

let g:pascal_delphi=1

For further explanations see :help pascal.vim

margin

The ex-command

set colorcolumn=80

will change the background of the 80th column. Depending on the tabstop setting 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 Tab 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