Vim and Swift: Development in a post-apocalyptic world

4 minute read

Vim and Swift

Disclaimer: I don’t advocate using Vim seriously as your primary development environment. As powerful as it is, Vim remains a text editor, not an IDE.

I ran into some Xcode problems the other day where I lost syntax highlighting and code completion in Swift files. What should we do when Xcode just doesn’t work?

I joked that at this point, it might be easier to just use Vim for my Swift development.


Well…


Vim and Swift? Challenge Accepted.


It’s not hard to use an arbitrary text editor to write some code and then run it through a compiler. For this setup, we want something a little bit more pleasant to use. The passionate programmers that we are, we want to be able to work on non-trivial Swift projects, with or without Xcode, from the comfort of our own home or in a post-apocalyptic nuclear bunker (hey, let’s not rule anything out).

We want:

  • Those sweet Vim keybindings
  • An easy way to compile and run code
  • Resilience against Xcode temper tantrums
  • Syntax highlighting that works. All. The. Time.

We’ll be using some Vim plugins. You’ll need some means of plugin management. I’ll be using Pathogen.

Syntastic

Syntastic provides syntax highlighting in Vim for many languages. It does this by associating checkers with a particular language. When you save the file you’re working on, it runs the appropriate checkers. A checker is essentially an adapter between the world of Vim with Syntastic and your language with its compiler or linter. What we want to accomplish through Syntastic is to easily compile a project when the current file is saved and surface any errors or warnings inside Vim.

Using Pathogen, install Syntastic.

cd ~/.vim/bundle && \
git clone --depth=1 https://github.com/vim-syntastic/syntastic.git

swift.vim

swift.vim has a SwiftPM checker that will compile a project and a SwiftLint checker to remind us that we occasionally have no idea what we’re doing.

Install swift.vim:

cd ~/.vim/bundle && \
git clone --depth=1 https://github.com/keith/swift.vim

SwiftPM

Go to a Swift project containing a Package.swift (The manifest file for SwiftPM) and open a Swift file in Vim. Run :SyntasticInfo and you should see some output from Syntastic:

Vim Syntastic :SyntasticInfo

The important tidbit here is that the swiftpm checker is available and enabled. This means that when we save the file, it’ll run swift build to build the package.

Vim Syntastic Swift Package Manager checker

SwiftLint

To get the SwiftLint checker working, we need to start off with: brew install swiftlint.

If you want to configure SwiftLint to your tastes, you’ll need to add a .swiftlint.yml file to your Swift project to get the syntax checker working. Or, if you’re happy with the global default, add this to your .vimrc:

let g:syntastic_swift_swiftlint_use_defaults = 1 

Adding that will let you have some awesome linting without needing to add a configuration file to every project.

Vim Swift SwiftLint checker

If you tried this in a project that has the SwiftPM checker enabled, you might run into a slight problem: :SyntasticInfo notes both the SwiftPM checker and the SwiftLint checker as available, but neither are enabled. Add the following to your .vimrc to enable both:

let g:syntastic_swift_checkers = ['swiftlint', 'swiftpm'] 

Individual Swift Files

SwiftPM is great, but sometimes it’s a little bit much when I want to create a short script. I created a Syntastic checker that will work a single Swift file to run the script when you save it and show any errors inside Vim.

Install syntastic swift:

cd ~/.vim/bundle && \
git clone --depth=1 https://github.com/TheCodedSelf/syntastic-swift.git

If you’re only working on a single Swift file, you can use this checker - no need to go through the hassle of setting up SwiftPM. Just remember to add the checker in your .vimrc if necessary:

let g:syntastic_swift_checkers = ['swiftlint', 'swiftpm'] 

Unfortunately, while the checker will surface any errors in Vim, it doesn’t show any output from running the Swift script. I added a mapping to my .vimrc to sort this out:

nnoremap <leader>sb :!swift %<CR>

By hitting my leader key followed by sb, the current script will run and you’ll be able to see any output.

Moving Forward

You can compile SwiftPM projects, run Swift scripts, and use SwiftLint to patch up your mistakes, all while remaining in the warm embrace of Vim.

Impressive

The next time you’re having troubles with Xcode, or if you just want to practice your Vim-fu, why not take this a bit further?

Have fun, and remember: learning is living.

Updated:

Comments