How to configure vim for rust


#1

Hello all,

First of all i’d like to say hello to the Rust community . I’m very new to Rust : i’ve just finished the 5th chapter of the book .

Now I want to configure a development environment based on VIM and I’m a bit confused because when searching for ‘vim rust’ apparently there are too many plugins for vim . And … I’m not sure how to find out which of them are mature and which are just prototypes .

The ‘are we ide yet’ website mentions several vim plugins (rust.vim, YouCompleteMe, vim-racer ,…) but it’s unclear whether they are alternatives that do the same thing, or if they need to be installed together.

So my question is : what vim plugin(s) do you recommend me to install in order to have

  • colored syntax for rust
  • code completion (if i understand correctly there is a tool called racer that can be used by editors)
  • nice to have : a left pane for files browsing

Thanks in advance for any suggestion!
Adrian


#2

I recommend https://github.com/rust-lang/rust.vim plus https://github.com/autozimu/LanguageClient-neovim plus https://github.com/rust-lang-nursery/rls. No idea what to use for a pane, though.


#3

Yep, pretty much this! I use NERDTree for a left pane / file browser that I can toggle when I need to.


#4

I went through my gross and gnarly vimrc and trimmed it down to something that’s possibly helpful. It’s a combination of my personal stuff and https://dougblack.io/words/a-good-vimrc.html . You’ll need to be using vim8 and install https://github.com/junegunn/vim-plug .

" plug
call plug#begin()
Plug 'rust-lang/rust.vim'
Plug 'scrooloose/nerdtree'
Plug 'ervandew/supertab'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'w0rp/ale'
call plug#end()

" history
set history=700

" Vim recommended
if has('autocmd')
  filetype plugin indent on
endif
if has('syntax') && !exists('g:syntax_on')
  syntax enable
endif
set synmaxcol=9999
set hidden " Allows you to switch buffers without saving current
set wildmenu "tab completion
set wildmode=longest:full,full " First tab brings up options, second tab cycles
set encoding=utf8

" Movement
let mapleader = ","
set tm=2000
noremap ,, ,

" treat wrapped lines as different lines
nnoremap j gj
nnoremap k gk

" Enable mouse support
set mouse=a

" Set 7 lines to the cursor - when moving vertically using j/k
set so=7

" Always show current position
set ruler

" Remove bell
set visualbell
set t_vb=

" Better searching
set incsearch
set ignorecase
set smartcase
set wrapscan "wraps around end of file
" Redraw screen and clear highlighting
nnoremap <Leader>r :nohl<CR><C-L>

" Don't redraw while executing macros (good performance config)
set lazyredraw

" tabs
set expandtab
set smarttab

" nowrap
set nowrap

" Show matching bracket
set showmatch
set matchtime=2
set shiftwidth=2
set tabstop=2

" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=<,>,h,l

" Return to last edit position when opening files (You want this!)
augroup last_edit
  autocmd!
  autocmd BufReadPost *
       \ if line("'\"") > 0 && line("'\"") <= line("$") |
       \   exe "normal! g`\"" |
       \ endif
augroup END

" Close nerdtree after a file is selected
let NERDTreeQuitOnOpen = 1

function! IsNERDTreeOpen()
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction

function! ToggleFindNerd()
  if IsNERDTreeOpen()
    exec ':NERDTreeToggle'
  else
    exec ':NERDTreeFind'
  endif
endfunction

" CtrlP using ripgrep
if executable('rg')
  set grepprg=rg\ --color=never
  let g:ctrlp_user_command = 'rg %s --files --color=never --glob ""'
  let g:ctrlp_use_caching = 0
endif

" If nerd tree is closed, find current file, if open, close it
nmap <silent> <leader>f <ESC>:call ToggleFindNerd()<CR>
nmap <silent> <leader>F <ESC>:NERDTreeToggle<CR>

set statusline=%f\ %h%w%m%r\ %=%(%l,%c%V\ %=\ %P%)

" Ale syntax checking
let g:ale_rust_cargo_use_check = 1
" use Ctrl-k and Ctrl-j to jump up and down between errors
nmap <silent> <C-k> <Plug>(ale_previous_wrap)
nmap <silent> <C-j> <Plug>(ale_next_wrap)

Open up a file, and run PlugInstall() to have vimplug download the plugins. This vimrc gets you a couple of goodies

  1. rust-vim: Provides rust language support/syntax highlighting.
  2. Ale: Error checking based off of cargo check. Use ctrl-j and ctrl-k to jump between errors
  3. NerdTree: A file explorer. Use f to open up the side pane (I have leader set to comma)
  4. Ctrl-P: Fuzzy file finding. Hit ctrl-p and type a file name, and it will show you files in your directory with that name. You can then open that file. If you have ripgrep installed, ctrl-p will use it to do the search.
  5. SuperTab: Basic tab completion (just based off of words already in your file).

#5

I’ve built something that I use for Rust, check it out here https://github.com/spastorino/vinimum/tree/vinimum2 is the vinimum2 branch. master doesn’t have any Rust stuff but have some other goodies. I’m going to keep adding general purpose plugins as the thing has in master but for now I’m just trying to use a bare thing that works for Rust.

It basically has rust.vim, ale, snippets/ultisnips and YouCompleteMe and some cool configs. I still need to make ctags autogenerate when saving a file and things like that.


#6

Thanks for the suggestions! Now I have a good starting point for experimenting a bit with different plugins and settings .


#7

Note that Vim 8.0 comes with rust.vim so if all you want is syntax highlighting you don’t need to do anything.


#8

Hey, great job making the vimrc, I agree with basically all of what is on there. Just wanted to add that the function ToggleFindNerd() is completely unnecessary. You can achieve the same functionality with
nmap <silent> <leader>f :NERDTreeToggle %<CR>


#9

Anyone had luck with getting RLS working? I have it installed via vim-plug using the instructions in the README, but none of the keymappings shown in the README work. K does nothing. both gd and the F2 key make “Not found!” appear in the status line. When I first open a Rust source file in Vim, the status line displays “LanguageClient project root: /path/to/repo” but doesn’t show any sort of “initializing” or “complete” message.


#10

Yes, RLS works easily. Installing it via vim-plug sounds like the wrong thing to do, which README are you referring to? Did you check :messages if there’s any kind of error? Which client pluting are you using?


#11

It looks like it actually is working — I suspect what happened before is that I coincidentally tried the keybindings on types that were in dependent crates and hence it couldn’t jump to their definitions.

Also, to clarify, I installed RLS itself through rustup. It’s LanguageClient-neovim that I installed with vim-plug.


#12

Ok, that makes a lot more sense :slight_smile: Great it’s working for you :+1:


#13

I highly recomend YouCompleteMe for reasonable racer autocometion out of the box.