Friday, March 17, 2017

Re: Automatically add two spaces between sentences

Den 2017-03-16 kl. 15:24, skrev BPJ:
> You can make one iabb turning ".<space>" into ".<space><space>" and
> another, e.g. ".~" for generating ".<space>" when you really want that. You
> can also have separate iabbs for the most frequent abbreviations expanding
> to themselves, like Mr. in English.
>
> /bpj

I came up with a way to do this which actually works: a keymap and
a couple of autocommands.
(See :h mbyte-keymap and :h 'kmp').

Don't use these when writing code! :-)

Put the following in a file ~/.vim/keymap/punctspace_utf-8.vim


" Vim: set noet ts=20 sts=20 list:

" short name for keymap
let b:keymap_name = "pct"
highlight lCursor guibg=red guifg=NONE
scriptencoding utf-8

loadkeymap
" leave this line blank!

" Punctuation plus space

.<Space> .<char-0xa0><Space>
!<Space> !<char-0xa0><Space>
?<Space> ?<char-0xa0><Space>
..<Space> .<char-0xa0> " dot plus non-breaking space
..<Space><Space> .<Space> " dot plus ordinary space
!!<Space> !<Space>
??<Space> ?<Space>

" Abbreviations

" You will want to add more and/or those for your language

Mr.<Space> Mr.<char-0xa0>
Mrs.<Space> Mrs.<char-0xa0>
Dr.<Space> Dr.<char-0xa0>
e.g.<Space> e.g.<char-0xa0>
i.e.<Space> i.e.<char-0xa0>
viz.<Space> viz.<char-0xa0>
" more...

Then whenever you need elegant handling of punctuation +
whitespace in prose just say

:setl kmp=punctspace

The point of using a non-breaking space after abbreviations is so
that you won't get linebreaks after abbreviations, which is
considered bad style.

The point of inserting a non-breaking space plus an ordinary space
at the end of a sentence is so that the double space will be
preserved when reformatting, but lines can break at the ordinary
space anyway. You almost certainly want to define these
autocommands (where `<nbsp>` and `<space>` mean that you should
type `<C-K>NS` (digraph for non-breaking space) and an ordinary
space respectively at those points):

" Replace dot + nbsp + space with dot + space + space before
writing Markdown files.
:au BufWritePre *.md %s/\.<npsp><space>/.<space><space>/g

" Replace dot + nbsp + newline with dot + newline before
writing Markdown files.
:au BufWritePre *.md %s/\.<npsp>$/./g

" The reverse actions after writing a Markdown file.
:au BufWritePost *.md %s/\.<space><space>/.<nbsp><space>/g
:au BufWritePost *.md %s/\.$/.<nbsp>/g

I use `*.md` in the examples because that fits my use case. You
may want to substitute or add `*.txt` or something else.

Note that the non-breaking spaces after abbreviations are
preserved when writing the file. That is intentional!

" Vim: set et ts=4 sts=4 list:

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: