# Setting up Sway in Vim

By [andrw](https://paragraph.com/@andrw) · 2023-09-24

---

![](https://storage.googleapis.com/papyrus_images/f299dd469c3228e3a3b3d0aa48185bfd49958e343aa2ac06ba8ed4b0d71b532f.png)

If you are a swayor and you use vim as your main code editor here I will show you how to configure the sway syntax in vim, so you can develop smart contracts like a ninja.

Let's started
-------------

### Pre-requisites:

1.  Ensure you have the `forc-lsp` installed with `forc-lsp —version`. If not, install [the Sway toolchain](https://fuellabs.github.io/sway/v0.25.2/introduction/installation.html).
    
2.  Clone the sway.vim repo
    
        git clone https://github.com/FuelLabs/sway.vim.git
        
    

Setting up Neovim
-----------------

1.  Copy the folders 'ftdetect' and 'syntax' to your .config folder:
    
        cp -R ~/sway.vim/syntax ~/.config/nvim && cp -R ~/sway.vim/ftdetect ~/.config/nvim
        
    
2.  If you do not have the `~/.config/nvim/init.lua` file, install [kickstart.nvim](https://github.com/nvim-lua/kickstart.nvim).
    
3.  Add the following to `~/.config/nvim/init.lua`:
    
        -- Install Sway LSP as a custom	server
        local lspconfig = require 'lspconfig'
        local configs = require 'lspconfig.configs'
        
        -- Check if the config is already defined (useful when reloading this file)
        if not configs.sway_lsp then
           configs.sway_lsp = {
             default_config = {
               cmd = {'forc-lsp'},
               filetypes = {'sway'},
               on_attach = on_attach,
               init_options = { 
                 -- Any initialization options
                 logging = { level = 'trace' }
               },
               root_dir = function(fname)
                 return lspconfig.util.find_git_ancestor(fname)
               end;
               settings = {};
             };
           }
         end
        
        lspconfig.sway_lsp.setup{}
        
    
    Check that the `LSP` is installed running `:LspInfo`
    

![](https://storage.googleapis.com/papyrus_images/74e3974c0081a3ad0eba66d3cef7d5892261f1152263e334e38b32c42c3d8a76.gif)

Setting up Vim
--------------

To configure sway in vim the steps are very similar to the ones we did to configure in neovim.

1.  Clone the sway.vim repo
    
        git clone https://github.com/FuelLabs/sway.vim.git
        
    
2.  Copy the folders 'ftdetect' and 'syntax' to your .config folder:
    
        cp -R ~/sway.vim/syntax ~/.config/nvim && cp -R ~/sway.vim/ftdetect ~/.config/nvim
        
    
3.  Add the following code to your `~/.vim/filetype.vim`
    
        if exists("did_load_filetypes")
          finish
        endif
        
        augroup filetypedetect
          au! BufNewFile,BufRead *.[sS][wW] setf sway
        augroup END
        
    
4.  Install [vim.plug](https://github.com/junegunn/vim-plug) with the following command
    
        curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
            https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
        
    
5.  Add the following code to your `~/.vimrc`
    
        call plug#begin()
        
        Plug 'prabirshrestha/vim-lsp'
        
        call plug#end()
        
    
6.  Create a file called `~/.vim/lsp.vim` with the following code
    
        " vim-lsp for Sway (sway-lsp)
        if executable('sway-lsp')
            au User lsp_setup call lsp#register_server({
                \ 'name': 'sway-lsp',
                \ 'cmd': {server_info->['sway-lsp']},
                \ 'whitelist': ['sway'],
                \ })
        endif
        
    
7.  Open vim and run `:source ~/.vim/lsp.vim`
    
8.  Check that the `LSP` is installed running `:LspStatus`
    

And done!! Now to code with sway in vim or neovim.

* * *

That's all for today, I hope this tutorial has worked for you, thanks for making it this far!

---

*Originally published on [andrw](https://paragraph.com/@andrw/setting-up-sway-in-vim)*
