DEV Community

Sophia Brandt
Sophia Brandt

Posted on • Originally published at rockyourcode.com on

5 1

Setup Nim With Neovim

Use Neovim as your Nim IDE

Why Nim?

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. 1

Nim resembles Python, but the language is significantly faster and statically compiled. Nim comes with meta-programming abilities (like a LISP). You can compile a Nim program into a stand-alone C binary that runs on every system.

Read more about Nim on the official homepage.

Why NeoVim?

NeoVim is a superb text-editor based on Vim.

With NeoVim you can seamlessly edit text, jump to the terminal and run commands, etc.

You don't need the mouse, and thus your workflow can be much faster than with an editor like VS Code.

The following instructions both work with Vim 8 and NeoVim.

Install Nim

Install Nim. I use choosenim to manage my installations.

curl https://nim-lang.org/choosenim/init.sh -sSf | sh
choosenim stable
Enter fullscreen mode Exit fullscreen mode

Language Server

Install a language server protocol implementation for Vim. LanguageClient-neovim is the only one I could get working with several languages (Elixir, Reason, JavaScript, Nim).

For example, with vim-plug:

Plug 'autozimu/LanguageClient-neovim', {
    \ 'branch': 'next',
    \ 'do': 'bash install.sh',
    \ }
Enter fullscreen mode Exit fullscreen mode

Install nimlsp. Run this command in your terminal:

nimble install nimlsp
Enter fullscreen mode Exit fullscreen mode

Configure the plugins within your vim configuration (~/.vimrc or similar):

set hidden

nnoremap <F5> :call LanguageClient_contextMenu()<CR>

let g:LanguageClient_serverCommands = {
\   'nim': ['~/.nimble/bin/nimlsp'],
\ }

Enter fullscreen mode Exit fullscreen mode

The setup specifies the location of nimlsp. On your computer, it might be different.

Now you can open a Nim file, and hit F5. The LanguageClient menu will pop up.

You can fine-tune the LanguageClient configuration to your liking.

Use :h LanguageClient within Vim to get more information.

nimlsp can be a bit peculiar about its setup. The language server needs some of Nim's files in order to work properly..

You might want to check out the GitHub repository for further trouble-shooting.

(Tab) Completion

You can use Vim's inbuilt completion, but the easier way is to install a completion plugin.

VimCompletesMe is a minimal plugin that does everything I need.

Plug 'ajh17/VimCompletesMe'
Enter fullscreen mode Exit fullscreen mode

Linting and Formatting Code

ALE (Asynchronous Lint Engine) is a plugin providing linting (syntax checking and semantic errors) in NeoVim 0.2.0+ and Vim 8 while you edit your text files.

Install it with your package manager (or find alternative instructions on GitHub):

Plug 'dense-analysis/ale'
Enter fullscreen mode Exit fullscreen mode

Example setup in your init.vim (or ~/.vimrc, etc.):

let g:ale_sign_error                  = '✘'
let g:ale_sign_warning                = '⚠'
highlight ALEErrorSign ctermbg        =NONE ctermfg=red
highlight ALEWarningSign ctermbg      =NONE ctermfg=yellow
let g:ale_linters_explicit            = 1
let g:ale_lint_on_text_changed        = 'never'
let g:ale_lint_on_enter               = 0
let g:ale_lint_on_save                = 1
let g:ale_fix_on_save                 = 1

let g:ale_linters = {
\   'nim':      ['nimlsp', 'nimcheck'],
\}

let g:ale_fixers = {
\   'nim':      ['nimpretty'],
\   '*':        ['remove_trailing_lines', 'trim_whitespace'],
\}
Enter fullscreen mode Exit fullscreen mode

Improve ALE's performance by setting the linters you need and don't lint every time you type in something.

Fix a file when you save or call :ALEFix manually.

See :h ale for more information.

Links


  1. https://nim-lang.org/ 

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (2)

Collapse
 
juancarlospaco profile image
Juan Carlos

Awesome post!. NeoVim uses the same ~/.vimrc config file as Vim?.

Collapse
 
sophiabrandt profile image
Sophia Brandt

Thanks.

Neovim's config file is "init.vim", but the principles are the same.
jdhao.github.io/2018/12/24/centos_...

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️