13

Does Vim (with or without a plugin - I don't care) support strikethrough text at all? I've found myself keeping a running list of "TO-DO's" in it, and would like to be able to "cross off" my done items, via strikethrough text.

Thanks!

Connor
  • 4,138
  • 8
  • 34
  • 51
  • why not just change the highlighting for that line to differentiate it? – abcd Dec 13 '11 at 00:40
  • 1
    @abcd `why not just change the highlighting for that line to differentiate` Maybe because it isn't SO beautiful? :) it's just a point of the question, and author explicitly gave us to understand he wants to be able `to "cross off" my done items, via strikethrough text.` That's why he was looking for `support strikethrough text` in Vim. It is the wish and will of the question's author. Why disregard him? Anyway, if you really want to supply another way to "cross off" text in Vim, maybe it's better to do it with details (in Answers)? – kenichi Jan 26 '17 at 12:52

8 Answers8

21

If you're working with Unicode text, you may be able to achieve this using combining characters. The following article describes how this can be accomplished in gvim:

http://vim.wikia.com/wiki/Create_underlines,_overlines,_and_strikethroughs_using_combining_characters

You will need to make sure that the font gvim is using supports the appropriate characters, on Windows both Consolas and Courier New appeared to handle this correctly, but most of the others did not.

DRH
  • 7,868
  • 35
  • 42
  • 1
    I can't get this to work under Vim73 on XP with either Consolas or Courier New. enc=utf-8, so is fileenc. Still displays "garbage". – Rook Dec 13 '11 at 01:37
  • +1, works fine in my TODO file. Also worked in TODOs in my code, thanks :). – 0xc0de Jan 13 '14 at 09:35
  • The combining characters don't work with Vim74 with Consolas in ConEmu under Windows 7 for me. :( Still, good answer, +1. – Keith Pinson Jul 03 '14 at 14:45
  • This worked great for me on OS X. Another tip for those using iTerm2 -- the struck through characters in my default font (Inconsolata) didn't look good at all, so I enabled the "Use a different font for non-ASCII characters" option. Courier looks significantly better. – Cardano Jan 12 '19 at 17:29
13

Going for a highlighting simpler solution, I would use a Vim custom syntax highlighting rule so that, for example, text marked like this:

~~ text ~~

is displayed in a different color (eg. a darker text color if you have a dark background, or as dark reversed colors). Which would be, in vimrc:

au BufRead,BufNewFile *.txt   syntax match StrikeoutMatch /\~\~.*\~\~/   
hi def  StrikeoutColor   ctermbg=darkblue ctermfg=black    guibg=darkblue guifg=blue
hi link StrikeoutMatch StrikeoutColor

(where the au command is used to apply the rule to filetype .txt files only)

  • Personally I found this to be the best/easiest implementation. I don't need actual strikethrough, I just need some indication that I can map in my brain to strikethrough, and this was the most simple approach. I added to this with some remap commands to suit my own tastes and now it's perfect. – Raj Dec 21 '16 at 02:34
  • I also like this approach. @Raj, would you share your mappings? – pschulz Jun 21 '20 at 08:09
  • @pschulz in addition to the syntax highlighting above, I added `nnoremap st I~~ A ~~:noh` `nnoremap ust :s/^\~\~ //:s/ \~\~$//:noh` Which allow me to easily add or remove the strikeout highlighting per line. I found I didn't need selection but you could easily add a mapping for that as well. – Raj Jun 25 '20 at 01:28
  • Sorry I couldn't preview the comment and I was trying to get the formatting right and then my comment edit timed out, but those `nnoremap` commands are on two separate lines. – Raj Jun 25 '20 at 01:34
7

You can create a (single) strikethrough character by appending the unicode "long strike overlay combining character" (0336) to the character. For example, to create a strikethrough "Z", enter (in input mode):

Z^Vu0336

(where ^V is CTRL-V).

You can use :s (substitute) to strikethrough a bunch of characters, for example, to strikethrough the current line:

:s/./&^Vu0336/g

Wikipedia links: strikethrough and combining chaaracter.

vsh
  • 71
  • 1
  • 1
  • 1
    and just to avoid confusion for others `:s/./&^Vu0336/g` is how you type it, so it will look `:s/./&̶/g` – Alex Feb 24 '20 at 12:30
  • I like this! Thank you. But how does one remove the strikeout? – dado Jun 04 '21 at 02:04
7

You can put this in your .vimrc

map _ a<C-V>u0336<Esc><Space>

and then the underscore character will "strikout-ize" whatever is under the cursor, analogous to how ~ (tilde) changes the case.

It works like this:

a - starts to append after the character under the cursor
<C-V>u0336 (stands for Control-V followed by u0336) - the strikout overlay combining character
<Esc> - exists append mode
<Space> - advances the cursor past the strikeout character

