3

This happens to me all the time: I copy something from a rich text screen (usually a browser) and then paste it into vim. Usually its a code block and then when I go to compile or run or what have you I get all kind of bazaar errors.

I scratch my head, and then spend half an hour trying to figure out what is wrong before I realize I've copied some non ASCII characters: dashes, left and right quotes, long underscores, multiplication signs in place of x's, etc.

So I ask you: how can I copy non-ASCII into my VIM session without error?

Is there a paste mode that automatically 'down samples' unicode to ASCII? Is there a quick/dirty search for non ASCII characters in a file?


Update: For those looking for a similar solution, I didn't find a common tool. But I did find this SO question related to the same solution.

If you're a Vim user, you're in luck: I've written a generic function and posted it to github as a plugin called vim-utf2ascii.

Community
  • 1
  • 1
dsummersl
  • 6,588
  • 50
  • 65
  • I've forked an alternate implementation that doesn't require `set encoding=utf-8`. Mine searches for hex values, rather than unicode values. Otherwise it is exactly the same. https://github.com/ryanvbissell/vim-asciiplz – Cognitive Hazard Aug 28 '21 at 04:03

4 Answers4

2

Just setup Vim to work with UTF-8:

set termencoding=utf-8
set encoding=utf-8

Also make sure you are copying real code: some famous blog systems like to convert straight quotes into curly quotes and do all kind of typographic horrors to code. To avoid that kind of pitfall, try to copy from the source when possible or look for a "raw" button somewhere.

romainl
  • 186,200
  • 21
  • 280
  • 313
2

Update: For those looking for a similar solution, I didn't find a common tool. But I did find this SO question related to the same solution.

If you're a Vim user, you're in luck: I've written a generic function and posted it to github as a plugin called vim-utf2ascii.

Community
  • 1
  • 1
dsummersl
  • 6,588
  • 50
  • 65
  • Your script only seems to work if I put `set termencoding=utf-8` and `set encoding=utf-8` in my .vimrc file. If I was willing to do that, then I personally wouldn't need your script. That doesn't mean your script is not useful to others, but I would recommend at least updating your README to mention this. – Cognitive Hazard Aug 28 '21 at 02:01
1

There is script called demoronizer that can (amongst other things) replace all these smart quotes and hyphens with ASCII equivalents. So you could filter your text through it.

jira
  • 3,890
  • 3
  • 22
  • 32
0

As @Romaonl said first set termencoding and set encoding to utf-8.

After you copied text in your browser, switch to vim and enter "+p

Rsh
  • 7,214
  • 5
  • 36
  • 45