DEV Community

Cover image for Let vim with external themes fill terminal window in Mac
deeeelin
deeeelin

Posted on

Let vim with external themes fill terminal window in Mac

Introduction

Some mac users with external colorschemes installed in vim may occur this problem : "vim editor didn't fill the whole screen of terminal" (which creates the blue gap in the sample image after opening vim) , although it doesn't bother in the functional aspects, but it still looks a little annoying or imperfect. This article helps you solve this problem !

Before opening vim :
Before opening vim

After opening vim :
After opening vim


Solution

Foreword

At first I tried to find a solution to let vim to fill the terminal, but it seems there are no direct way to make this happen ,so there is a alternative solution, "let terminal background color be same as vim editor when vim is open". I get inspried when looking at here, so I wrote a bash script to make this happen, below is the code,make sure your computer can execute applescript and bash !

Let's start!

  1. Make a terminal config file with the same background color as vim
    1. You can use the builtin app "Digital Color Meter" in mac to get the rgb value of the vim background color. image alt
    2. Then choose Terminal > Preferences , click Profiles, create a profile, then click Text.You can see the "Color and Effects section" under a subject "Background", change the color using the obtained rgb value.

2.Copy the below code, remember to change <1> in the quote to step 1.2's terminal profile name, <2> as your vim editor path.

3.Save the below code as a bash file

#!/bin/bash


original=$(osascript -e 'tell application "Terminal" to return name of current settings of window 1')

osascript -e 'tell application "Terminal"

   set current settings of window 1 to settings set "<1>"

end tell'

<2> $@

osascript \
-e 'tell application "Terminal"' \
-e "set current settings of window 1 to settings set \"$original\"" \
-e 'end tell'

unset original

exit 0
Enter fullscreen mode Exit fullscreen mode

Additional step

  1. For convenience, you can rename your bash file as "vim" using "mv" command in terminal (need to use "mv" command to forcely remove file extension in filename)

  2. Then put the file under a empty directory

  3. After placing your directory in a nice place,you can add the below line to ".bash_profile" or ".zprofile" (depends on which default shell you use ) , remember to change <1> to the path of the directory your bash file is.

  PATH="<1>:${PATH}"
Enter fullscreen mode Exit fullscreen mode

Now execute the bash script and see if it works !

ps. If you gone through step 5, reopen your terminal, and you can just type "vim" to use vim editor with the problem solved now !

problem solved


Additional information

  • Arguments :
    • you can add arguments behind the name of bash file when entering command, these arguements will be thrown to vim in the exact same form.

Example :

```
 <bash file path> test.txt
```
Enter fullscreen mode Exit fullscreen mode

the above command is equivalent to "edit text.txt by vim"

Reference

  1. https://superuser.com/questions/1188772/mac-command-to-change-the-background-color-in-a-terminal

Top comments (0)