âExtending Vim to better support typographic (âcurlyâ) quote characters.â
While Vim is renowned for its text manipulation capabilities, it nevertheless retains a bias towards ASCII that stretches back to its vi roots on Unix. This can limit Vimâs appeal for those who prefer typographic characters like âcurly quotesâ over ASCII "straight quotes" in the prose and documentation they write.
Core features of this plugin:
- Text object supporting âtypographic quotesâ, incl. motion commands
- Implemented with regular expressions via the kana/vim-textobj-user plugin
- Supports quoted strings containing contractions (
âdonâtâ
, e.g.) - Configurable to support international variations in quotation marks
Includes four additional features:
- educate - automatic entry of âtypographic quotesâ from the 'straight quote' keys
- replace - transform quotes from straight to typographic, and vice versa
- matchit -
%
matching for typographic quote pairs - surround - surround a word or visual selection with quotes
Requires a recent version of Vim compiled with Unicode support.
Install using Pathogen, Vundle, Neobundle, or your favorite Vim package manager.
This plugin has an essential dependency that you will need to install:
- kana/vim-textobj-user - a Vim plugin to create your own text objects without pain
Because you won't want typographic quotes in your code, the behavior of
this plugin can be configured per file type. For example, to enable
typographic quote support in markdown
and textile
files, place in your
.vimrc
:
set nocompatible
filetype plugin on " may already be in your .vimrc
augroup textobj_quote
autocmd!
autocmd FileType markdown call textobj#quote#init()
autocmd FileType textile call textobj#quote#init()
autocmd FileType text call textobj#quote#init({'educate': 0})
augroup END
The last autocmd
statement initializes the plugin for buffers of text
file type, but disables the âeducateâ feature by default. More on that
below.
Motion commands on text objects are a powerful feature of Vim.
By default, for motion commands, q
denotes an operation on âdoubleâ
quotes. Q
for âsingleâ quotes. For example, with the c
change
operator:
caq
- change around âdoubleâ quotes - includes quote charsciq
- change inside âdoubleâ quotes - excludes quote charscaQ
- change around âsingleâ quotes - includes quote charsciQ
- change inside âsingleâ quotes - excludes quote chars
Apart from c
for change, you can v
for visual selection, d
for
deletion, y
for yanking to clipboard, etc. Note that count isnât
supported at present (due to limitations of the underlying
vim-textobj-user) but repeat with .
should work.
quoteâs motion command is smart too, able to distinguish between an
apostrophe and single closing quote, even though both are represented by
the same glyph. For example, try out viQ
on the following sentence:
âReally, Iâd rather not relive the â70s,â said zombie Elvis.
You can change these key mappings from their defaults in your .vimrc
:
let g:textobj#quote#doubleMotion = 'q'
let g:textobj#quote#singleMotion = 'Q'
The four additional features of this plugin include: educate, matchit, replace, and surround.
This plugin will âeducateâ quotes, meaning that while in Insert mode,
your straight quote key presses ("
or '
) will be dynamically
transformed into the appropriate typographic quote characters.
For example, entering the following sentence without the educate feature using the straight quote keys:
"It's Dr. Evil. I didn't spend six years in Evil Medical
School to be called 'mister,' thank you very much."
As expected all the quotes are straight ones. But with the educate feature, the straight quotes are transformed into the typographic equivalent as you type:
âItâs Dr. Evil. I didnât spend six years in Evil Medical
School to be called âmister,â thank you very much.â
You can configure the default settings for the educate feature in your
.vimrc
:
let g:textobj#quote#educate = 1 " 0=disable, 1=enable (def)
You can change educating behavior with the following commands:
Educate
NoEducate
ToggleEducate
As seen above, educating behavior can be configured as a parameter in the
textobj#quote#init()
call.
In some cases, straight (ASCII) quotes are needed, such as:
âprint "Hello World!"â is a simple program you can write in Python.
To insert a straight quote while educating, enter «Ctrl-V»
(mnemonic is
verbatim) before the quote key:
«Ctrl-V» "
- straight double quote«Ctrl-V» '
- straight single quote
Note that for units of measurement youâll want to use the prime symbol(s) rather than straight quotes, as in:
Standing at 7âČ3âł (2.21 m), Hasheem Thabeet of the Oklahoma City Thunder
is the tallest player in the NBA.
matchit enables jumping to matching typographic quotes.
%
- jump to the matching typographic (curly) quote character
You can configure this feature in your .vimrc
:
let g:textobj#quote#matchit = 1 " 0=disable, 1=enable (def)
You can replace straight quotes in existing text with curly quotes, and
visa versa. Add key mappings of your choice to your .vimrc
:
map <silent> <leader>qc <Plug>ReplaceWithCurly
map <silent> <leader>qs <Plug>ReplaceWithStraight
Both Normal and Visual modes are supported by this feature. (In Normal mode, quotes in the current paragraph are replaced.)
To transform all quotes in a document, use Visual mode to select the entire document.
By default there are no key mappings for surround
support.
This feature supports basic surround capabilities. Add to your .vimrc
key mappings of your choice:
" NOTE: remove these mappings if using the tpope/vim-surround plugin!
map <silent> Sq <Plug>SurroundWithDouble
map <silent> SQ <Plug>SurroundWithSingle
Then you can use âmotion commandsâ to surround text with quotes:
(an asterisk is used to denote the cursor position)
visSq
- My senten*ce. => âMy sentence.âvisSQ
- My senten*ce. => âMy sentence.â
Using Tim Popeâs vim-surround plugin your text object key mappings should be available. For example,
cs'q
- 'Hello W*orld' => âHello Worldâcs"q
- "Hello W*orld" => âHello Worldâcs(q
- (Hello W*orld) => âHello Worldâcs(Q
- (Hello W*orld) => âHello Worldâ
Sometimes you must enter special characters (like typographic quotes) manually, such as in a search expression. You can do so through Vimâs digraphs or via your operating systemâs keyboard shortcuts.
Glyph | Vim Digraph | OS X | Description |
---|---|---|---|
â |
'6 |
Opt-] |
left single quotation mark |
â |
'9 |
Shift-Opt-] |
right single quotation mark |
â |
"6 |
Opt-[ |
left double quotation mark |
â |
"9 |
Shift-Opt-[ |
right double quotation mark |
â |
.9 |
single low-9 quote | |
â |
:9 |
Shift-Opt-w |
double low-9 quote |
âč |
1< |
Opt-\ |
left pointing single quotation mark |
âș |
1> |
Shift-Opt-\ |
right pointing single quotation mark |
« |
<< |
Opt-\ |
left pointing double quotation mark |
» |
>> |
Shift-Opt-\ |
right pointing double quotation mark |
âČ |
1' |
single prime | |
âł |
2' |
double prime | |
â |
-N |
Opt-hyphen |
en dash |
â |
-M |
Shift-Opt-hyphen |
em dash |
⊠|
,. |
Opt-; |
horizontal ellipsis |
 |
NS |
non-breaking space | |
ĂŻ |
i: |
Opt-U i |
lowercase i, umlaut |
ĂŠ |
ae |
Opt-' |
lowercase ae |
For example, to enter left double quotation mark â
, precede the digraph code
"6
with Ctrl-K
, like
«Ctrl-K» "6
Alternatively, if youâre on OS X, you can use Opt-[
to enter this
character.
For more details, see:
:help digraphs
Many international keyboards feature keys to allow you to input typographic quote characters directly. In such cases, you wonât need to change the behavior of the straight quote keys.
But if you do, you can override the defaults. For example, those users editing most of their prose in German could change those defaults to:
let g:textobj#quote#doubleDefault = 'ââ' " âdoppelâ
let g:textobj#quote#singleDefault = 'ââ' " âeinzelâ
Or on a file type initialization...
augroup textobj_quote
autocmd!
autocmd FileType markdown call textobj#quote#init({ 'double':'ââ', 'single':'ââ' })
...
augroup END
Or in a key mapping...
nnoremap <silent> <leader>qd :call textobj#quote#init({ 'double':'ââ', 'single':'ââ' })<cr>
If you find this plugin useful, check out these others originally by @reedes:
- vim-colors-pencil - color scheme for Vim inspired by IA Writer
- vim-lexical - building on Vimâs spell-check and thesaurus/dictionary completion
- vim-litecorrect - lightweight auto-correction for Vim
- vim-pencil - rethinking Vim as a tool for writers
- vim-textobj-sentence - improving on Vim's native sentence motion command
- vim-thematic - modify Vimâs appearance to suit your task and environment
- vim-wheel - screen-anchored cursor movement for Vim
- vim-wordy - uncovering usage problems in writing
- vim-wordchipper - power tool for shredding text in Insert mode
If youâve spotted a problem or have an idea on improving this plugin, please post it to the GitHub project issue page.