|
|
| Line 5: |
Line 5: |
| |
|previous=759 |
|
|previous=759 |
| |
|next=761 |
|
|next=761 |
| − |
|created=July 14, 2004 |
+ |
|created=2004 |
| |
|complexity=intermediate |
|
|complexity=intermediate |
| |
|author=Wouter Bolsterlee |
|
|author=Wouter Bolsterlee |
| Line 15: |
Line 15: |
| |
If you regularly switch to a larger or smaller font, for example because someone looking at your code thinks the letters are too small, or because you want to lay back in your chair while reading, this tip is for you. |
|
If you regularly switch to a larger or smaller font, for example because someone looking at your code thinks the letters are too small, or because you want to lay back in your chair while reading, this tip is for you. |
| |
|
|
|
| − |
The following script defines two commands, <tt>:LargerFont</tt> and <tt>:SmallerFont,</tt> to allow quick adjustments to the font size used in the gtk2 gui. Change minfontsize and maxfontsize to suit your needs. See below for alternative solutions. |
+ |
The following script defines two commands, <code>:LargerFont</code> and <code>:SmallerFont,</code> to allow quick adjustments to the font size used in the gtk2 gui. Change minfontsize and maxfontsize to suit your needs. See below for alternative solutions. |
| |
|
|
|
| |
To use this script, put the following code into ~/.vim/plugin/gtk2fontsize.vim or in your vimrc. |
|
To use this script, put the following code into ~/.vim/plugin/gtk2fontsize.vim or in your vimrc. |
| Line 54: |
Line 54: |
| |
|
|
|
| |
==Mapping solution== |
|
==Mapping solution== |
| − |
Another alternative is to use the following mappings which use a clever substitute (no functions are required). This example works using gvim under Windows, where the command <tt>:set guifont?</tt> would show the font name and <tt>:h12</tt> if the font size is 12 points, for example. |
+ |
Another alternative is to use the following mappings which use a clever substitute (no functions are required). This example works using gvim under Windows, where the command <code>:set guifont?</code> would show the font name and <code>:h12</code> if the font size is 12 points, for example. |
| |
|
|
|
| |
<pre> |
|
<pre> |
| Line 70: |
Line 70: |
| |
|
|
|
| |
=====Explanation===== |
|
=====Explanation===== |
| − |
For example, the command <tt>:echo &guifont</tt> might show <tt>Bitstream_Vera_Sans_Mono:h12:b:cANSI</tt>. |
+ |
For example, the command <code>:echo &guifont</code> might show <code>Bitstream_Vera_Sans_Mono:h12:b:cANSI</code>. |
| |
|
|
|
| − |
The substitute pattern <tt>:h\zs\d\+</tt> looks for <tt>:h</tt> followed by a number (one or more digits). The <tt>\zs</tt> marks the start of what is found (so it skips the <tt>:h</tt>). |
+ |
The substitute pattern <code>:h\zs\d\+</code> looks for <code>:h</code> followed by a number (one or more digits). The <code>\zs</code> marks the start of what is found (so it skips the <code>:h</code>). |
| |
|
|
|
| − |
The replacement string <tt>\=eval(submatch(0)+1)</tt> uses: |
+ |
The replacement string <code>\=eval(submatch(0)+1)</code> uses: |
| − |
;<tt>\=</tt> |
+ |
;<code>\=</code> |
| |
:Following is an expression to be evaluated. |
|
:Following is an expression to be evaluated. |
| − |
;<tt>submatch(0)</tt> |
+ |
;<code>submatch(0)</code> |
| − |
:The text that matched (this example finds 12 with <tt>\d\+</tt>). |
+ |
:The text that matched (this example finds 12 with <code>\d\+</code>). |
| − |
<tt>eval()</tt> |
+ |
<code>eval()</code> |
| |
:Evaluate the following string (12+1). |
|
:Evaluate the following string (12+1). |
| |
|
|
|
Duplicate tip
This tip is very similar to the following:
These tips need to be merged – see the merge guidelines.
If you regularly switch to a larger or smaller font, for example because someone looking at your code thinks the letters are too small, or because you want to lay back in your chair while reading, this tip is for you.
The following script defines two commands, :LargerFont and :SmallerFont, to allow quick adjustments to the font size used in the gtk2 gui. Change minfontsize and maxfontsize to suit your needs. See below for alternative solutions.
To use this script, put the following code into ~/.vim/plugin/gtk2fontsize.vim or in your vimrc.
let s:pattern = '^\(.* \)\([1-9][0-9]*\)$'
let s:minfontsize = 6
let s:maxfontsize = 16
function! AdjustFontSize(amount)
if has("gui_gtk2") && has("gui_running")
let fontname = substitute(&guifont, s:pattern, '\1', '')
let cursize = substitute(&guifont, s:pattern, '\2', '')
let newsize = cursize + a:amount
if (newsize >= s:minfontsize) && (newsize <= s:maxfontsize)
let newfont = fontname . newsize
let &guifont = newfont
endif
else
echoerr "You need to run the GTK2 version of Vim to use this function."
endif
endfunction
function! LargerFont()
call AdjustFontSize(1)
endfunction
command! LargerFont call LargerFont()
function! SmallerFont()
call AdjustFontSize(-1)
endfunction
command! SmallerFont call SmallerFont()
Plugin solution
As an alternative, you could use one of these plugins which work with fonts on Unix and Windows systems:
- script#202 quickfonts.vim : quickly switch between a list of favorite fonts, manage list of favorite fonts
- script#593 guifont++.vim : Vim plugin for quickly changing GUI font size
Mapping solution
Another alternative is to use the following mappings which use a clever substitute (no functions are required). This example works using gvim under Windows, where the command :set guifont? would show the font name and :h12 if the font size is 12 points, for example.
nnoremap <C-Up> :silent! let &guifont = substitute(
\ &guifont,
\ ':h\zs\d\+',
\ '\=eval(submatch(0)+1)',
\ '')<CR>
nnoremap <C-Down> :silent! let &guifont = substitute(
\ &guifont,
\ ':h\zs\d\+',
\ '\=eval(submatch(0)-1)',
\ '')<CR>
Explanation
For example, the command :echo &guifont might show Bitstream_Vera_Sans_Mono:h12:b:cANSI.
The substitute pattern :h\zs\d\+ looks for :h followed by a number (one or more digits). The \zs marks the start of what is found (so it skips the :h).
The replacement string \=eval(submatch(0)+1) uses:
\=
- Following is an expression to be evaluated.
submatch(0)
- The text that matched (this example finds 12 with
\d\+).
eval()
- Evaluate the following string (12+1).
References