3

For example, lets say I have a line like:

Hello, this should be bold.

Then is there any way to change one or more words to be bold, so it will appear on the screen as:

Hello, this should be bold.

For example, in some text editors I can select a word and press a "bold" button, and the text remains bold for the lifetime of the file.

I'm using gVim 8.0 on Ubuntu 18.04.

3
  • 2
    Have a look at the Txtfmt plugin Aug 14 '19 at 15:06
  • Are you dealing with text files like txt, markdown or with source code? For source code, it is non-sense to bold some code.
    – jdhao
    Aug 15 '19 at 2:33
  • It is a text file Aug 15 '19 at 5:44
2

Well, it is not that simple.

What you can come up with -- use syntax definitions. But this bold text you will create would be temporary, it will not be saved with your file like word processing software does.

So to the code.

func! RegionMakeBold()
  hi MyBold gui=bold
  let l_start = getpos("'<")[1]
  let l_end = getpos("'>")[1]
  let c_start = getpos("'<")[2]
  let c_end = getpos("'>")[2]+1
  execute 'syntax region MyBold start=/\%'.l_start.'l\%'.c_start.'c/ end=/\%'.l_end.'l\%'.c_end.'c/'
endfunc

vnoremap <leader>b :<C-U>call RegionMakeBold()<CR>

Having this in your .vimrc will allow you to make bold text out of visually selected blocks. Just select text, press <leader>b and text should be bolded.

enter image description here

It will probably not work for the files with defined filetypes/syntax.

5
  • Problem with matching on absolute coordinates is that when you start modifying the file, the contents will shift... But given the question is quite open ended, perhaps this is a good answer?
    – filbranden
    Aug 14 '19 at 11:32
  • 1
    Actually, @filbranden this is one of the cases where there is no reason (or way) to answer. The question is unclear and unanswerable until it’s been clarified, so here (unlike the mapping Q) Id rather not see answers. But thats just me
    – D. Ben Knoble
    Aug 14 '19 at 12:15
  • @D.BenKnoble Totally agree with your point! Looking forward to the next moderator elections on Vi and Vim.SE! 😁
    – filbranden
    Aug 14 '19 at 12:33
  • 1
    @filbranden got to get out of beta first 😉
    – D. Ben Knoble
    Aug 14 '19 at 12:36
  • 3
    The new (and experimental) text properties feature should be a better fit for this (and fix @filbranden's remark), but you'll need at least 8.1.1341 (May this year). Aug 14 '19 at 17:22
2

Plain text files do not have formatting options like a word processor does. If you want that WYSIWYG functionality like a traditional word processor, then I recommend a word processor like LibreOffice.

However, there are document formats that support common formatting with additional syntax. Markdown is a lightweight syntax that is still readable as a textfile but vim syntax highlighting can somewhat show you what the final result will look like, e.g. with pandoc's markdown https://github.com/vim-pandoc/vim-pandoc-syntax

enter image description here From pandoc's README.

Another option is using LaTeX which has lots of precise control for formatting. For small documents, compiling is very fast so you can quickly get a preview of your document.

2
  • You dont need pandoc to have markdown syntax highlighting.
    – D. Ben Knoble
    Aug 19 '19 at 18:04
  • The plugin integrating vim with the pandoc document converter has such a function. ":help vim-pandoc" explains that <localleader>b toggles bold [vn]. The help page contains a list of other mappings. Sep 15 '20 at 13:21

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .