vim: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
Title: Vim vim
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | `vim` is an [[source code editor|editor]] well-suited to program. | |
| 2 | + | ||
| 3 | + | ## basic usage | |
| 4 | + | ||
| 5 | + | 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”. | |
| 6 | + | ||
| 7 | + | The `vim` has several very useful modes, but to start off you need to know there are | |
| 8 | + | ||
| 9 | + | - normal mode, and | |
| 10 | + | - insert mode. | |
| 11 | + | ||
| 12 | + | `vim` indicates any modes *other* than *normal* in the bottom line, e.g. by showing `‑‑ INSERT ‑‑`. | |
| 13 | + | ||
| 14 | + | 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. | |
| 15 | + | ||
| 16 | + | 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`). | |
| 17 | + | ||
| 18 | + | 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`. | |
| 19 | + | ||
| 20 | + | > **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. | |
| 21 | + | ||
| 22 | + | ## configuration | |
| 23 | + | ||
| 24 | + | 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. | |
| 25 | + | ||
| 26 | + | ### syntax highlighting | |
| 27 | + | ||
| 28 | + | 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. | |
| 29 | + | ||
| 30 | + | If the automatisms fail, e. g. for `*.inc` files, syntax highlighting can be chosen explicitly: | |
| 31 | + | ||
| 32 | + | ```vim | |
| 33 | + | set filetype=pascal | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | Multiple syntax highlighting definitions can be combined, for example: | |
| 37 | + | ||
| 38 | + | ```vim | |
| 39 | + | set filetype=pascal.doxygen | |
| 40 | + | ``` | |
| 41 | + | ||
| 42 | + | 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 | |
| 43 | + | ||
| 44 | + | ```vim | |
| 45 | + | let g:pascal_fpc=1 | |
| 46 | + | ``` | |
| 47 | + | ||
| 48 | + | and/or | |
| 49 | + | ||
| 50 | + | ```vim | |
| 51 | + | let g:pascal_delphi=1 | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | For further explanations see `:help pascal.vim` | |
| 55 | + | ||
| 56 | + | ### margin | |
| 57 | + | ||
| 58 | + | The ex-command | |
| 59 | + | ||
| 60 | + | ```vim | |
| 61 | + | set colorcolumn=80 | |
| 62 | + | ``` | |
| 63 | + | ||
| 64 | + | will change the background of the 80<sup>th</sup> 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. | |
| 65 | + | ||
| 66 | + | ### tabstops | |
| 67 | + | ||
| 68 | + | 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: | |
| 69 | + | ||
| 70 | + | ```vim | |
| 71 | + | set tabstop=5 | |
| 72 | + | ``` | |
| 73 | + | ||
| 74 | + | `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. | |
| 75 | + | ||
| 76 | + | ### tab replacement | |
| 77 | + | ||
| 78 | + | 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. | |
| 79 | + | ||
| 80 | + | Confer `:help retab` if you want to `expand(1)`. | |
| 81 | + | ||
| 82 | + | ### white characters | |
| 83 | + | ||
| 84 | + | Using | |
| 85 | + | ||
| 86 | + | ```vim | |
| 87 | + | set listchars=space:⍽ | |
| 88 | + | ``` | |
| 89 | + | ||
| 90 | + | 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. | |
| 91 | + | ||
| 92 | + | ### navigation | |
| 93 | + | ||
| 94 | + | The [[%]] key allows you to jump matching parenthesis and curly braces. However, extra precautions have to be taken for [[Begin|begin]] and [[End|end]]. You have enable the `matchit` plugin (cf. `:help matchit-install`): | |
| 95 | + | ||
| 96 | + | ```vim | |
| 97 | + | packadd! matchit | |
| 98 | + | ``` | |
| 99 | + | ||
| 100 | + | ### folding | |
| 101 | + | ||
| 102 | + | Confer `:help folding`. | |
| 103 | + | ||
| 104 | + | The original editor of this article recommends manual folds using markers: | |
| 105 | + | ||
| 106 | + | ```vim | |
| 107 | + | set foldmethod=marker | |
| 108 | + | ``` | |
| 109 | + | ||
| 110 | + | This will generate folds on markers. The default markers are `{}` (confer `:help foldmarker`). Thus, the following code | |
| 111 | + | ||
| 112 | + | ``` pascal | |
| 113 | + | // procedure foo(integer, Boolean) {} | |
| 114 | + | ``` | |
| 115 | + | ||
| 116 | + | will look like | |
| 117 | + | ||
| 118 | + | ```text | |
| 119 | + | +-- 5 lines: procedure foo(integer, Boolean) ---------------------------------- | |
| 120 | + | ``` | |
| 121 | + | ||
| 122 | + | when folded. | |
| 123 | + | ||
| 124 | + | Instead of the default triple curly braces, you can honor Lazarus’ IDE directives: | |
| 125 | + | ||
| 126 | + | ```vim | |
| 127 | + | set foldmarker={%region},{%endregion} | |
| 128 | + | ``` | |
| 129 | + | ||
| 130 | + | 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.'` | |
| 131 | + | ||
| 132 | + | ## see also | |
| 133 | + | ||
| 134 | + | - Wikibook: [Learning the `vi` editor](https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim) | |
| 135 | + | ||
| 136 | + | --- | |
| 137 | + | *From the Free Pascal wiki, page "vim", as it stood on 2022-08-28 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 138 | + | Category: Tools | |
| 139 | + | Category: Tutorials |
