Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-doc
Path: blob/main/documentation/content/en/books/fdp-primer/editor-config/_index.adoc
18099 views
---
title: Chapter 13. Editor Configuration
prev: books/fdp-primer/writing-style
next: books/fdp-primer/trademarks
description: Configuration used in the texts editors in the FreeBSD Documentation Project
tags: ["editor", "configuration", "vim", "emacs", "FreeBSD"]
showBookMenu: true
weight: 13
params:
  path: "/books/fdp-primer/editor-config/"
---

[[editor-config]]
= Editor Configuration
:doctype: book
:toc: macro
:toclevels: 1
:icons: font
:sectnums:
:sectnumlevels: 6
:sectnumoffset: 13
:partnums:
:source-highlighter: rouge
:experimental:
:images-path: books/fdp-primer/

ifdef::env-beastie[]
ifdef::backend-html5[]
:imagesdir: ../../../../images/{images-path}
endif::[]
ifndef::book[]
include::shared/authors.adoc[]
include::shared/mirrors.adoc[]
include::shared/releases.adoc[]
include::shared/attributes/attributes-{{% lang %}}.adoc[]
include::shared/{{% lang %}}/teams.adoc[]
include::shared/{{% lang %}}/mailing-lists.adoc[]
include::shared/{{% lang %}}/urls.adoc[]
toc::[]
endif::[]
ifdef::backend-pdf,backend-epub3[]
include::../../../../../shared/asciidoctor.adoc[]
endif::[]
endif::[]

ifndef::env-beastie[]
toc::[]
include::../../../../../shared/asciidoctor.adoc[]
endif::[]

Adjusting your text editor configuration can make working on document files quicker and easier, and help documents conform to FDP guidelines.

[[editor-config-vim]]
== Vim

Install from package:editors/vim[], then follow the configuration instructions in crossref:editor-config[editor-config-vim-config, Configuration].
More advanced users can use a proper linter like link:https://github.com/dense-analysis/ale[Ale] which can also act as a Vim link:https://langserver.org/[Language Server Protocol] client.

[[editor-config-vim-use]]
=== Use

Manual page writers can use the following keyboard shortcuts to reformat:

* Press kbd:[P] to reformat paragraphs or text that has been selected in Visual mode.
* Press kbd:[T] to replace groups of eight spaces with a tab.

A linter named link:https://vale.sh[Vale] has been introduced to check grammatical and cosmetic errors on the documents.
Vale has support for various editors and IDEs.

Vale may already be installed as a dependency of the package:textproc/docproj[] meta-port.
If not, install package:textproc/vale[] with:

[source,console]
....
$ pkg install vale
....

Install link:https://github.com/dense-analysis/ale[Ale] to integrate into package:editors/vim[], for using package:textproc/vale[].

[source,console?prompt=%]
....
% mkdir -p ~/.vim/pack/vendor/start
% git clone --depth 1 https://github.com/dense-analysis/ale.git ~/.vim/pack/vendor/start/ale
....

Users who are using plugin managers for package:editors/vim[] do not need the above and should follow the instructions of that plugin manager to install link:https://github.com/dense-analysis/ale[Ale].

At this moment due to a bug in link:https://vale.sh[Vale] it is necessary to copy the link:https://vale.sh[Vale] configuration to the home directory.
Considering the repository was cloned into [.filename]#~/doc# copy as following:

[source,console?prompt=%]
....
% cp -R ~/doc/.vale* ~/
....

[[editor-config-vim-config]]
=== Configuration

Edit [.filename]#~/.vimrc#, adding these lines to the end of the file:

[source.programlisting,viml]
.`~/.vimrc`
....
if has("autocmd")
  au BufNewFile,BufRead *.adoc call Set_ADOC()
  au BufNewFile,BufRead *.[1-9] call Set_MAN()
endif " has(autocmd)

function Set_Highlights()
  "match ExtraWhitespace /^\s* \s*\|\s\+$/
  return 0
endfunction " Set_Highlights_Adoc()

function Set_Highlights_MAN()
  highlight default link OverLength ErrorMsg
  match OverLength /\%71v.\+/
  return 0
endfunction " Set_Highlights_MAN()

function ShowSpecial()
  setlocal list listchars=tab:>>,trail:*,eol:$
  hi def link nontext ErrorMsg
  return 0
endfunction " ShowSpecial()

function Set_COMMON()
  setlocal number
  setlocal shiftwidth=2
  setlocal tabstop=8
  setlocal softtabstop=2
  setlocal formatprg="fmt -p"
  setlocal autoindent
  setlocal smartindent
  call ShowSpecial()
  call Set_Highlights()
  return 0
endfunction " Set_COMMON()

function Set_ADOC()
  setlocal syntax=asciidoc
  setlocal filetype=asciidoc
  call Set_COMMON()
  return 0
endfunction " Set_ADOC()

function Set_MAN()
  setlocal syntax=man
  setlocal filetype=man
  setlocal textwidth=70
  " Rewrap paragraphs
  noremap P gqj
  " Replace spaces with tabs
  noremap T :s/        /\t/<CR>
  call Set_COMMON()
  call Set_Highlights_MAN()
  return 0
endfunction " Set_Man()

