33

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.)

  • 4
    FYI, U+2014 is an EM DASH. U+2013 is an EN DASH. fileformat.info/info/unicode/char/2014/index.htm, fileformat.info/info/unicode/char/2013/index.htm – Quincy Bowers Feb 25 '15 at 14:39
  • 7
    If you're using X11, I would say XCompose ... As this will work on all your applications, and not just Vim. Or if you're on OSX or Windows, the facilities that that OS provide. – Martin Tournoij Feb 25 '15 at 14:40
  • Perhaps not the easiest way, but I tend to use <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; –/—/…/−/→/°). – wchargin Feb 25 '15 at 18:08
  • Out of curiosity, how do you display en dashes? AFAIK, there is no way to display non-fixed-width fonts properly (i.e. with non-uniform character widths and kerning), so em- and en-dashes always look exactly like hyphens that extend to the extreme edge of the character boundary. Do you just not mind having an extreme disconnect between how the character is displayed in Vim and how it's rendered elsewhere? – Kyle Strand Feb 25 '15 at 18:45
  • 2
    I am with @Carpetsmoker: 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. – Rmano Feb 26 '15 at 9:24
42

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.

  • Likewise, an EM DASH can be inserted with <C-k>-M. – Quincy Bowers Feb 25 '15 at 14:40
  • 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. – SummerBreeze Feb 25 '15 at 15:11
  • 3
    To arrive at this answer: hex 2013 is decimal 8211. Use the :digraphs command to display all digraphs, and look for 8211. – 200_success Feb 25 '15 at 18:04
10

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.

  • The en dash digraph is built in. – 200_success Feb 25 '15 at 21:39
  • 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. – jamessan Feb 25 '15 at 21:49
8

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> -- &#8202;—&#8202;

syntax match entity_hairspace "&#8202;" 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 &ndash; 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? – Kyle Strand Feb 28 '18 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.) – Rich Feb 28 '18 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? – Kyle Strand Feb 28 '18 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? – Kyle Strand Feb 28 '18 at 23:08
  • @KyleStrand I understood your question. Did you try copying that line into your vimrc? Or just running it in Vim? :) – Rich Feb 28 '18 at 23:12
3

On my French AZERTY Mac keyboard, I can insert an en dash with Alt+-. Your mileage may obviously vary.

  • 2
    Alt+- (or rather Option+- to be pedantic) is a Mac-specific shortcut. It works everywhere in OS X, but is not related to vi or vim. – ash108 Feb 25 '15 at 19:52
  • 1
    Works 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. – 200_success Feb 25 '15 at 21:51
3

Install my unicode plugin, enter Vim and type DASH followed by CtrlXCtrlZ to activate unicode completion.

enter image description here

(Note the -N in the description which shows the digraph to enter that character)

0

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!

0

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. – tremby Sep 6 '17 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.