Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

This plugin for gvim adds the ability to change font size (and therefore window size too) using the same methods used in most web browsers, terminal emulators and some other graphical text editors.

In console Vim or Neovim the plugin exits and doesn’t do anything.

I may add Mac support if this plugin attracts any interest.

## Usage
Expand All @@ -15,28 +17,44 @@ I may add Mac support if this plugin attracts any interest.
|<kbd>Ctrl</kbd> + <kbd>=</kbd>| Increases text size |
|<kbd>Ctrl</kbd> + <kbd>-</kbd>| Decreases text size |
|<kbd>Ctrl</kbd> + <kbd>0</kbd>| Resets text to original size |
|<kbd>Ctrl</kbd> + <kbd>mousewheel</kbd>| Resize text |
|<kbd>Ctrl</kbd> + <kbd>mousewheel</kbd> | Increases/Decreases text size |

If you already have any of these keys mapped, your mappings will not be overridden.
There are also a few limitations when using older Windows gvim versions.
First, before v9.0 patch 1112 <kbd>Ctrl</kbd> + <kbd>-</kbd> will not work.
Instead, <kbd>Ctrl</kbd> + <kbd>=</kbd>, <kbd>=</kbd> has been made the default.
Second, the mousewheel mappings will not work if you’re using a version before v8.2 patch 5069.

If you don’t like these mappings, you can map your own in your .vimrc with:

```vim
nnoremap {LHS} <Plug>CfsPlus
nnoremap {LHS} <Plug>CfsMinus
nnoremap {LHS} <Plug>CfsOriginal
```

In terminal vim or neovim the plugin exits and doesn't do anything.
...where `{LHS}` is the key combination you want.

## Install

### Plugin manager

Installation can be done via a plugin manager.