let g:ale_fixers = {
\   '*': ['remove_trailing_lines', 'trim_whitespace'],
\}
let g:ale_linters = {
\   'asciidoc': ['vale'],
\}
let g:ale_fix_on_save = 1
....

[IMPORTANT]
======
Above configuration will automatically remove trailing line, trailing space and multiple spaces which might display additional unwanted changes in `git diff` output.
In such cases properly mention that in the commit log.
======

[[editor-config-emacs]]
== Emacs

Install from package:editors/emacs[] or package:editors/emacs-devel[].

[[editor-config-emacs-igor]]
=== Automated Proofreading with Flycheck and Igor

The link:https://www.flycheck.org/[Flycheck] package is available from link:https://melpa.org/[Milkypostman's Emacs Lisp Package Archive] (MELPA).
If MELPA is not already in Emacs's packages-archives, it can be added by evaluating

[source,emacs-lisp]
....
(add-to-list 'package-archives '("melpa" . "http://stable.melpa.org/packages/") t)
....

Add the line to Emacs's initialization file (one of [.filename]#~/.emacs#, [.filename]#~/.emacs.el#, or [.filename]#~.emacs.d/init.el#) to make this change permanent.

To install Flycheck, evaluate

[source,emacs-lisp]
....
(package-install 'flycheck)
....

Create a Flycheck checker for package:textproc/igor[] by evaluating

[source,emacs-lisp]
....
(flycheck-define-checker igor
  "FreeBSD Documentation Project sanity checker.

See URLs https://www.freebsd.org/docproj/ and
http://www.freshports.org/textproc/igor/."
  :command ("igor" "-X" source-inplace)
  :error-parser flycheck-parse-checkstyle
  :modes (nxml-mode)
  :standard-input t)

  (add-to-list 'flycheck-checkers 'igor 'append)
....

Again, add these lines to Emacs's initialization file to make the changes permanent.

[[editor-config-emacs-specifc]]
=== FreeBSD Documentation Specific Settings

To apply settings specific to the FreeBSD documentation project, create [.filename]#.dir-locals.el# in the root directory of the documentation repository and add these lines to the file:

[source,emacs-lisp]
....
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((nxml-mode
  (eval . (turn-on-auto-fill))
  (fill-column . 70)
  (eval . (require 'flycheck))
  (eval . (flycheck-mode 1))
  (flycheck-checker . igor)
  (eval . (add-to-list 'rng-schema-locating-files "~/.emacs.d/schema/schemas.xml"))))
....

[[editor-config-nano]]
== nano

Install from package:editors/nano[].

[[editor-config-nano-config]]
=== Configuration

Currently there is no adoc/asciidoc syntax highlight file with nano distribution.
So let's create one from scratch and use an editor to create new file or add lines in the [.filename]#~/.nanorc# with these contents:

[source]
.`~/.nanorc`
....
syntax "asciidoc" "\.(adoc|asc|asciidoc)$"
# main header
color red "^====+$"
# h1
color red "^==[[:space:]].*$"
color red "^----+$"
# h2
color magenta "^===[[:space:]].*$"
color magenta "^~~~~+$"
# h4
color green "^====[[:space:]].*$"
color green "^\^\^\^\^+$"
# h5
color brightblue "^=====[[:space:]].*$"
color brightblue "^\+\+\+\++$"
# attributes
color brightgreen ":.*:"
color brightred "\{[a-z0-9]*\}"
color red "\\\{[a-z0-9]*\}"
color red "\+\+\+\{[a-z0-9]*\}\+\+\+"
# Paragraph Title
color yellow "^\..*$"
# source
color magenta "^\[(source,.+|NOTE|TIP|IMPORTANT|WARNING|CAUTION)\]"
# Other markup
color yellow ".*[[:space:]]\+$"
color yellow "_[^_]+_"
color yellow "\*[^\*]+\*"
color yellow "\+[^\+]+\+"
color yellow "`[^`]+`"
color yellow "\^[^\^]+\^"
color yellow "~[^~]+~"
color yellow "'[^']+'"
color cyan "`{1,2}[^']+'{1,2}"
# bullets
color brightmagenta "^[[:space:]]*[\*\.-]{1,5}[[:space:]]"
# anchors
color brightwhite "\[\[.*\]\]"
color brightwhite "<<.*>>"
# trailing whitespace
color ,blue "[[:space:]]+$"
# multiples of eight spaces at the start a line
# (after zero or more tabs) should be a tab
color ,blue "^([TAB]*[ ]{8})+"
# tabs after spaces
color ,yellow "( )+TAB"
# highlight indents that have an odd number of spaces
color ,red "^(([ ]{2})+|(TAB+))*[ ]{1}[^ ]{1}"
....

Process the file to create embedded tabs:

[source,console?prompt=%]
....
% perl -i'' -pe 's/TAB/\t/g' ~/.nanorc
....

[[editor-config-nano-use]]
=== Use

Specify additional helpful options when running the editor:

[source,console?prompt=%]
....
% nano -AKipwz -T8 _index.adoc
....

Users of man:csh[1] can define an alias in [.filename]#~/.cshrc# to automate these options:

[source,shell]
....
alias nano "nano -AKipwz -r 70 -T8"
....

After the alias is defined, the options will be added automatically:

[source,console?prompt=%]
....
% nano _index.adoc
....