]> git.madduck.net Git - etc/vim.git/blob - docs/installation_and_usage.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:

Symlink docs/change_log.md to CHANGES.md, don't copy (#2146)
[etc/vim.git] / docs / installation_and_usage.md
1 [//]: # "NOTE: THIS FILE WAS AUTOGENERATED FROM README.md"
2
3 # Installation and usage
4
5 ## Installation
6
7 _Black_ can be installed by running `pip install black`. It requires Python 3.6.0+ to
8 run but you can reformat Python 2 code with it, too.
9
10 ### Install from GitHub
11
12 If you can't wait for the latest _hotness_ and want to install from GitHub, use:
13
14 `pip install git+git://github.com/psf/black`
15
16 ## Usage
17
18 To get started right away with sensible defaults:
19
20 ```sh
21 black {source_file_or_directory}
22 ```
23
24 You can run _Black_ as a package if running it as a script doesn't work:
25
26 ```sh
27 python -m black {source_file_or_directory}
28 ```
29
30 ## Command line options
31
32 _Black_ doesn't provide many options. You can list them by running `black --help`:
33
34 ```text
35 Usage: black [OPTIONS] [SRC]...
36
37   The uncompromising code formatter.
38
39 Options:
40   -c, --code TEXT                 Format the code passed in as a string.
41   -l, --line-length INTEGER       How many characters per line to allow.
42                                   [default: 88]
43
44   -t, --target-version [py27|py33|py34|py35|py36|py37|py38|py39]
45                                   Python versions that should be supported by
46                                   Black's output. [default: per-file auto-
47                                   detection]
48
49   --pyi                           Format all input files like typing stubs
50                                   regardless of file extension (useful when
51                                   piping source on standard input).
52
53   -S, --skip-string-normalization
54                                   Don't normalize string quotes or prefixes.
55   -C, --skip-magic-trailing-comma
56                                   Don't use trailing commas as a reason to
57                                   split lines.
58
59   --check                         Don't write the files back, just return the
60                                   status.  Return code 0 means nothing would
61                                   change.  Return code 1 means some files
62                                   would be reformatted. Return code 123 means
63                                   there was an internal error.
64
65   --diff                          Don't write the files back, just output a
66                                   diff for each file on stdout.
67
68   --color / --no-color            Show colored diff. Only applies when
69                                   `--diff` is given.
70
71   --fast / --safe                 If --fast given, skip temporary sanity
72                                   checks. [default: --safe]
73
74   --include TEXT                  A regular expression that matches files and
75                                   directories that should be included on
76                                   recursive searches.  An empty value means
77                                   all files are included regardless of the
78                                   name.  Use forward slashes for directories
79                                   on all platforms (Windows, too).  Exclusions
80                                   are calculated first, inclusions later.
81                                   [default: \.pyi?$]
82
83   --exclude TEXT                  A regular expression that matches files and
84                                   directories that should be excluded on
85                                   recursive searches.  An empty value means no
86                                   paths are excluded. Use forward slashes for
87                                   directories on all platforms (Windows, too).
88                                   Exclusions are calculated first, inclusions
89                                   later.  [default: /(\.direnv|\.eggs|\.git|\.
90                                   hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.sv
91                                   n|_build|buck-out|build|dist)/]
92
93   --force-exclude TEXT            Like --exclude, but files and directories
94                                   matching this regex will be excluded even
95                                   when they are passed explicitly as
96                                   arguments.
97
98   --extend-exclude TEXT           Like --exclude, but adds additional files
99                                   and directories on top of the excluded
100                                   ones. (useful if you simply want to add to
101                                   the default)
102
103   --stdin-filename TEXT           The name of the file when passing it through
104                                   stdin. Useful to make sure Black will
105                                   respect --force-exclude option on some
106                                   editors that rely on using stdin.
107
108   -q, --quiet                     Don't emit non-error messages to stderr.
109                                   Errors are still emitted; silence those with
110                                   2>/dev/null.
111
112   -v, --verbose                   Also emit messages to stderr about files
113                                   that were not changed or were ignored due to
114                                   exclusion patterns.
115
116   --version                       Show the version and exit.
117   --config FILE                   Read configuration from FILE path.
118   -h, --help                      Show this message and exit.
119 ```
120
121 _Black_ is a well-behaved Unix-style command-line tool:
122
123 - it does nothing if no sources are passed to it;
124 - it will read from standard input and write to standard output if `-` is used as the
125   filename;
126 - it only outputs messages to users on standard error;
127 - exits with code 0 unless an internal error occurred (or `--check` was used).
128
129 ## Using _Black_ with other tools
130
131 While _Black_ enforces formatting that conforms to PEP 8, other tools may raise warnings
132 about _Black_'s changes or will overwrite _Black_'s changes. A good example of this is
133 [isort](https://pypi.org/p/isort). Since _Black_ is barely configurable, these tools
134 should be configured to neither warn about nor overwrite _Black_'s changes.
135
136 Actual details on _Black_ compatible configurations for various tools can be found in
137 [compatible_configs](https://github.com/psf/black/blob/master/docs/compatible_configs.md#black-compatible-configurations).
138
139 ## Migrating your code style without ruining git blame
140
141 A long-standing argument against moving to automated code formatters like _Black_ is
142 that the migration will clutter up the output of `git blame`. This was a valid argument,
143 but since Git version 2.23, Git natively supports
144 [ignoring revisions in blame](https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revltrevgt)
145 with the `--ignore-rev` option. You can also pass a file listing the revisions to ignore
146 using the `--ignore-revs-file` option. The changes made by the revision will be ignored
147 when assigning blame. Lines modified by an ignored revision will be blamed on the
148 previous revision that modified those lines.
149
150 So when migrating your project's code style to _Black_, reformat everything and commit
151 the changes (preferably in one massive commit). Then put the full 40 characters commit
152 identifier(s) into a file.
153
154 ```
155 # Migrate code style to Black
156 5b4ab991dede475d393e9d69ec388fd6bd949699
157 ```
158
159 Afterwards, you can pass that file to `git blame` and see clean and meaningful blame
160 information.
161
162 ```console
163 $ git blame important.py --ignore-revs-file .git-blame-ignore-revs
164 7a1ae265 (John Smith 2019-04-15 15:55:13 -0400 1) def very_important_function(text, file):
165 abdfd8b0 (Alice Doe  2019-09-23 11:39:32 -0400 2)     text = text.lstrip()
166 7a1ae265 (John Smith 2019-04-15 15:55:13 -0400 3)     with open(file, "r+") as f:
167 7a1ae265 (John Smith 2019-04-15 15:55:13 -0400 4)         f.write(formatted)
168 ```
169
170 You can even configure `git` to automatically ignore revisions listed in a file on every
171 call to `git blame`.
172
173 ```console
174 $ git config blame.ignoreRevsFile .git-blame-ignore-revs
175 ```
176
177 **The one caveat is that GitHub and GitLab do not yet support ignoring revisions using
178 their native UI of blame.** So blame information will be cluttered with a reformatting
179 commit on those platforms. (If you'd like this feature, there's an open issue for
180 [GitLab](https://gitlab.com/gitlab-org/gitlab/-/issues/31423) and please let GitHub
181 know!)
182
183 ## NOTE: This is a beta product
184
185 _Black_ is already [successfully used](https://github.com/psf/black#used-by) by many
186 projects, small and big. It also sports a decent test suite. However, it is still very
187 new. Things will probably be wonky for a while. This is made explicit by the "Beta"
188 trove classifier, as well as by the "b" in the version number. What this means for you
189 is that **until the formatter becomes stable, you should expect some formatting to
190 change in the future**. That being said, no drastic stylistic changes are planned,
191 mostly responses to bug reports.
192
193 Also, as a temporary safety measure, _Black_ will check that the reformatted code still
194 produces a valid AST that is equivalent to the original. This slows it down. If you're
195 feeling confident, use `--fast`.