Terminal Vim has no control over, or knowledge of, the line spacing that is currently being used in the terminal within which it is running. It just sends characters to the terminal (in your case, iTerm2), which renders them according to its own settings. There isn't, therefore, a clean way to do this.
What you can do, however, is tell iTerm2 to switch profiles on the fly by using its proprietary escape codes.
So, to switch line-spacing when you exit and enter Vim:
- Create a copy of your iTerm2 profile: (With Preferences... -> Profiles -> Other Actions... -> Duplicate Profile
- Set the name of your new profile: Under the General tab set the Name to LineSpaced
- Set up the required line spacing: Under the Text tab, click on Change Font and drag the slider for Character Spacing -> Vertical to your preferred value.
Add the following code to your .vimrc
:
if exists('$ITERM_PROFILE')
silent !printf '\e]50;SetProfile=LineSpaced\x7'
autocmd VimLeave * silent !printf '\e]50;SetProfile=Default\x7'
endif
This will switch to the iTerm2 profile named "LineSpaced" when you run Vim, and sets up an autocommand
to switch back to the profile named "Default" when you exit Vim.
This has the side-effect of changing the height of the iTerm2 window (because you are changing the line-spacing without changing the number of lines). If you don't want this to happen, you can use some AppleScript to tell iTerm2 to change the number of rows dynamically, in order to keep the height of the window roughly consistent:
if exists('$ITERM_PROFILE')
silent !printf '\e]50;SetProfile=LineSpaced\x7'
silent !osascript -e "tell application \"iTerm\" to tell the current terminal to set number of rows to 36"
autocmd VimLeave * silent !printf '\e]50;SetProfile=Default\x7'
autocmd VimLeave * silent !osascript -e "tell application \"iTerm\" to tell the current terminal to set number of rows to 48"
endif
N.B. You might need to tweak the number of rows specified to work well with the line-spacings you have set for your two profiles.