Let vim with external themes fill terminal window in Mac
Tags: vim, bash, beginners, computerscience
Published: 2023-03-11 08:15:24


## 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](https://i.imgur.com/JDqcqO2.png)



After opening vim :
![After opening vim](https://i.imgur.com/zPvYw5U.png)



***
## 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](https://superuser.com/questions/1188772/mac-command-to-change-the-background-color-in-a-terminal), 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](https://help.apple.com/assets/61E87FFE1DFCFB222874A4D9/61E87FFE1DFCFB222874A4E0/zh_TW/73871c3e2be37a62a93b1f9ab437c23b.png )
   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.
   ![](https://i.imgur.com/80CAFZ8.png)

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
```
## 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}"
```

## 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](https://i.imgur.com/jxBVI1D.png)



***
## 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
    ```
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