To insert an en dash, I use
^v u 2014 which is seven keypresses. Is there an easier way? (I was looking at digraphs and didn't find en dash there.)
You can use digraphs: Ctrl+k-N (with a capital
In Insert mode, digraphs are entered by Ctrl+k followed by a two-character sequence. For en-dash, the sequence is
As Quincy pointed out,
U+2014 is em-dash. If you want to enter that using digraphs, the sequence is
If there isn't a predefined digraph or you want a more memorable key sequence, then you can define digraphs in your vimrc. For example, the below will define two digraphs for en dash (8211) and em dash (8212) which mirror their XCompose sequences.
digraph -. 8211 -- 8212
:digraph command expects the value to be specified in decimal rather than hex, which is why it's 8211 for en dash rather than 2013.
The standard way of inserting an en dash in Vim is to use a digraph, as tommcdo explained.
However, I find digraphs in general, and particularly this one (Ctrl+k, -, Shift+N) pretty awkward to type. For faster input of dashes, you could add the following pair of insert-mode mappings to a filetype file, such as
For en/em dashes, respectively:
inoremap <buffer> --<space> –<space> inoremap <buffer> -- —
<buffer> means this mapping will only apply to Markdown files. This is necessary because there are some contexts (e.g. programming) where I don't want two hyphens to be replaced with dashes.
These mappings take advantage of the fact that in the contexts I usually use them, en dashes are surrounded by spaces, and em dashes are not.* Using these mappings therefore means that I can use exactly the same keypresses in the less capable text editors I'm sometimes forced to use and still get a sensible output.**
I actually use a slightly more complicated solution than the above, because, for typographical reasons, I like to surround my em-dashes with hair spaces. Having these in the Markdown makes it less readable, but luckily, Vim has a solution for this, with its "conceal" function, which allows you to hide certain syntax items when the cursor is on a different line:***
inoremap <buffer> --<space> –<space> inoremap <buffer> --  —  syntax match entity_hairspace " " conceal setl conceallevel=2
* If I ever need to use an en dash unspaced, then I can simply press the backspace after typing the mapping.
** If you don't generally use en-dashes like this, or if you don't ever venture out of Vim, you could use something else to trigger the mappings: e.g.
---. If you use other triggers, you could also place this directly in your .vimrc, and remove the
*** You can also use a conceal character
cchar to replace syntax items with a single character, instead of hiding them entirely. If you're worried about encoding issues, you can use this to get Vim to display
– HTML entities in the file as en dashes.
On my French AZERTY Mac keyboard, I can insert an en dash with Alt+-. Your mileage may obviously vary.
Install my unicode plugin, enter Vim and type DASH followed by CtrlXCtrlZ to activate unicode completion.
(Note the -N in the description which shows the digraph to enter that character)
Inspired by Rich's great answer I've added the following lines to my vimrc:
" For en/em dashes, respectively autocmd FileType markdown imap -- – autocmd FileType markdown imap --- —
Works like a treat!
For an en dash a digraph is the best way to go, as suggested in various other answers. But for arbitrary unicode characters, another way is to use
^V in insert mode (see
For an en dash that'd be for example