I am using Vim 7.4.488 in iTerm2. Currently I've configured the 'linespace' setting in iTerm2's preferences. However, I would like to set a different linespace for Vim. I know that :set linespace=1 should to the trick. However this does not work, as seen here.

I would like to know if there is some kind of trick or setting that allows me to set different linespaces for iTerm and Vim, instead of using iTerm's 'global' preferences.


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:

  1. Create a copy of your iTerm2 profile: (With Preferences... -> Profiles -> Other Actions... -> Duplicate Profile
  2. Set the name of your new profile: Under the General tab set the Name to LineSpaced
  3. 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.
  4. 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'

    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"

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.