For [vim-plug](https://github.com/junegunn/vim-plug) it's:
For [vim-plug](https://github.com/junegunn/vim-plug) its:

```vim
Plug 'eggbean/resize-font.gvim'
```

### No plugin manager

If you don't use a plugin manager you can just place [the script](https://github.com/eggbean/resize-font.gvim/blob/master/plugin/change_font_size.vim) in...
If you dont use a plugin manager you can just place [the scripts](https://github.com/eggbean/resize-font.gvim/blob/master/plugin/) `change_font_*.vim` in

`~/.vim/plugin` on Linux, or...
`~/.vim/plugin` on Linux, or

`%USERPROFILE%\vimfiles\plugin` on Windows.

## Not working with your font?

This should work with all modern Unicode fonts, but if you find one which doesn't work tell me and I'll modify the regex. This is only likely to occur with Windows.
This should work with all modern Unicode fonts but if you find one that doesnt work tell me and Ill modify the regex. This is only likely to occur with Windows.
52 changes: 44 additions & 8 deletions plugin/change_font_size.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
" Author: Jason Gomez <jason@jinkosystems.co.uk>
" Credit: Originally based on https://vi.stackexchange.com/a/3104/37532

if !has('gui_running') || exists('g:loaded_change_font_size')
" Finish when any of the following apply:
" * Version 9 or 8.2 with patch 4807? Use the vim9script version.
" * No GUI running or Neovim? So, N/A.
" * Plugin already loaded? Don't it load again!
if v:version >= 900 || (v:version == 802 && has('patch4807')) || !has('gui_running') || has('nvim') || exists('g:loaded_change_font_size')
finish
endif
let g:loaded_change_font_size= 1
let g:loaded_change_font_size = "vimscript" " For testing which version

if has('gui_gtk2') || has('gui_gtk3')
if has('gui_gtk')
" gui_gtk includes gui_gtk2 and gui_gtk3 - refer :h has-patch
autocmd GUIEnter * let g:gf_size_orig = matchstr(&guifont, '\( \)\@<=\d\+$')
function! FontSizePlus()
let l:gf_size_whole = matchstr(&guifont, '\( \)\@<=\d\+$')
Expand Down Expand Up @@ -64,8 +69,39 @@ elseif has('gui_win32')
endfunction
endif

nmap <C-=> :call FontSizePlus()<CR>
nmap  :call FontSizeMinus()<CR>
nmap <C-0> :call FontSizeOriginal()<CR>
map <silent> <C-ScrollWheelUp> :call FontSizePlus()<CR>
map <silent> <C-ScrollWheelDown> :call FontSizeMinus()<CR>
" Mappings. Only when one of the supported GUIs is running:
" - Create <Plug> mappings, and
" - Apply mappings (but only when not already specified - 'Do no harm').
" NB: 1. In Windows, the <C-Scroll*> mappings will not work with versions
" before 8.2 with patch 5069 (they do nothing). Refer:
" https://github.com/vim/vim/commit/ebb01bdb273216607f60faddf791a1b378cccfa8
" 2. In Windows, <C-{char}> mappings will not work before 8.2 with
" patch 4807, which has been set as the cut-off for using the
" vim9script version. Therefore, different keys are needed in
" Windows - that's not been done here *yet* (testing of <S-F10> to
" <S-F12> in Windows was _somewhat_ functional, though seemed to
" stop/start working for some reason).
" So, the <C-Scroll*> and <C-{char}> mappings have been left as per Jason's
" initial setup, notwithstanding none will work in Windows and whether they
" work in *gtk* for 8.2 before patch 4807 ... ???
if has('gui_gtk') || has('gui_win32')

map <silent> <Plug>CfsPlus <Cmd>execute "call FontSizePlus()"<CR>
" <C-=> and/or <C-ScrollWheelUp> to FontSizePlus() when not already mapped
execute maparg('<C-=>', 'n') == '' ? ':nnoremap <C-=> <Plug>CfsPlus' : ''
execute maparg('<C-ScrollWheelUp>', '') == '' ? ':noremap <C-ScrollWheelUp> <Plug>CfsPlus' : ''

map <silent> <Plug>CfsMinus <Cmd>execute "call FontSizeMinus()"<CR>
" U+001F (, i.e., <C-_>) and/or <C-ScrollWheelDown> to FontSizeMinus() when not already mapped
" Specifically, before v9.0 patch 1112 <C-_> will not work on Windows
" Refer: https://github.com/vim/vim/commit/7b0afc1d7698a79423c7b066a5d8d20dbb8a295a
" That's academic because before 8.2 patch 4807 no <C- mappings work
" in Windows anyhow; see the "NB:" item 2, above).
execute maparg('', 'n') == '' ? ':nnoremap  <Plug>CfsMinus' : ''
execute maparg('<C-ScrollWheelDown>', '') == '' ? ':noremap <C-ScrollWheelDown> <Plug>CfsMinus' : ''

map <silent> <Plug>CfsOriginal <Cmd>execute "call FontSizeOriginal()"<CR>
" <C-0> to FontSizeOriginal() only when not already mapped
execute maparg('<C-0>', '') == '' ? ':nnoremap <C-0> <Plug>CfsOriginal' : ''

endif
119 changes: 119 additions & 0 deletions plugin/change_font_size_vim9script.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"
" Δ FFFF OOO NNN TTT SSS III ZZZZ EEEE
" Δ Δ F O O N N T S I ZZ E
" Δ Δ FFFF O O N N T SSS I ZZ EEE
" Δ Δ F O O N N T S I ZZ E
" ΔΔΔΔΔΔΔΔΔ F OOO N N T SSS III ZZZZ EEEE
"
" File: change_font_size_vim9script.vim
" Description: Change gvim font size using keyboard or mousewheel
" Author: Peter Kenny
" Credit: Based on Jason Gomez's github.com/eggbean/resize-font.gvim,
" (itself citing vi.stackexchange.com/a/3104/37532)
"
" vim9-mix for running vim9script.
" Finish when any of the following apply:
" * Version < 8.2 (inclusive of any version of Neovim, which returns 801)
" * Version 8.2 without patch 4807 (Win32 64-bit gvim at/above that works)
" * Console version, so no GUI is running, or
" * The plugin has already been loaded.
if (v:version < 802 || (v:version == 802 && !has('patch4807')) || !has('gui_running') || exists('g:loaded_change_font_size'))
finish
endif

vim9script

g:loaded_change_font_size = "vim9script"

# initialisation of variables
var gf_size_whole: number
var new_font_size: string
var gf_weight: string
var gf_bold: string
var gf_cansi: string
var gf_qdraft: string

# gui_gtk2 and gui_gtk3 - refer :h has-patch (gui_gtk does addresses both)
if has('gui_gtk')
autocmd GUIEnter * g:gf_size_orig = matchstr(&guifont, '\( \)\@<=\d\+$')
def g:FontSizePlus(): void
gf_size_whole = str2nr(matchstr(&guifont, '\( \)\@<=\d\+$'))
if gf_size_whole < 28
gf_size_whole += 1
new_font_size = ' ' .. gf_size_whole
&guifont = substitute(&guifont, ' \d\+$', new_font_size, '')
endif
enddef
def g:FontSizeMinus(): void
gf_size_whole = str2nr(matchstr(&guifont, '\( \)\@<=\d\+$'))
if gf_size_whole > 1
gf_size_whole -= 1
new_font_size = ' ' .. gf_size_whole
&guifont = substitute(&guifont, ' \d\+$', new_font_size, '')
endif
enddef
def g:FontSizeOriginal(): void
&guifont = substitute(&guifont, ' \d\+$', ' ' .. g:gf_size_orig, '')
enddef
elseif has('gui_win32')
autocmd GUIEnter * g:gf_size_orig = matchstr(&guifont, '\(:h\)\@<=\d\+\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$')
def g:FontSizePlus(): void
gf_size_whole = str2nr(matchstr(&guifont, '\(:h\)\@<=\d\+\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$'))
gf_weight = matchstr(&guifont, '\(:W\d\+\)\?$')
gf_bold = matchstr(&guifont, '\(:b\)\?$')
gf_cansi = matchstr(&guifont, '\(:cANSI\)\?$')
gf_qdraft = matchstr(&guifont, '\(:qDRAFT\)\?$')
if gf_size_whole < 28
gf_size_whole += 1
new_font_size = ':h' .. gf_size_whole
&guifont = substitute(&guifont, '\(:h\d\+\)\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$', new_font_size .. gf_weight .. gf_bold .. gf_cansi .. gf_qdraft, '')
endif
enddef
def g:FontSizeMinus(): void
gf_size_whole = str2nr(matchstr(&guifont, '\(:h\)\@<=\d\+\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$'))
gf_weight = matchstr(&guifont, '\(:W\d\+\)\?$')
gf_bold = matchstr(&guifont, '\(:b\)\?$')
gf_cansi = matchstr(&guifont, '\(:cANSI\)\?$')
gf_qdraft = matchstr(&guifont, '\(:qDRAFT\)\?$')
if gf_size_whole > 1
gf_size_whole -= 1
new_font_size = ':h' .. gf_size_whole
&guifont = substitute(&guifont, '\(:h\d\+\)\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$', new_font_size .. gf_weight .. gf_bold .. gf_cansi .. gf_qdraft, '')
endif
enddef
def g:FontSizeOriginal(): void
gf_weight = matchstr(&guifont, '\(:W\d\+\)\?$')
gf_bold = matchstr(&guifont, '\(:b\)\?$')
gf_cansi = matchstr(&guifont, '\(:cANSI\)\?$')
gf_qdraft = matchstr(&guifont, '\(:qDRAFT\)\?$')
&guifont = substitute(&guifont, '\(:h\d\+\)\(:W\d\+\)\?\(:b\)\?\(:cANSI\)\?\(:qDRAFT\)\?$', ':h' .. g:gf_size_orig .. gf_weight .. gf_bold .. gf_cansi .. gf_qdraft, '')
enddef
endif

# Mappings. Only when one of the supported GUIs is running:
# - Create <Plug> mappings, and
# - Apply them only when they're not already specified ('Do no harm'!).
if has('gui_gtk') || has('gui_win32') # has('gui_running') is too permissive

map <silent> <Plug>CfsPlus <Cmd>execute "call g:FontSizePlus()"<CR>
# <C-=> and/or <C-ScrollWheelUp> to FontSizePlus() when not already mapped
execute maparg('<C-=>', 'n') == '' ? ':nnoremap <C-=> <Plug>CfsPlus' : ''
execute maparg('<C-ScrollWheelUp>', '') == '' ? ':noremap <C-ScrollWheelUp> <Plug>CfsPlus' : ''

map <silent> <Plug>CfsMinus <Cmd>execute "call g:FontSizeMinus()"<CR>
# U+001F ( i.e., <C-_>) and/or <C-ScrollWheelDown> to FontSizeMinus() when not already mapped
# NB: Before v9.0 patch 1112 <C-_> will not work on Windows - refer:
# https://github.com/vim/vim/commit/7b0afc1d7698a79423c7b066a5d8d20dbb8a295a
if !has('win32') || (has('win32') && v:versionlong >= 9001112)
execute maparg('', 'n') == '' ? ':nnoremap  <Plug>CfsMinus' : ''
else
# <C-_> is unavailable, as noted above, so use <C-=>= instead
execute maparg('<C-=>=', 'n') == '' ? ':nnoremap <C-=>= <Plug>CfsMinus' : ''
endif
execute maparg('<C-ScrollWheelDown>', '') == '' ? ':noremap <C-ScrollWheelDown> <Plug>CfsMinus' : ''

map <silent> <Plug>CfsOriginal <Cmd>execute "call g:FontSizeOriginal()"<CR>
# <C-0> to FontSizeOriginal() when not already mapped
execute maparg('<C-0>', '') == '' ? ':nnoremap <C-0> <Plug>CfsOriginal' : ''

endif
Binary file added resizing_gvim_WSL.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.