|
CharacterInput
How to input characters that are not on your keyboard.
Featured Right now it does not say much about encoding setup: it should be expanded later to provide more detailed guidance. IntroductionThe problem: How do I get a specific character into my file if that character does not appear on my keyboard? Sometimes, it may be just an occasional special character you do not normally use (say, in a "foreign" name). It is more painful if you need a character more often (for instance, the proper quote characters in you language). In the worst case, it could even be one of the characters used for wiki markup. There is no simple answer: the problem depends on many (wildly variable) things: the character, the keyboard, the operating system setup and its localization... There are usually several ways in which you can setup your Vim to allow you to insert "unusual" characters conveniently, but only in the case that your operating system supports it. (All modern systems support Unicode, at least in principle, but if you work in a terminal window, you may have problems.) The problem involves roughly three different areas: encoding issues, the actual character input, and font support. EncodingsA Unicode encoding is ideal, UTF-8 specifically is preferable. Choose something else only after a careful consideration. If you have your encoding set as UTF-8 ( set encoding=utf-8 will do that,; if in your .vimrc, this encoding will be the default), you should be able to paste any character from any other window and it will get into your file (it has to be a character (sequence), not a picture or some other object). You may not see the glyph, though: this depends on which font is your Vim using. But in principle, the character is there. Read more on the topic of encodings using :he enc and :he fencs, or look at http://vim.wikia.com/wiki/Working_with_Unicode. Method 0: Copy/paste in GUI. For a comprehensive list of glyphs, you can consult WikiBook on Unicode Characters, or just look at a small sample of characters first. In other (non-unicode) encodings, you can only successfully do the same with the characters that the encoding supports (often, this is just a hundred characters in single-byte encodings). Character InputAssuming you prefer typing rather than pasting from some other window, vim has several ways that you could employ to get a character that your keyboard does not provide.
Ideal for cases you need to insert something very often. Good for both frequent and occasional uses, has a pre-defined support for several hundred characters already. More often used to insert longer sequences via a custom shorthand. For the brave ones looking for a more complete remapping of the keyboard. This is only useful if you know the number of the character you need (in the encoding you use). Works only if someone with similar specific needs prepared some methods and made them available. Use the Vim help command to get more information: for instance :he digraphs to read on digraphs or :he 24.8 to read on direct codepoint entry. You can put the commands that will help with your specific needs into your .vimrc file (if you do not mind that the mapping will work on other types than just .wiki). If you do not feel like doing your own customization, explore the "digraph" method, that is the only one that may cover your needs out-of-the-box (or the more general codepoint numeric entry, which is however much harder to remember). Unicode has over 100 000 codepoints for which there is no built-in setup in Vim or Vimwiki, which is quite understandable. Depending on your needs, you may need to do some customization once in a while... MappingsExamples:
imap <F5> ¡ For more help, see :he mapping. Things to avoid: remapping keys that you may need for their original function, and attempting to remap keys that are intercepted by the operating system (Vim cannot overrule that). DigraphsExamples:
Frequent users may set it up so that instead of having to type CTRL-K char1 char2, the sequence char1 <BS> char2 will do the job (<BS> represents the Backspace key) --- see :he digraph. For many digraphs, the actual pair of characters used is to some extent predictable. Here is the significance of the second character (from :he digraph-default):
accents: grave, acute, circumflex, tilde, macron, breve accents: dot, diaresis (umlaut), cedilla, underline, stroke accents: double acute (Hungarian umlaut), ogonek, caron (háček), ring, hook, horn various Latin/Greek/Cyrillic letters Cyrillic and Greek Arabic and Hebrew Bopomofo (Chinese phonetic alphabet) Hiragana and Katakana (Japanese syllabaries) AbbreviationsSimple example: :ia VW Vimwiki sets it up so that in insert mode, typing VW expands to Vimwiki (after you type the space). For more complex abbreviations (where the replacement string is not fixed, but calculated by a function), see :he map-expression. KeymapAs said above, this is an option to consider if you need an extensive set of mappings. It maybe worth exploring if the operating system does not provide something you would be happy with.
Using the operating system's key-map facilities, it is going to be the opposite. Direct Codepoint EntryYou need to know the character's codepoint (= the number of the character in the used encoding) --- in decimal, octal or hexadecimal representation. In insert mode, you press CTRL-V and then input the number. (On Windows, it is often the case that CTRL-Q has to be used, because CTRL-V is used to paste text for consistency with other applications.) Examples (assuming a unicode encoding is used):
See :he 24.8 (in recent versions of Vim, it is 24th chapter of the user documentation, section on Entering special characters) or :he i_CTRL-V_digit. Look at the List of Unicode Blocks if you want to see how codepoints are organized. In Vim's normal mode, to see the codepoint for the character under cursor, press ga and the status line will show the decimal, octal and hexadecimal values. Vim Scripts for InputJust as with the keymap method: many people would first see if their operating system could provide some support (especially when the issue is just a specific language support). For a heavy Unicode user, this script will allow you to search the official Unicode character description (a text database):
The next script, on the other hand, covers the entry of only a handful of basic, but frequently used characters (proper quotes, ellipsis, dashes):
The following scripts will help you with inputting Chinese (note that you will still need to make sure that you have a suitable font to show you the result of your efforts): These scripts help with inputting mathematical symbols in Unicode (greek letters, arrows, operator symbols etc.):
FontsThis is a complicated issue in general: Vim can use only mono-spaced fonts (which usually comprise just a tiny fraction of available fonts on a modern system), and pretty much only one font family at a time (unlike GUI programs, that may try to combine many fonts in order to display as many symbols as possible). If you need some very unusual characters, it may not be easy to find a Vim-usable font that has them. A list of free monospaced fonts is at http://www.thefreecountry.com/programming/programmers-fonts.shtml The following have a very wide coverage, including reasonably good technical and mathematical symbol coverage (not supporting East Asian characters, though):
You can use the script http://vim.wikia.com/wiki/Generate_all_Unicode_characters to help you see what glyphs are in your font within a given range. If you have any suggestions, please leave a comment below. Information on suitable font sets is particularly needed: this is the one topic where Vim help is not telling you all you need. |
off: awesome!