]> git.madduck.net Git - etc/vim.git/blob - docs/usage_and_configuration/the_basics.md

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Reorganize docs v2 (GH-2174)
[etc/vim.git] / docs / usage_and_configuration / the_basics.md
1 # The basics
2
3 Foundational knowledge on using and configuring Black.
4
5 _Black_ is a well-behaved Unix-style command-line tool:
6
7 - it does nothing if no sources are passed to it;
8 - it will read from standard input and write to standard output if `-` is used as the
9   filename;
10 - it only outputs messages to users on standard error;
11 - exits with code 0 unless an internal error occurred (or `--check` was used).
12
13 ## Usage
14
15 To get started right away with sensible defaults:
16
17 ```sh
18 black {source_file_or_directory}
19 ```
20
21 You can run _Black_ as a package if running it as a script doesn't work:
22
23 ```sh
24 python -m black {source_file_or_directory}
25 ```
26
27 ### Command line options
28
29 _Black_ has quite a few knobs these days, although _Black_ is opinionated so style
30 configuration options are deliberately limited and rarely added. You can list them by
31 running `black --help`.
32
33 <details>
34
35 <summary>Help output</summary>
36
37 ```
38   Usage: black [OPTIONS] [SRC]...
39
40     The uncompromising code formatter.
41
42   Options:
43     -c, --code TEXT                 Format the code passed in as a string.
44     -l, --line-length INTEGER       How many characters per line to allow.
45                                     [default: 88]
46
47     -t, --target-version [py27|py33|py34|py35|py36|py37|py38|py39]
48                                     Python versions that should be supported by
49                                     Black's output. [default: per-file auto-
50                                     detection]
51
52     --pyi                           Format all input files like typing stubs
53                                     regardless of file extension (useful when
54                                     piping source on standard input).
55
56     -S, --skip-string-normalization
57                                     Don't normalize string quotes or prefixes.
58     -C, --skip-magic-trailing-comma
59                                     Don't use trailing commas as a reason to
60                                     split lines.
61
62     --check                         Don't write the files back, just return the
63                                     status. Return code 0 means nothing would
64                                     change. Return code 1 means some files
65                                     would be reformatted. Return code 123 means
66                                     there was an internal error.
67
68     --diff                          Don't write the files back, just output a
69                                     diff for each file on stdout.
70
71     --color / --no-color            Show colored diff. Only applies when
72                                     `--diff` is given.
73
74     --fast / --safe                 If --fast given, skip temporary sanity
75                                     checks. [default: --safe]
76
77     --include TEXT                  A regular expression that matches files and
78                                     directories that should be included on
79                                     recursive searches. An empty value means
80                                     all files are included regardless of the
81                                     name. Use forward slashes for directories
82                                     on all platforms (Windows, too). Exclusions
83                                     are calculated first, inclusions later.
84                                     [default: \.pyi?$]
85
86     --exclude TEXT                  A regular expression that matches files and
87                                     directories that should be excluded on
88                                     recursive searches. An empty value means no
89                                     paths are excluded. Use forward slashes for
90                                     directories on all platforms (Windows, too).
91                                     Exclusions are calculated first, inclusions
92                                     later.  [default: /(\.direnv|\.eggs|\.git|\.
93                                     hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.svn|_bu
94                                     ild|buck-out|build|dist)/]
95
96     --extend-exclude TEXT           Like --exclude, but adds additional files
97                                     and directories on top of the excluded
98                                     ones (useful if you simply want to add to
99                                     the default).
100
101     --force-exclude TEXT            Like --exclude, but files and directories
102                                     matching this regex will be excluded even
103                                     when they are passed explicitly as
104                                     arguments.
105
106
107     --stdin-filename TEXT           The name of the file when passing it through
108                                     stdin. Useful to make sure Black will
109                                     respect --force-exclude option on some
110                                     editors that rely on using stdin.
111
112     -q, --quiet                     Don't emit non-error messages to stderr.
113                                     Errors are still emitted; silence those with
114                                     2>/dev/null.
115
116     -v, --verbose                   Also emit messages to stderr about files
117                                     that were not changed or were ignored due to
118                                     exclusion patterns.
119
120     --version                       Show the version and exit.
121     --config FILE                   Read configuration from FILE path.
122     -h, --help                      Show this message and exit.
123 ```
124
125 </details>
126
127 ## Configuration via a file
128
129 _Black_ is able to read project-specific default values for its command line options
130 from a `pyproject.toml` file. This is especially useful for specifying custom
131 `--include` and `--exclude`/`--force-exclude`/`--extend-exclude` patterns for your
132 project.
133
134 **Pro-tip**: If you're asking yourself "Do I need to configure anything?" the answer is
135 "No". _Black_ is all about sensible defaults. Applying those defaults will have your
136 code in compliance with many other _Black_ formatted projects.
137
138 ### What on Earth is a `pyproject.toml` file?
139
140 [PEP 518](https://www.python.org/dev/peps/pep-0518/) defines `pyproject.toml` as a
141 configuration file to store build system requirements for Python projects. With the help
142 of tools like [Poetry](https://python-poetry.org/) or
143 [Flit](https://flit.readthedocs.io/en/latest/) it can fully replace the need for
144 `setup.py` and `setup.cfg` files.
145
146 ### Where _Black_ looks for the file
147
148 By default _Black_ looks for `pyproject.toml` starting from the common base directory of
149 all files and directories passed on the command line. If it's not there, it looks in
150 parent directories. It stops looking when it finds the file, or a `.git` directory, or a
151 `.hg` directory, or the root of the file system, whichever comes first.
152
153 If you're formatting standard input, _Black_ will look for configuration starting from
154 the current working directory.
155
156 You can use a "global" configuration, stored in a specific location in your home
157 directory. This will be used as a fallback configuration, that is, it will be used if
158 and only if _Black_ doesn't find any configuration as mentioned above. Depending on your
159 operating system, this configuration file should be stored as:
160
161 - Windows: `~\.black`
162 - Unix-like (Linux, MacOS, etc.): `$XDG_CONFIG_HOME/black` (`~/.config/black` if the
163   `XDG_CONFIG_HOME` environment variable is not set)
164
165 Note that these are paths to the TOML file itself (meaning that they shouldn't be named
166 as `pyproject.toml`), not directories where you store the configuration. Here, `~`
167 refers to the path to your home directory. On Windows, this will be something like
168 `C:\\Users\UserName`.
169
170 You can also explicitly specify the path to a particular file that you want with
171 `--config`. In this situation _Black_ will not look for any other file.
172
173 If you're running with `--verbose`, you will see a blue message if a file was found and
174 used.
175
176 Please note `blackd` will not use `pyproject.toml` configuration.
177
178 ### Configuration format
179
180 As the file extension suggests, `pyproject.toml` is a
181 [TOML](https://github.com/toml-lang/toml) file. It contains separate sections for
182 different tools. _Black_ is using the `[tool.black]` section. The option keys are the
183 same as long names of options on the command line.
184
185 Note that you have to use single-quoted strings in TOML for regular expressions. It's
186 the equivalent of r-strings in Python. Multiline strings are treated as verbose regular
187 expressions by Black. Use `[ ]` to denote a significant space character.
188
189 <details>
190 <summary>Example <code>pyproject.toml</code></summary>
191
192 ```toml
193 [tool.black]
194 line-length = 88
195 target-version = ['py37']
196 include = '\.pyi?$'
197 extend-exclude = '''
198 # A regex preceded with ^/ will apply only to files and directories
199 # in the root of the project.
200 ^/foo.py  # exclude a file named foo.py in the root of the project (in addition to the defaults)
201 '''
202 ```
203
204 </details>
205
206 ### Lookup hierarchy
207
208 Command-line options have defaults that you can see in `--help`. A `pyproject.toml` can
209 override those defaults. Finally, options provided by the user on the command line
210 override both.
211
212 _Black_ will only ever use one `pyproject.toml` file during an entire run. It doesn't
213 look for multiple files, and doesn't compose configuration from different levels of the
214 file hierarchy.
215
216 ## Next steps
217
218 You've probably noted that not all of the options you can pass to _Black_ have been
219 covered. Don't worry, the rest will be covered in a later section.
220
221 A good next step would be configuring auto-discovery so `black .` is all you need
222 instead of laborously listing every file or directory. You can get started by heading
223 over to [File collection and discovery](./file_collection_and_discovery.md).
224
225 Another good choice would be setting up an
226 [integration with your editor](../integrations/editors.md) of choice or with
227 [pre-commit for source version control](../integrations/source_version_control.md).