How can I make it so when in visual mode, pressing b will toggle highlighted text to its bold unicode counterpart?

This is what I mean by its Unicode counterpart:


For example:

Formatting done with markup

SE bold

SE Italic

Formatting done with unicode

𝗨𝗻𝗢𝗰𝗼𝗱𝗲 𝗯𝗼𝗹𝗱

𝘜𝘯π˜ͺ𝘀𝘰π˜₯𝘦 π˜ͺ𝘡𝘒𝘭π˜ͺ𝘀

  • 2
    You may have to use functions for that. For example, :vmap b :call Bold() will call the function when you press b in visual mode. Then, you can create a function for it to get the text and replace each character with its bold counterpart if available. You may also try other possibilities like using HTML tags like <b> or latex like things. Give an example text along with sample fonts and its bold faced font name. Remove all that is irrelevant to the question.
    – SibiCoder
    Sep 20 '16 at 7:11

Create a mapping like this so that you can press a key and it will call the function which converts the text for you.

      :vmap b :call MakeMeBold()

Let's create a function now.

     :function MakeMeBold()
     :normal! gv"by
     :let @b=substitute (@b,"a","A","g")
   . :normal! gv"bp

The above code with the substitute function substitutes all a to A for example. You can copy paste each normal and bold instead of a and A in try code and use the same line multiple times with different values. A better approach will be to use arrays instead of multiple lines of code.

Put the above sentences in ~/.vimrc

  • 1
    Since this is mobile, I couldn't practice the above code. Also, I can't use such Unicode characters from my mobile because of keypads. Once I switch to my PC, I will elaborate my answer with more perfect code.
    – SibiCoder
    Sep 20 '16 at 8:51
  • This is a great suggestion; Once I get my vimrc configured, i'll edit the answer in for others to use. Thanks.
    – Anon
    Sep 20 '16 at 9:38
  • 1
    Was easy and it worked. It would be nice if there was a toggle ability, meaning this: If (some letters are bold) { All letters are made bold. } else if (All letters are bold) { All letters are made regular. }
    – Anon
    Sep 22 '16 at 20:26
  • 1
    That is also possible. Adding a line of code to inspect their Unicode points or the font will help us to detect and replace with the other one. Once I am done with that, I will add it in the answer :
    – SibiCoder
    Sep 23 '16 at 5:52

My TextTransform plugin will handle the setup of a mapping; this will support normal mode command for lines and {motion}, as well as visual mode.

Here's an example that only works with A; you need to expand the tr() accordingly.

function! BoldToggle( text )
return tr(a:text, "A\U0001D400", "\U0001D400A")

call TextTransform#MakeMappings('', '<Leader>b', 'BoldToggle')

You can then transform a line with <Leader>bb, or (any) selection with <Leader>b.

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.