Vim assigns another meaning to the Underscore (_) character (see :help _) so you might want to choose another character (or sequence of multiple characters).

jimav
  • 671
  • 6
  • 16
  • In order to strike out an entire section of visually selected text, try: `s/\%V\(.\)/\=submatch(1) . "\u0336"/g`, where `\%V` makes the substitution operate on a selection and `\=` makes it possible to have unicode characters in the replacement string. – sampi Apr 19 '22 at 08:46
3

it works perfectly well with a unicode vim in a terminal.

Just put it in my vim vundle file:

https://github.com/crux/crux-vimrc/blob/master/plugin/unicode.vim

-nargs=0 Overline        call s:CombineSelection(<line1>, <line2>, '0305') command! -range -nargs=0 Underline       call
s:CombineSelection(<line1>, <line2>, '0332') command! -range -nargs=0
DoubleUnderline call s:CombineSelection(<line1>, <line2>, '0333')
command! -range -nargs=0 Strikethrough   call
s:CombineSelection(<line1>, <line2>, '0336')

function! s:CombineSelection(line1, line2, cp)   execute 'let char =
"\u'.a:cp.'"'   execute
a:line1.','.a:line2.'s/\%V[^[:cntrl:]]/&'.char.'/ge' endfunction

vnoremap  :Strikethrough<CR> vnoremap __ :Underline<CR> ```
General Grievance
  • 4,555
  • 31
  • 31
  • 45
1

There is a patch pending, to make this work in the gui. Unfortunately, this is currently burried in the todo list, so it will take I while, until it will be applied by Bram.

Christian Brabandt
  • 8,038
  • 1
  • 28
  • 32
-4

no, vim doesn't support this. it's a text editor, not a WYSIWYG editor.

Raymond
  • 744
  • 5
  • 12
-7

If you are using Vim under a terminal, no you can't.

  1. highlight arguments for normal terminals

                                  *bold* *underline* *undercurl*
                                  *inverse* *italic* *standout*
    

    term={attr-list} attr-list *highlight-term* E418 attr-list is a comma separated list (without spaces) of the following items (in any order): bold underline undercurl not always available reverse inverse same as reverse italic standout NONE no attributes used (used to reset it)

    Note that "bold" can be used here and by using a bold font. They have the same effect. "undercurl" is a curly underline. When "undercurl" is not possible then "underline" is used. In general "undercurl" is only available in the GUI. The color is set with |highlight-guisp|. ~

However, under GUI, you can do so. Under 'guifont', we have the following:

  For the Win32 GUI                                       *E244* *E245*
  - takes these options in the font name:
          hXX - height is XX (points, can be floating-point)
          wXX - width is XX (points, can be floating-point)
          b   - bold
          i   - italic
          u   - underline
          s   - strikeout
          cXX - character set XX.  Valid charsets are: ANSI, ARABIC,
                BALTIC, CHINESEBIG5, DEFAULT, EASTEUROPE, GB2312, GREEK,
                HANGEUL, HEBREW, JOHAB, MAC, OEM, RUSSIAN, SHIFTJIS,
                SYMBOL, THAI, TURKISH, VIETNAMESE ANSI and BALTIC.
                Normally you would use "cDEFAULT".

    Use a ':' to separate the options.
  - A '_' can be used in the place of a space, so you don't need to use
    backslashes to escape the spaces.
  - Examples: >
      :set guifont=courier_new:h12:w5:b:cRUSSIAN
      :set guifont=Andale_Mono:h7.5:w4.5
Seyeong Jeong
  • 10,658
  • 2
  • 28
  • 39
  • And since guifont being global, how would you make only a portion of the text, stroken-out? – Rook Dec 13 '11 at 00:46
  • Yes, you are right. There isn't a way to make a portion of the text strickeout. The OP needs to use different background/font color sets or go with highlight-term option. – Seyeong Jeong Dec 13 '11 at 00:52
  • 9
    -1 [Obviously](http://stackoverflow.com/a/8483044/430766) your statement is wrong. – bitmask Dec 13 '11 at 01:16
  • 6
    @bitmask - And your statement is even worse - it is useless. It adds no information why the above is wrong, no useful information as to the solution of the problem, and serves only as SPAM, which we are trying to minimize here. So please refrain from leaving comments of type "this is bad". – Rook Dec 13 '11 at 01:50
  • 5
    @ldigas: I included a link to the answer that falsifies the statement. I extremely rarely downvote and I consider it polite to always leave a note, why I did so. I believe downvoting without saying why is considered rude by most people. – bitmask Dec 13 '11 at 01:57
  • @Idigas: The answer is simple, use special :higlight statements, because they support to set the font (not in all flavors of Vim) though. See also :h highlight-font – Christian Brabandt Jan 13 '14 at 11:12
  • 2
    If this answer is wrong, why is it (still) accepted? Can't moderators un-accept it? – Nikos Alexandris Nov 03 '15 at 22:45