diff --git a/doc/help/configuring.asciidoc b/doc/help/configuring.asciidoc index afb6ec8e6..62b10ebf5 100644 --- a/doc/help/configuring.asciidoc +++ b/doc/help/configuring.asciidoc @@ -262,3 +262,106 @@ qutebrowser tries to display errors which are easy to understand even for people who are not used to writing Python. If you see a config error which you find confusing or you think qutebrowser could handle better, please https://github.com/qutebrowser/qutebrowser/issues[open an issue]! + +Recipes +~~~~~~~ + +Reading a YAML file +^^^^^^^^^^^^^^^^^^^ + +To read a YAML config like this: + +.config.yml: +---- +tabs.position: left +tabs.show: switching +---- + +You can use: + +.config.py: +[source,python] +---- +import yaml + +with (config.configdir / 'config.yml').open() as f: + yaml_data = yaml.load(f) + +for k, v in yaml_data.items(): + config.set(k, v) +---- + +Reading a nested YAML file +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +To read a YAML file with nested values like this: + +.colors.yml: +---- +colors: + statusbar: + normal: + bg: lime + fg: black + url: + fg: red +---- + +You can use: + +.config.py: +[source,python] +---- +import yaml + +with (config.configdir / 'colors.yml').open() as f: + yaml_data = yaml.load(f) + +def dict_attrs(obj, path=''): + if isinstance(obj, dict): + for k, v in obj.items(): + yield from dict_attrs(v, '{}.{}'.format(path, k) if path else k) + else: + yield path, obj + +for k, v in dict_attrs(yaml_data): + config.set(k, v) +---- + +Note that this won't work for values which are dictionaries. + +Binding chained commands +^^^^^^^^^^^^^^^^^^^^^^^^ + +If you have a lot of chained comamnds you want to bind, you can write a helper +to do so: + +[source,python] +---- +def bind_chained(key, *commands, force=False): + config.bind(key, ' ;; '.join(commands), force=force) + +bind_chained('', 'clear-keychain', 'search', force=True) +---- + +Avoiding flake8 errors +^^^^^^^^^^^^^^^^^^^^^^ + +If you use an editor with flake8 integration which complains about `c` and `config` being undefined, you can use: + +[source,python] +---- +c = c # noqa: F821 +config = config # noqa: F821 +---- + +For type annotation support (note that those imports aren't guaranteed to be +stable across qutebrowser versions): + +[source,python] +---- +from qutebrowser.config.configfiles import ConfigAPI # noqa: F401 +from qutebrowser.config.config import ConfigContainer # noqa: F401 +config = config # type: ConfigAPI # noqa: F821 +c = c # type: ConfigContainer # noqa: F821 +----