Updating Vim (MacOS)
Tags: beginners, vim
Published: 2019-07-07 14:37:06

## What is Vim? 
It's a powerful open-source text editor that allows you to edit your text files on the command line. You technically don't need to leave the Terminal to code. 

## Setup
I spent like 30 mins trying to figure out how to install Vim when I first heard about it, so I'm saving you the wasted effort: Vim comes pre-installed in MacOS. No need to install if your current version is the latest stable version. The most current version at this time of writing is Vim <strong>8.1</strong>. Check out the latest stable Vim version at [vim.org/download.php](https://www.vim.org/download.php). 

<img src="https://drive.google.com/uc?export=view&id=1VBHhfqg-ipk-k_GwYuFrzbv0dF7QRDO3" alt="latest vim version" />

## Updating Vim
My current Vim is at version **8.0.1283**. Sadly, it's not the latest stable version so I'm going to update it to <strong>^ 8.1.x</strong>. FYI my laptop is a MacOS Mojave 10.14.4. If you are not a Mac user, click [here](https://www.vim.org/download.php) for device-specific instructions (Unix or MS-Windows). 

**Get Current Version of Local Vim**
To figure out what your local version of Vim, enter the command `vim -v`. 

It should return something like below:
 
<img src="https://drive.google.com/uc?export=view&id=1AU8WreMRGQR_HrNxBCYkGWSNAVYevR6W" alt="local vim version" />

### MacVim for OS
I decided to update using [MacVim](https://github.com/macvim-dev). There are other [options](https://www.vim.org/download.php) though! If you're like me, you're wondering what the difference is between MacVim and Vim. Google says there's no difference 😁

<img src="https://drive.google.com/uc?export=view&id=1fALK6gy5FeI7sR2t04kls_WMwm2wYFgD" alt="macvim vs vim" />

[More about MacVim on FAQ](https://github.com/macvim-dev/macvim/wiki/FAQ).

### Installing MacVim
- Go to [https://github.com/macvim-dev/macvim/releases/latest](https://github.com/macvim-dev/macvim/releases/latest) for the latest version of MacVim and download **MacVim.dmg**
- Open the **.dmg** file, and copy MacVim.app to **/Applications**

<img src="https://drive.google.com/uc?export=view&id=1e55IYBZ65kp29xyYUflKVZ3vb4qUM6MB" alt="copy macvim to applications" />

- At this point, you can run the MacVim GUI by double-clicking the icon.
- You'll enter **vim command mode** once you double-click the icon. 
- To exit vim command mode, type in `:q` and press Enter. 

### Here's a list of common Vim commands below: 
    - type `:q<ENTER>` to **EXIT** out of MacVim mode
    - type `:help mvim` for help
    - type `:wq<ENTER>` to save and exit
    - type `:q!` to exit without saving changes
    - type `i` to enter insert mode
    - press **ESC** to exit insert mode

Vim has two modes **insert mode** and **command mode**. For a starter list visit [coderwall - basic vim commands](https://coderwall.com/p/adv71w/basic-vim-commands-for-getting-started).

### Add mvim as a terminal command
You don't really want to rely on double-clicking the GUI every time you want to use MacVim. So, the next step is to add **mvim*** script to your path. In this way, your machine will know where to find the MacVim executable whenever you run the command `mvim`. 

The easiest way to do this is to run the command: 

```
sudo ln -s /Applications/MacVim.app/Contents/bin/mvim /usr/local/bin/mvim
``` 

The command above will link the mvim script to **/usr/local/bin/mvim**.

<img src='https://drive.google.com/uc?export=view&id=1-kD3Fpl-OsYgrwPpwuT4RrJVXMblRgsc' alt="mvim script in /usr/local/bin" />

- Once you do that, you can use `mvim` to run a MacVim shell. 

- To check if everything was linked correctly, enter `/usr/local/bin/mvim` on the command line. It should open up MacVim. 

- To check that you have the correct version, run: `mvim -v`. You should see the latest version you just downloaded (old 8.0.x => new 8.1.x).

<img src="https://drive.google.com/uc?export=view&id=1aOzwZS5EqB_cTFl10oL4_QCpE3Zdm66o" alt="newer macvim version" />

### Optional - Make mvim your default vim
Want to use MacVim instead of the older version of Vim on the command line? Follow the steps below:

- If you already have a **.bash_profile** open it using the command `<bash_profile_path>; open .bash_profile;`. 
- For example, my **.bash_profile** is located at my **Users/<user_name>** directory so I run the command `cd Users/<user_name>; open .bash_profile;`. 
- This will open the .bash_profile text file. Add the alias below: 

```
# ----------------------
# MacVim alias
# ----------------------
# use mvim as a default text editor, assuming you already have mvim installed
alias vim="mvim -v" 

```

**Note:** If you don't know your **`<user_name>`**, run `whoami` on the command line. 

- Viola! You're all set. Check the `vim -v `and see an updated version of Vim. 
- **Note:** You need to open a new terminal shell or restart your terminal to see any of changes you've made applied to the shell. 


## Troubleshooting
Having trouble? Google it! StackOverflow will give you a clue how to debug. Or you can use some of the resources below. 

- [Vim Homepage](https://www.vim.org)
- [MacVim FAQ](https://github.com/macvim-dev/macvim/wiki/FAQ)

I hope this helps someone! This is the first part of the series "Building a Docker Project Using Vim". Stay tuned for more!