Advertisement
FocusedWolf

VIM: Bind F11 and F12 to increase/decrease font size

Apr 1st, 2022 (edited)
2,851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 5.76 KB | None | 0 0
  1. function! AdjustFontSize(amount)
  2.     if !has("gui_running")
  3.         return
  4.     endif
  5.  
  6.     let l:min_font_size = 5
  7.     let l:max_font_size = 64
  8.  
  9.     let l:font_info = GetFontInfo()
  10.     if l:font_info.name == '' || l:font_info.size == ''
  11.         return
  12.     endif
  13.  
  14.     let l:font_name = l:font_info.name
  15.     let l:font_size = l:font_info.size
  16.  
  17.     " Decrease font size.
  18.     if a:amount == '-'
  19.         let l:font_size = l:font_size - 1
  20.  
  21.     " Increase font size.
  22.     elseif a:amount == '+'
  23.         let l:font_size = l:font_size + 1
  24.  
  25.     " Use a specific font size.
  26.     elseif str2nr(a:amount)
  27.         let l:font_size = str2nr(a:amount)
  28.     endif
  29.  
  30.     " Clamp font size.
  31.     let l:font_size = max([l:min_font_size, min([l:max_font_size, l:font_size])])
  32.  
  33.     if matchstr(&guifont, ':') == '' " Linux guifont style.
  34.         " \v           Very magical.
  35.         " (\d+$)       Capture group:       Match [0-9] one-or-more times, at the end of the string.
  36.         let l:font_size_pattern = '\v(\d+$)'
  37.     else " Windows and macOS guifont style.
  38.         " \v           Very magical.
  39.         " (:h)@<=      Positive lookbehind: Match ':h'.
  40.         " (\d+)        Capture group:       Match [0-9] one-or-more times.
  41.         let l:font_size_pattern = '\v(:h)@<=(\d+)'
  42.     endif
  43.  
  44.     " Update vim font size.
  45.     let &guifont = substitute(&guifont, l:font_size_pattern, l:font_size, '')
  46.  
  47.     call DisplayFontInfo()
  48. endfunction
  49.  
  50. function! DisplayFontSelector()
  51.     if !has("gui_running")
  52.         return
  53.     endif
  54.  
  55.     " Display font selector.
  56.     " NOTE: This only changes &guifont to '*' in terminal vim.
  57.     set guifont=*
  58.  
  59.     " Display font info after font selector closes.
  60.     call DisplayFontInfo()
  61. endfunction
  62.  
  63. function! DisplayFontInfo()
  64.     let l:font_info = GetFontInfo()
  65.     if l:font_info.name == '' || l:font_info.size == ''
  66.         return
  67.     endif
  68.  
  69.     " Display font name and size.
  70.     redraw | echomsg l:font_info.name . ' ' . l:font_info.size . '%'
  71. endfunction
  72.  
  73. function! GetFontInfo()
  74.     " Windows and macOS &guifont: Hack NF:h11:cANSI
  75.     "                             3270Medium_NF:h10:W500:cANSI:qDRAFT
  76.     " Linux &guifont: Hack Nerd Font Mono Regular 10
  77.  
  78.     if matchstr(&guifont, ':') == '' " Linux guifont style.
  79.         " \v           Very magical.
  80.         " (^.{-1,})    Capture group:       Anchored at the start of the string, match any character one-or-more times non-greedy.
  81.         " ( \d+$)@=    Positive lookahead:  Match ' ' followed by [0-9] one-or-more times, at the end of the string.
  82.         let l:font_name_pattern = '\v(^.{-1,})( \d+$)@='
  83.  
  84.         " \v           Very magical.
  85.         " (\d+$)       Capture group:       Match [0-9] one-or-more times, at the end of the string.
  86.         let l:font_size_pattern = '\v(\d+$)'
  87.     else " Windows and macOS guifont style.
  88.         " \v           Very magical.
  89.         " (^.{-1,})    Capture group:       Anchored at the start of the string, match any character one-or-more times non-greedy.
  90.         " (:)@=        Positive lookahead:  Match ':'.
  91.         let l:font_name_pattern = '\v(^.{-1,})(:)@='
  92.  
  93.         " \v           Very magical.
  94.         " (:h)@<=      Positive lookbehind: Match ':h'.
  95.         " (\d+)        Capture group:       Match [0-9] one-or-more times.
  96.         let l:font_size_pattern = '\v(:h)@<=(\d+)'
  97.     endif
  98.  
  99.     let l:font_name = matchstr(&guifont, l:font_name_pattern)
  100.     let l:font_size = matchstr(&guifont, l:font_size_pattern)
  101.  
  102.     return { 'name' : l:font_name, 'size' : l:font_size }
  103. endfunction
  104.  
  105. " Bind Control + Mouse-wheel to zoom text.
  106. " NOTE: Starting in version 9.0 this works in Windows. Previously it only worked in Linux and macOS. SEE: :h scroll-mouse-wheel
  107. map <silent> <C-ScrollWheelDown> :call AdjustFontSize('-')<CR>
  108. map <silent> <C-ScrollWheelUp> :call AdjustFontSize('+')<CR>
  109.  
  110. " Decrease font size.
  111. nnoremap <silent> <F11> :call AdjustFontSize('-')<CR>
  112. inoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
  113. vnoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
  114. cnoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
  115. onoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
  116.  
  117. " Increase font size.
  118. nnoremap <silent> <F12> :call AdjustFontSize('+')<CR>
  119. inoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
  120. vnoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
  121. cnoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
  122. onoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
  123.  
  124. " Set font size to my preferred size (10).
  125. nnoremap <silent> <S-F11> :call AdjustFontSize(10)<CR>
  126. inoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
  127. vnoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
  128. cnoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
  129. onoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
  130.  
  131. " TODO: Sadly cannot bind Ctrl + 0 yet:
  132. " It would allow me to get rid of F11 F12 binding completely,
  133. " so just C-ScrollWheel and C-0 would be needed, and maybe Shift-0 to open font selector.
  134. " https://vi.stackexchange.com/questions/19358/cannot-map-ctrl-number-except-6-or
  135. " https://vimhelp.org/vim_faq.txt.html#faq-20.5
  136. "
  137. " Set font size to my preferred size (10).
  138. " nnoremap <silent> <C-0> :call AdjustFontSize(10)<CR>
  139. " inoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
  140. " vnoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
  141. " cnoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
  142. " onoremap <silent> <C-0> <Esc>:call AdjustFontSize(10)<CR>
  143.  
  144. " Display font selector.
  145. nnoremap <silent> <S-F12> :call DisplayFontSelector()<CR>
  146. inoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
  147. vnoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
  148. cnoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
  149. onoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement