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.)
7 Answers
You can use digraphs: Ctrl+k-N (with a capital N
)
In Insert mode, digraphs are entered by Ctrl+k followed by a two-character sequence. For en-dash, the sequence is -N
.
As Quincy pointed out, U+2014
is em-dash. If you want to enter that using digraphs, the sequence is -M
.
-
-
U+2014
is not en-dash? my whole life was a lie! Edit: now I get it: my terminal font (Inconsolata) renders both dashes the same way. Feb 25, 2015 at 15:11 -
3To arrive at this answer: hex 2013 is decimal 8211. Use the
:digraphs
command to display all digraphs, and look for 8211. Feb 25, 2015 at 18:04
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
The :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.
-
-
1@200_success I didn't say it wasn't. :) Vim's digraph choices don't always align with more standard ones, so knowing how to define your own digraph is useful, even if one already exists.– jamessanFeb 25, 2015 at 21:49
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 .vim/after/ftplugin/markdown.vim
.
For en/em dashes, respectively:
inoremap <buffer> --<space> –<space>
inoremap <buffer> -- —
N.B. The <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. --.
and ---
. If you use other triggers, you could also place this directly in your .vimrc, and remove the <buffer>
options.
*** 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.
-
So you use dashes in Vim? Don't they look kind of odd, since they are only given one column-width? Do you know of any tricks to make em-dashes actually take up a good amount of space, perhaps two columns' worth? Feb 28, 2018 at 21:35
-
@KyleStrand How hacky do you want to get? Try this:
set conceallevel=1 | syntax match longdash '—' conceal cchar=-
(Note that the first dash in this command is an em-dash, and the second is a fullwidth hyphen-minus.)– RichFeb 28, 2018 at 22:18 -
I don't see a difference in the display, or see why that would make the display take up two columns. Did you by chance misunderstand my question, or is it likely I'm misunderstanding your suggestion? Feb 28, 2018 at 22:58
-
....actually, I don't know anything about Vim syntax specification. Does the
syntax match
command even work outside the context of a syntax file? Feb 28, 2018 at 23:08 -
@KyleStrand I understood your question. Did you try copying that line into your vimrc? Or just running it in Vim? :)– RichFeb 28, 2018 at 23:12
On my French AZERTY Mac keyboard, I can insert an en dash with Alt+-. Your mileage may obviously vary.
-
2
Alt+-
(or ratherOption+-
to be pedantic) is a Mac-specific shortcut. It works everywhere in OS X, but is not related to vi or vim.– ash108Feb 25, 2015 at 19:52 -
1Works in MacVim, but in Terminal vim it acts like [Esc] followed by
-
, thus switching to Normal mode and putting the cursor at the beginning of the previous line. Feb 25, 2015 at 21:51
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 :he i_CTRL-V_digit
).
For an en dash that'd be for example ^Vu2013
.
-
I realize now that this is actually what the OP was already doing! (Though en dash is 2013, not 2014 -- that's em dash.) I think this still may be good to have somewhere in the pile of answers, however.– trembySep 6, 2017 at 22:45
<Ctrl-Shift-U>2013<Space>
because it works everywhere. A few key codes go a long way (I mainly use U+2013, U+2014, U+2026, U+2212, U+2192, U+B0; –/—/…/−/→/°).Compose
-
-
-
for a em-dash,Compose
-
-
.
for a en-dash. Compose is magical, especially if you map it to the otherwise totally unused Caps-Lock.