How to run mvim (MacVim) from Terminal?

Learn how to run mvim (macvim) from terminal? with practical examples, diagrams, and best practices. Covers macos, shell, vim development techniques with visual explanations.

Running MacVim (mvim) from Your macOS Terminal

Hero image for How to run mvim (MacVim) from Terminal?

Learn how to seamlessly integrate MacVim with your command-line workflow, enabling you to open files and directories directly from the Terminal.

MacVim, often invoked as mvim, is a powerful graphical user interface (GUI) version of Vim for macOS. While it provides a rich set of features and a native macOS experience, many developers prefer to work within the Terminal. This article will guide you through the process of setting up mvim so you can launch it directly from your command line, enhancing your development workflow.

Understanding the mvim Command

When you install MacVim, it typically places the application in your /Applications folder. However, simply typing mvim in your Terminal won't work out of the box because the mvim executable isn't automatically added to your system's PATH. The MacVim application bundle contains a command-line tool, also named mvim, which acts as a wrapper to launch the GUI application from the Terminal.

flowchart TD
    A[User types 'mvim file.txt' in Terminal]
    B{Is 'mvim' in PATH?}
    A --> B
    B -- No --> C[Command not found error]
    B -- Yes --> D[Terminal executes 'mvim' wrapper script]
    D --> E[Wrapper script launches MacVim GUI application]
    E --> F[MacVim opens 'file.txt']

Flowchart of mvim command execution

Setting Up the mvim Command-Line Tool

To make the mvim command available in your Terminal, you need to create a symbolic link (symlink) to the mvim executable located within the MacVim application bundle. This symlink should be placed in a directory that is already part of your system's PATH environment variable, such as /usr/local/bin.

1. Locate the mvim executable

The mvim executable is typically found inside the MacVim application bundle. The full path is usually /Applications/MacVim.app/Contents/MacOS/mvim.

Open your Terminal and execute the following command to create a symlink. You might need administrator privileges, so use sudo.

3. Verify the installation

After creating the symlink, close and reopen your Terminal, or source your shell configuration file (e.g., source ~/.zshrc or source ~/.bash_profile). Then, try running mvim to see if it launches MacVim.

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

Creating a symbolic link for the mvim command

Using mvim from the Terminal

Once the setup is complete, you can use mvim just like you would use vim in the Terminal, but it will launch the GUI version. Here are some common use cases:

# Open a file in the current directory
mvim myfile.txt

# Open a file in a specific path
mvim ~/Documents/project/main.py

# Open a directory (MacVim will open a file browser or a new empty buffer)
mvim .

# Open multiple files
mvim file1.txt file2.js

# Open a file and go to a specific line number (e.g., line 10)
mvim +10 anotherfile.md

# Open a file in the background (Terminal remains usable)
mvim -f & mybackgroundfile.log

Common mvim command-line usages