annotate runtime/doc/usr_30.txt @ 32936:c517845bd10e v9.0.1776

patch 9.0.1776: No support for stable Python 3 ABI Commit: https://github.com/vim/vim/commit/c13b3d1350b60b94fe87f0761ea31c0e7fb6ebf3 Author: Yee Cheng Chin <ychin.git@gmail.com> Date: Sun Aug 20 21:18:38 2023 +0200 patch 9.0.1776: No support for stable Python 3 ABI Problem: No support for stable Python 3 ABI Solution: Support Python 3 stable ABI Commits: 1) Support Python 3 stable ABI to allow mixed version interoperatbility Vim currently supports embedding Python for use with plugins, and the "dynamic" linking option allows the user to specify a locally installed version of Python by setting `pythonthreedll`. However, one caveat is that the Python 3 libs are not binary compatible across minor versions, and mixing versions can potentially be dangerous (e.g. let's say Vim was linked against the Python 3.10 SDK, but the user sets `pythonthreedll` to a 3.11 lib). Usually, nothing bad happens, but in theory this could lead to crashes, memory corruption, and other unpredictable behaviors. It's also difficult for the user to tell something is wrong because Vim has no way of reporting what Python 3 version Vim was linked with. For Vim installed via a package manager, this usually isn't an issue because all the dependencies would already be figured out. For prebuilt Vim binaries like MacVim (my motivation for working on this), AppImage, and Win32 installer this could potentially be an issue as usually a single binary is distributed. This is more tricky when a new Python version is released, as there's a chicken-and-egg issue with deciding what Python version to build against and hard to keep in sync when a new Python version just drops and we have a mix of users of different Python versions, and a user just blindly upgrading to a new Python could lead to bad interactions with Vim. Python 3 does have a solution for this problem: stable ABI / limited API (see https://docs.python.org/3/c-api/stable.html). The C SDK limits the API to a set of functions that are promised to be stable across versions. This pull request adds an ifdef config that allows us to turn it on when building Vim. Vim binaries built with this option should be safe to freely link with any Python 3 libraies without having the constraint of having to use the same minor version. Note: Python 2 has no such concept and this doesn't change how Python 2 integration works (not that there is going to be a new version of Python 2 that would cause compatibility issues in the future anyway). --- Technical details: ====== The stable ABI can be accessed when we compile with the Python 3 limited API (by defining `Py_LIMITED_API`). The Python 3 code (in `if_python3.c` and `if_py_both.h`) would now handle this and switch to limited API mode. Without it set, Vim will still use the full API as before so this is an opt-in change. The main difference is that `PyType_Object` is now an opaque struct that we can't directly create "static types" out of, and we have to create type objects as "heap types" instead. This is because the struct is not stable and changes from version to version (e.g. 3.8 added a `tp_vectorcall` field to it). I had to change all the types to be allocated on the heap instead with just a pointer to them. Other functions are also simply missing in limited API, or they are introduced too late (e.g. `PyUnicode_AsUTF8AndSize` in 3.10) to it that we need some other ways to do the same thing, so I had to abstract a few things into macros, and sometimes re-implement functions like `PyObject_NEW`. One caveat is that in limited API, `OutputType` (used for replacing `sys.stdout`) no longer inherits from `PyStdPrinter_Type` which I don't think has any real issue other than minor differences in how they convert to a string and missing a couple functions like `mode()` and `fileno()`. Also fixed an existing bug where `tp_basicsize` was set incorrectly for `BufferObject`, `TabListObject, `WinListObject`. Technically, there could be a small performance drop, there is a little more indirection with accessing type objects, and some APIs like `PyUnicode_AsUTF8AndSize` are missing, but in practice I didn't see any difference, and any well-written Python plugin should try to avoid excessing callbacks to the `vim` module in Python anyway. I only tested limited API mode down to Python 3.7, which seemes to compile and work fine. I haven't tried earlier Python versions. 2) Fix PyIter_Check on older Python vers / type##Ptr unused warning For PyIter_Check, older versions exposed them as either macros (used in full API), or a function (for use in limited API). A previous change exposed PyIter_Check to the dynamic build because Python just moved it to function-only in 3.10 anyway. Because of that, just make sure we always grab the function in dynamic builds in earlier versions since that's what Python eventually did anyway. 3) Move Py_LIMITED_API define to configure script Can now use --with-python-stable-abi flag to customize what stable ABI version to target. Can also use an env var to do so as well. 4) Show +python/dyn-stable in :version, and allow has() feature query Not sure if the "/dyn-stable" suffix would break things, or whether we should do it another way. Or just don't show it in version and rely on has() feature checking. 5) Documentation first draft. Still need to implement v:python3_version 6) Fix PyIter_Check build breaks when compiling against Python 3.8 7) Add CI coverage stable ABI on Linux/Windows / make configurable on Windows This adds configurable options for Windows make files (both MinGW and MSVC). CI will also now exercise both traditional full API and stable ABI for Linux and Windows in the matrix for coverage. Also added a "dynamic" option to Linux matrix as a drive-by change to make other scripting languages like Ruby / Perl testable under both static and dynamic builds. 8) Fix inaccuracy in Windows docs Python's own docs are confusing but you don't actually want to use `python3.dll` for the dynamic linkage. 9) Add generated autoconf file 10) Add v:python3_version support This variable indicates the version of Python3 that Vim was built against (PY_VERSION_HEX), and will be useful to check whether the Python library you are loading in dynamically actually fits it. When built with stable ABI, it will be the limited ABI version instead (`Py_LIMITED_API`), which indicates the minimum version of Python 3 the user should have, rather than the exact match. When stable ABI is used, we won't be exposing PY_VERSION_HEX in this var because it just doesn't seem necessary to do so (the whole point of stable ABI is the promise that it will work across versions), and I don't want to confuse the user with too many variables. Also, cleaned up some documentation, and added help tags. 11) Fix Python 3.7 compat issues Fix a couple issues when using limited API < 3.8 - Crash on exit: In Python 3.7, if a heap-allocated type is destroyed before all instances are, it would cause a crash later. This happens when we destroyed `OptionsType` before calling `Py_Finalize` when using the limited API. To make it worse, later versions changed the semantics and now each instance has a strong reference to its own type and the recommendation has changed to have each instance de-ref its own type and have its type in GC traversal. To avoid dealing with these cross-version variations, we just don't free the heap type. They are static types in non-limited-API anyway and are designed to last through the entirety of the app, and we also don't restart the Python runtime and therefore do not need it to have absolutely 0 leaks. See: - https://docs.python.org/3/whatsnew/3.8.html#changes-in-the-c-api - https://docs.python.org/3/whatsnew/3.9.html#changes-in-the-c-api - PyIter_Check: This function is not provided in limited APIs older than 3.8. Previously I was trying to mock it out using manual PyType_GetSlot() but it was brittle and also does not actually work properly for static types (it will generate a Python error). Just return false. It does mean using limited API < 3.8 is not recommended as you lose the functionality to handle iterators, but from playing with plugins I couldn't find it to be an issue. - Fix loading of PyIter_Check so it will be done when limited API < 3.8. Otherwise loading a 3.7 Python lib will fail even if limited API was specified to use it. 12) Make sure to only load `PyUnicode_AsUTF8AndSize` in needed in limited API We don't use this function unless limited API >= 3.10, but we were loading it regardless. Usually it's ok in Unix-like systems where Python just has a single lib that we load from, but in Windows where there is a separate python3.dll this would not work as the symbol would not have been exposed in this more limited DLL file. This makes it much clearer under what condition is this function needed. closes: #12032 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
author Christian Brabandt <cb@256bit.org>
date Sun, 20 Aug 2023 21:30:04 +0200
parents f8116058ca76
children 4635e43f2c6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29314
f8116058ca76 release version 9.0
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
1 *usr_30.txt* For Vim version 9.0. Last change: 2007 Nov 10
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 VIM USER MANUAL - by Bram Moolenaar
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 Editing programs
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8 Vim has various commands that aid in writing computer programs. Compile a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 program and directly jump to reported errors. Automatically set the indent
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 for many languages and format comments.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 |30.1| Compiling
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 |30.2| Indenting C files
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 |30.3| Automatic indenting
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 |30.4| Other indenting
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 |30.5| Tabs and spaces
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17 |30.6| Formatting comments
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 Next chapter: |usr_31.txt| Exploiting the GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20 Previous chapter: |usr_29.txt| Moving through programs
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 Table of contents: |usr_toc.txt|
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 *30.1* Compiling
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 Vim has a set of so called "quickfix" commands. They enable you to compile a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 program from within Vim and then go through the errors generated and fix them
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 (hopefully). You can then recompile and fix any new errors that are found
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 until finally your program compiles without any error.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 The following command runs the program "make" (supplying it with any argument
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 you give) and captures the results: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 :make {arguments}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 If errors were generated, they are captured and the editor positions you where
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 the first error occurred.
237
73354c21f1e4 updated for version 7.0066
vimboss
parents: 7
diff changeset
38 Take a look at an example ":make" session. (Typical :make sessions generate
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 far more errors and fewer stupid ones.) After typing ":make" the screen looks
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 :!make | &tee /tmp/vim215953.err ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 gcc -g -Wall -o prog main.c sub.c ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 main.c: In function 'main': ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 main.c:6: too many arguments to function 'do_sub' ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 main.c: At top level: ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 main.c:10: parse error before '}' ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 make: *** [prog] Error 1 ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 2 returned ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 "main.c" 11L, 111C ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 (3 of 6): too many arguments to function 'do_sub' ~
1125
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
53 Press ENTER or type command to continue ~
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 From this you can see that you have errors in the file "main.c". When you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 press <Enter>, Vim displays the file "main.c", with the cursor positioned on
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 line 6, the first line with an error. You did not need to specify the file or
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 the line number, Vim knew where to go by looking in the error messages.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 +---------------------------------------------------+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 |int main() |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 |{ |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 | int i=3; |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 cursor -> | do_sub("foo"); |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 | ++i; |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66 | return (0); |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 |} |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 |} |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 | ~ |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 |(3 of 12): too many arguments to function 'do_sub' |
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 +---------------------------------------------------+
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 The following command goes to where the next error occurs: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 :cnext
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 Vim jumps to line 10, the last line in the file, where there is an extra '}'.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 When there is not enough room, Vim will shorten the error message. To see
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 the whole message use: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 :cc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 You can get an overview of all the error messages with the ":clist" command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 The output looks like this: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 :clist
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 < 3 main.c: 6:too many arguments to function 'do_sub' ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 5 main.c: 10:parse error before '}' ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 Only the lines where Vim recognized a file name and line number are listed
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 here. It assumes those are the interesting lines and the rest is just boring
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 messages. However, sometimes unrecognized lines do contain something you want
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93 to see. Output from the linker, for example, about an undefined function.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 To see all the messages add a "!" to the command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 :clist!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 < 1 gcc -g -Wall -o prog main.c sub.c ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 2 main.c: In function 'main': ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 3 main.c:6: too many arguments to function 'do_sub' ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 4 main.c: At top level: ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 5 main.c:10: parse error before '}' ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 6 make: *** [prog] Error 1 ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104 Vim will highlight the current error. To go back to the previous error, use:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 :cprevious
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 Other commands to move around in the error list:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 :cfirst to first error
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111 :clast to last error
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 :cc 3 to error nr 3
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 USING ANOTHER COMPILER
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 The name of the program to run when the ":make" command is executed is defined
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 by the 'makeprg' option. Usually this is set to "make", but Visual C++ users
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 should set this to "nmake" by executing the following command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 :set makeprg=nmake
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 You can also include arguments in this option. Special characters need to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 be escaped with a backslash. Example: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 :set makeprg=nmake\ -f\ project.mak
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128 You can include special Vim keywords in the command specification. The %
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 character expands to the name of the current file. So if you execute the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 command: >
5690
40f18a1c1592 updated for version 7.4.191
Bram Moolenaar <bram@vim.org>
parents: 5294
diff changeset
131 :set makeprg=make\ %:S
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 When you are editing main.c, then ":make" executes the following command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 make main.c
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 This is not too useful, so you will refine the command a little and use the :r
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 (root) modifier: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139
5690
40f18a1c1592 updated for version 7.4.191
Bram Moolenaar <bram@vim.org>
parents: 5294
diff changeset
140 :set makeprg=make\ %:r:S.o
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 Now the command executed is as follows: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 make main.o
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 More about these modifiers here: |filename-modifiers|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 OLD ERROR LISTS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150
1125
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
151 Suppose you ":make" a program. There is a warning message in one file and an
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 error message in another. You fix the error and use ":make" again to check if
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153 it was really fixed. Now you want to look at the warning message. It doesn't
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 show up in the last error list, since the file with the warning wasn't
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 compiled again. You can go back to the previous error list with: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157 :colder
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 Then use ":clist" and ":cc {nr}" to jump to the place with the warning.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160 To go forward to the next error list: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
162 :cnewer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
163
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
164 Vim remembers ten error lists.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
165
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
166
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
167 SWITCHING COMPILERS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
168
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
169 You have to tell Vim what format the error messages are that your compiler
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
170 produces. This is done with the 'errorformat' option. The syntax of this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
171 option is quite complicated and it can be made to fit almost any compiler.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
172 You can find the explanation here: |errorformat|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
173
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
174 You might be using various different compilers. Setting the 'makeprg' option,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
175 and especially the 'errorformat' each time is not easy. Vim offers a simple
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
176 method for this. For example, to switch to using the Microsoft Visual C++
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
177 compiler: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
178
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
179 :compiler msvc
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
180
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
181 This will find the Vim script for the "msvc" compiler and set the appropriate
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
182 options.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
183 You can write your own compiler files. See |write-compiler-plugin|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
184
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
185
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
186 OUTPUT REDIRECTION
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
187
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
188 The ":make" command redirects the output of the executed program to an error
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
189 file. How this works depends on various things, such as the 'shell'. If your
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
190 ":make" command doesn't capture the output, check the 'makeef' and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
191 'shellpipe' options. The 'shellquote' and 'shellxquote' options might also
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
192 matter.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
193
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
194 In case you can't get ":make" to redirect the file for you, an alternative is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
195 to compile the program in another window and redirect the output into a file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
196 Then have Vim read this file with: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
197
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
198 :cfile {filename}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
199
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
200 Jumping to errors will work like with the ":make" command.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
201
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
202 ==============================================================================
1624
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
203 *30.2* Indenting C style text
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
204
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
205 A program is much easier to understand when the lines have been properly
1624
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
206 indented. Vim offers various ways to make this less work. For C or C style
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
207 programs like Java or C++, set the 'cindent' option. Vim knows a lot about C
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
208 programs and will try very hard to automatically set the indent for you. Set
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
209 the 'shiftwidth' option to the amount of spaces you want for a deeper level.
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
210 Four spaces will work fine. One ":set" command will do it: >
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
211
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
212 :set cindent shiftwidth=4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
213
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
214 With this option enabled, when you type something such as "if (x)", the next
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
215 line will automatically be indented an additional level.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
216
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
217 if (flag)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
218 Automatic indent ---> do_the_work();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
219 Automatic unindent <-- if (other_flag) {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
220 Automatic indent ---> do_file();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
221 keep indent do_some_more();
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
222 Automatic unindent <-- }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
223
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
224 When you type something in curly braces ({}), the text will be indented at the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
225 start and unindented at the end. The unindenting will happen after typing the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
226 '}', since Vim can't guess what you are going to type.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
227
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
228 One side effect of automatic indentation is that it helps you catch errors in
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
229 your code early. When you type a } to finish a function, only to find that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
230 the automatic indentation gives it more indent than what you expected, there
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
231 is probably a } missing. Use the "%" command to find out which { matches the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
232 } you typed.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
233 A missing ) and ; also cause extra indent. Thus if you get more white
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
234 space than you would expect, check the preceding lines.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
235
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
236 When you have code that is badly formatted, or you inserted and deleted lines,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
237 you need to re-indent the lines. The "=" operator does this. The simplest
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
238 form is: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
239
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
240 ==
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
241
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
242 This indents the current line. Like with all operators, there are three ways
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
243 to use it. In Visual mode "=" indents the selected lines. A useful text
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
244 object is "a{". This selects the current {} block. Thus, to re-indent the
1125
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
245 code block the cursor is in: >
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
246
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
247 =a{
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
248
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
249 I you have really badly indented code, you can re-indent the whole file with:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
250 >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
251 gg=G
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
252
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
253 However, don't do this in files that have been carefully indented manually.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
254 The automatic indenting does a good job, but in some situations you might want
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
255 to overrule it.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
256
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
257
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
258 SETTING INDENT STYLE
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
259
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
260 Different people have different styles of indentation. By default Vim does a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
261 pretty good job of indenting in a way that 90% of programmers do. There are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
262 different styles, however; so if you want to, you can customize the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
263 indentation style with the 'cinoptions' option.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
264 By default 'cinoptions' is empty and Vim uses the default style. You can
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
265 add various items where you want something different. For example, to make
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
266 curly braces be placed like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
267
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
268 if (flag) ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
269 { ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
270 i = 8; ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
271 j = 0; ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
272 } ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
273
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
274 Use this command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
275
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
276 :set cinoptions+={2
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
277
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
278 There are many of these items. See |cinoptions-values|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
279
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
280 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
281 *30.3* Automatic indenting
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
282
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
283 You don't want to switch on the 'cindent' option manually every time you edit
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
284 a C file. This is how you make it work automatically: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
285
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
286 :filetype indent on
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
287
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
288 Actually, this does a lot more than switching on 'cindent' for C files. First
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
289 of all, it enables detecting the type of a file. That's the same as what is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
290 used for syntax highlighting.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
291 When the filetype is known, Vim will search for an indent file for this
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
292 type of file. The Vim distribution includes a number of these for various
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
293 programming languages. This indent file will then prepare for automatic
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
294 indenting specifically for this file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
295
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
296 If you don't like the automatic indenting, you can switch it off again: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
297
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
298 :filetype indent off
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
299
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
300 If you don't like the indenting for one specific type of file, this is how you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
301 avoid it. Create a file with just this one line: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
302
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
303 :let b:did_indent = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
304
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
305 Now you need to write this in a file with a specific name:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
306
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
307 {directory}/indent/{filetype}.vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
308
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
309 The {filetype} is the name of the file type, such as "cpp" or "java". You can
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
310 see the exact name that Vim detected with this command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
311
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
312 :set filetype
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
313
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
314 In this file the output is:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
315
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
316 filetype=help ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
317
1125
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
318 Thus you would use "help" for {filetype}.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
319 For the {directory} part you need to use your runtime directory. Look at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
320 the output of this command: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
321
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
322 set runtimepath
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
323
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
324 Now use the first item, the name before the first comma. Thus if the output
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
325 looks like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
326
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
327 runtimepath=~/.vim,/usr/local/share/vim/vim60/runtime,~/.vim/after ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
328
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
329 You use "~/.vim" for {directory}. Then the resulting file name is:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
330
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
331 ~/.vim/indent/help.vim ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
332
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
333 Instead of switching the indenting off, you could write your own indent file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
334 How to do that is explained here: |indent-expression|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
335
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
336 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
337 *30.4* Other indenting
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
338
21676
1b345fb68ae3 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 18879
diff changeset
339 The simplest form of automatic indenting is with the 'autoindent' option.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
340 It uses the indent from the previous line. A bit smarter is the 'smartindent'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
341 option. This is useful for languages where no indent file is available.
1125
96cd8222a819 updated for version 7.1a
vimboss
parents: 874
diff changeset
342 'smartindent' is not as smart as 'cindent', but smarter than 'autoindent'.
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
343 With 'smartindent' set, an extra level of indentation is added for each {
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
344 and removed for each }. An extra level of indentation will also be added for
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
345 any of the words in the 'cinwords' option. Lines that begin with # are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
346 treated specially: all indentation is removed. This is done so that
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
347 preprocessor directives will all start in column 1. The indentation is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
348 restored for the next line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
349
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
350
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
351 CORRECTING INDENTS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
352
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
353 When you are using 'autoindent' or 'smartindent' to get the indent of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
354 previous line, there will be many times when you need to add or remove one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
355 'shiftwidth' worth of indent. A quick way to do this is using the CTRL-D and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
356 CTRL-T commands in Insert mode.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
357 For example, you are typing a shell script that is supposed to look like
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
358 this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
359
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
360 if test -n a; then ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
361 echo a ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
362 echo "-------" ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
363 fi ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
364
2207
b17bbfa96fa0 Add the settabvar() and gettabvar() functions.
Bram Moolenaar <bram@vim.org>
parents: 2154
diff changeset
365 Start off by setting these options: >
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
366
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
367 :set autoindent shiftwidth=3
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
368
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
369 You start by typing the first line, <Enter> and the start of the second line:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
370
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
371 if test -n a; then ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
372 echo ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
373
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
374 Now you see that you need an extra indent. Type CTRL-T. The result:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
375
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
376 if test -n a; then ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
377 echo ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
378
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
379 The CTRL-T command, in Insert mode, adds one 'shiftwidth' to the indent, no
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
380 matter where in the line you are.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
381 You continue typing the second line, <Enter> and the third line. This time
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
382 the indent is OK. Then <Enter> and the last line. Now you have this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
383
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
384 if test -n a; then ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
385 echo a ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
386 echo "-------" ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
387 fi ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
388
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
389 To remove the superfluous indent in the last line press CTRL-D. This deletes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
390 one 'shiftwidth' worth of indent, no matter where you are in the line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
391 When you are in Normal mode, you can use the ">>" and "<<" commands to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
392 shift lines. ">" and "<" are operators, thus you have the usual three ways to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
393 specify the lines you want to indent. A useful combination is: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
394
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
395 >i{
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
396
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
397 This adds one indent to the current block of lines, inside {}. The { and }
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
398 lines themselves are left unmodified. ">a{" includes them. In this example
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
399 the cursor is on "printf":
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
400
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
401 original text after ">i{" after ">a{"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
402
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
403 if (flag) if (flag) if (flag) ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
404 { { { ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
405 printf("yes"); printf("yes"); printf("yes"); ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
406 flag = 0; flag = 0; flag = 0; ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
407 } } } ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
408
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
409 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
410 *30.5* Tabs and spaces
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
411
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
412 'tabstop' is set to eight by default. Although you can change it, you quickly
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
413 run into trouble later. Other programs won't know what tabstop value you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
414 used. They probably use the default value of eight, and your text suddenly
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
415 looks very different. Also, most printers use a fixed tabstop value of eight.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
416 Thus it's best to keep 'tabstop' alone. (If you edit a file which was written
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
417 with a different tabstop setting, see |25.3| for how to fix that.)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
418 For indenting lines in a program, using a multiple of eight spaces makes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
419 you quickly run into the right border of the window. Using a single space
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
420 doesn't provide enough visual difference. Many people prefer to use four
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
421 spaces, a good compromise.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
422 Since a <Tab> is eight spaces and you want to use an indent of four spaces,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
423 you can't use a <Tab> character to make your indent. There are two ways to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
424 handle this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
425
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
426 1. Use a mix of <Tab> and space characters. Since a <Tab> takes the place of
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
427 eight spaces, you have fewer characters in your file. Inserting a <Tab>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
428 is quicker than eight spaces. Backspacing works faster as well.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
429
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
430 2. Use spaces only. This avoids the trouble with programs that use a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
431 different tabstop value.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
432
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
433 Fortunately, Vim supports both methods quite well.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
434
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
435
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
436 SPACES AND TABS
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
437
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
438 If you are using a combination of tabs and spaces, you just edit normally.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
439 The Vim defaults do a fine job of handling things.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
440 You can make life a little easier by setting the 'softtabstop' option.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
441 This option tells Vim to make the <Tab> key look and feel as if tabs were set
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
442 at the value of 'softtabstop', but actually use a combination of tabs and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
443 spaces.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
444 After you execute the following command, every time you press the <Tab> key
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
445 the cursor moves to the next 4-column boundary: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
446
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
447 :set softtabstop=4
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
448
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
449 When you start in the first column and press <Tab>, you get 4 spaces inserted
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
450 in your text. The second time, Vim takes out the 4 spaces and puts in a <Tab>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
451 (thus taking you to column 8). Thus Vim uses as many <Tab>s as possible, and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
452 then fills up with spaces.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
453 When backspacing it works the other way around. A <BS> will always delete
1624
18ee39301b82 updated for version 7.2a
vimboss
parents: 1276
diff changeset
454 the amount specified with 'softtabstop'. Then <Tab>s are used as many as
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
455 possible and spaces to fill the gap.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
456 The following shows what happens pressing <Tab> a few times, and then using
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
457 <BS>. A "." stands for a space and "------->" for a <Tab>.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
458
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
459 type result ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
460 <Tab> ....
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
461 <Tab><Tab> ------->
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
462 <Tab><Tab><Tab> ------->....
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
463 <Tab><Tab><Tab><BS> ------->
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
464 <Tab><Tab><Tab><BS><BS> ....
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
465
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
466 An alternative is to use the 'smarttab' option. When it's set, Vim uses
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
467 'shiftwidth' for a <Tab> typed in the indent of a line, and a real <Tab> when
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
468 typed after the first non-blank character. However, <BS> doesn't work like
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
469 with 'softtabstop'.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
470
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
471
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
472 JUST SPACES
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
473
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
474 If you want absolutely no tabs in your file, you can set the 'expandtab'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
475 option: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
476
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
477 :set expandtab
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
478
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
479 When this option is set, the <Tab> key inserts a series of spaces. Thus you
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
480 get the same amount of white space as if a <Tab> character was inserted, but
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
481 there isn't a real <Tab> character in your file.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
482 The backspace key will delete each space by itself. Thus after typing one
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
483 <Tab> you have to press the <BS> key up to eight times to undo it. If you are
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
484 in the indent, pressing CTRL-D will be a lot quicker.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
485
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
486
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
487 CHANGING TABS IN SPACES (AND BACK)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
488
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
489 Setting 'expandtab' does not affect any existing tabs. In other words, any
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
490 tabs in the document remain tabs. If you want to convert tabs to spaces, use
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
491 the ":retab" command. Use these commands: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
492
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
493 :set expandtab
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
494 :%retab
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
495
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
496 Now Vim will have changed all indents to use spaces instead of tabs. However,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
497 all tabs that come after a non-blank character are kept. If you want these to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
498 be converted as well, add a !: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
499
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
500 :%retab!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
501
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
502 This is a little bit dangerous, because it can also change tabs inside a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
503 string. To check if these exist, you could use this: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
504
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
505 /"[^"\t]*\t[^"]*"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
506
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
507 It's recommended not to use hard tabs inside a string. Replace them with
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
508 "\t" to avoid trouble.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
509
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
510 The other way around works just as well: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
511
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
512 :set noexpandtab
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
513 :%retab!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
514
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
515 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
516 *30.6* Formatting comments
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
517
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
518 One of the great things about Vim is that it understands comments. You can
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
519 ask Vim to format a comment and it will do the right thing.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
520 Suppose, for example, that you have the following comment:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
521
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
522 /* ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
523 * This is a test ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
524 * of the text formatting. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
525 */ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
526
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
527 You then ask Vim to format it by positioning the cursor at the start of the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
528 comment and type: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
529
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
530 gq]/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
531
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
532 "gq" is the operator to format text. "]/" is the motion that takes you to the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
533 end of a comment. The result is:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
534
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
535 /* ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
536 * This is a test of the text formatting. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
537 */ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
538
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
539 Notice that Vim properly handled the beginning of each line.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
540 An alternative is to select the text that is to be formatted in Visual mode
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
541 and type "gq".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
542
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
543 To add a new line to the comment, position the cursor on the middle line and
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
544 press "o". The result looks like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
545
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
546 /* ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
547 * This is a test of the text formatting. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
548 * ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
549 */ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
550
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
551 Vim has automatically inserted a star and a space for you. Now you can type
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
552 the comment text. When it gets longer than 'textwidth', Vim will break the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
553 line. Again, the star is inserted automatically:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
554
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
555 /* ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
556 * This is a test of the text formatting. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
557 * Typing a lot of text here will make Vim ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
558 * break ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
559 */ ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
560
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
561 For this to work some flags must be present in 'formatoptions':
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
562
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
563 r insert the star when typing <Enter> in Insert mode
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
564 o insert the star when using "o" or "O" in Normal mode
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
565 c break comment text according to 'textwidth'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
566
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
567 See |fo-table| for more flags.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
568
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
569
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
570 DEFINING A COMMENT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
571
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
572 The 'comments' option defines what a comment looks like. Vim distinguishes
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
573 between a single-line comment and a comment that has a different start, end
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
574 and middle part.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
575 Many single-line comments start with a specific character. In C++ // is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
576 used, in Makefiles #, in Vim scripts ". For example, to make Vim understand
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
577 C++ comments: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
578
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
579 :set comments=://
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
580
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
581 The colon separates the flags of an item from the text by which the comment is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
582 recognized. The general form of an item in 'comments' is:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
583
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
584 {flags}:{text}
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
585
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
586 The {flags} part can be empty, as in this case.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
587 Several of these items can be concatenated, separated by commas. This
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
588 allows recognizing different types of comments at the same time. For example,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
589 let's edit an e-mail message. When replying, the text that others wrote is
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
590 preceded with ">" and "!" characters. This command would work: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
591
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
592 :set comments=n:>,n:!
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
593
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
594 There are two items, one for comments starting with ">" and one for comments
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
595 that start with "!". Both use the flag "n". This means that these comments
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
596 nest. Thus a line starting with ">" may have another comment after the ">".
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
597 This allows formatting a message like this:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
598
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
599 > ! Did you see that site? ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
600 > ! It looks really great. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
601 > I don't like it. The ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
602 > colors are terrible. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
603 What is the URL of that ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
604 site? ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
605
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
606 Try setting 'textwidth' to a different value, e.g., 80, and format the text by
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
607 Visually selecting it and typing "gq". The result is:
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
608
237
73354c21f1e4 updated for version 7.0066
vimboss
parents: 7
diff changeset
609 > ! Did you see that site? It looks really great. ~
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
610 > I don't like it. The colors are terrible. ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
611 What is the URL of that site? ~
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
612
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
613 You will notice that Vim did not move text from one type of comment to
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
614 another. The "I" in the second line would have fit at the end of the first
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
615 line, but since that line starts with "> !" and the second line with ">", Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
616 knows that this is a different kind of comment.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
617
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
618
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
619 A THREE PART COMMENT
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
620
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
621 A C comment starts with "/*", has "*" in the middle and "*/" at the end. The
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
622 entry in 'comments' for this looks like this: >
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
623
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
624 :set comments=s1:/*,mb:*,ex:*/
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
625
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
626 The start is defined with "s1:/*". The "s" indicates the start of a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
627 three-piece comment. The colon separates the flags from the text by which the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
628 comment is recognized: "/*". There is one flag: "1". This tells Vim that the
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
629 middle part has an offset of one space.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
630 The middle part "mb:*" starts with "m", which indicates it is a middle
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
631 part. The "b" flag means that a blank must follow the text. Otherwise Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
632 would consider text like "*pointer" also to be the middle of a comment.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
633 The end part "ex:*/" has the "e" for identification. The "x" flag has a
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
634 special meaning. It means that after Vim automatically inserted a star,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
635 typing / will remove the extra space.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
636
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
637 For more details see |format-comments|.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
638
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
639 ==============================================================================
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
640
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
641 Next chapter: |usr_31.txt| Exploiting the GUI
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
642
14519
5c5908e81e93 Update runtime files.
Christian Brabandt <cb@256bit.org>
parents: 13963
diff changeset
643 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: