Getting Rust autocompletion in Vim


I am trying to install Racer for Vim to get some Rust autocompletion in my Vim. I guess
this will make my life a little bit easier.

First I installed racer which seems to work fine, executing racer complete std::io::B in my command line does give some options.

Finally I installed Vim-racer using Pathogen:

git clone --depth=1 ~/.vim/bundle/vim-racer

I added the racer_cmd variable in my vimrc like they asked:

" For Rust
let g:racer_cmd = "/Users/oniel/.cargo/bin/racer"

I restarted vim, opened a .rs file but when pressing Cmd + X, or Control + X (normally the shortcut for getting some completion suggestions) nothing happens.

What did I miss?

Full vimrc:

set nocompatible
" Initialisation de pathogen
call pathogen#infect()
call pathogen#helptags()

set number
set tabstop=4
set shiftwidth=4
set expandtab
set smartindent
set autoindent

filetype plugin indent on
syntax on
runtime! config/**/*.vim

autocmd VimEnter * NERDTree
autocmd BufWinEnter * NERDTreeMirror
"autocmd BufWinEnter * NERDTreeMinor

set guifont=Source\ Code\ Pro\ SemiBold:h16

let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1
set laststatus=2

let g:airline_powerline_fonts = 1
let g:airline_theme='minimalist'

colorscheme afterglow

set guioptions-=r  "remove right-hand scroll bar
set guioptions-=L

set scrolloff=5

"nnoremap <ScrollWheelUp> k
"nnoremap <ScrollWheelDown> j

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

"" Use the vim-plug plugin manager:
" Remember to run :PlugInstall when loading this vimrc for the first time, so
" vim-plug downloads the plugins listed.
silent! if plug#begin('~/.vim/plugged')
Plug 'OmniSharp/omnisharp-vim'
Plug 'w0rp/ale'
call plug#end()

" Note: this is required for the plugin to work
filetype plugin indent on

" Use the stdio OmniSharp-roslyn server
let g:OmniSharp_server_stdio = 1

" Set the type lookup function to use the preview window instead of echoing it
"let g:OmniSharp_typeLookupInPreview = 1

" Timeout in seconds to wait for a response from the server
let g:OmniSharp_timeout = 5

" Don't autoselect first omnicomplete option, show options even if there is only
" one (so the preview documentation is accessible). Remove 'preview' if you
" don't want to see any documentation whatsoever.
set completeopt=longest,menuone,preview

" Fetch full documentation during omnicomplete requests.
" By default, only Type/Method signatures are fetched. Full documentation can
" still be fetched when you need it with the :OmniSharpDocumentation command.
"let g:omnicomplete_fetch_full_documentation = 1

" Set desired preview window height for viewing documentation.
" You might also want to look at the echodoc plugin.
set previewheight=5

" Tell ALE to use OmniSharp for linting C# files, and no other linters.
let g:ale_linters = { 'cs': ['OmniSharp'] }

" Update semantic highlighting after all text changes
let g:OmniSharp_highlight_types = 3
" Update semantic highlighting on BufEnter and InsertLeave
" let g:OmniSharp_highlight_types = 2

augroup omnisharp_commands

    " Show type information automatically when the cursor stops moving
    autocmd CursorHold *.cs call OmniSharp#TypeLookupWithoutDocumentation()

    " The following commands are contextual, based on the cursor position.
    autocmd FileType cs nnoremap <buffer> gd :OmniSharpGotoDefinition<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>fi :OmniSharpFindImplementations<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>fs :OmniSharpFindSymbol<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>fu :OmniSharpFindUsages<CR>

    " Finds members in the current buffer
    autocmd FileType cs nnoremap <buffer> <Leader>fm :OmniSharpFindMembers<CR>

    autocmd FileType cs nnoremap <buffer> <Leader>fx :OmniSharpFixUsings<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>tt :OmniSharpTypeLookup<CR>
    autocmd FileType cs nnoremap <buffer> <Leader>dc :OmniSharpDocumentation<CR>
    autocmd FileType cs nnoremap <buffer> <C-\> :OmniSharpSignatureHelp<CR>
    autocmd FileType cs inoremap <buffer> <C-\> <C-o>:OmniSharpSignatureHelp<CR>

    " Navigate up and down by method/property/field
    autocmd FileType cs nnoremap <buffer> <C-k> :OmniSharpNavigateUp<CR>
    autocmd FileType cs nnoremap <buffer> <C-j> :OmniSharpNavigateDown<CR>

    " Find all code errors/warnings for the current solution and populate the quickfix window
    autocmd FileType cs nnoremap <buffer> <Leader>cc :OmniSharpGlobalCodeCheck<CR>
augroup END

" Contextual code actions (uses fzf, CtrlP or unite.vim when available)
nnoremap <Leader><Space> :OmniSharpGetCodeActions<CR>
" Run code actions with text selected in visual mode to extract method
xnoremap <Leader><Space> :call OmniSharp#GetCodeActions('visual')<CR>

" Rename with dialog
nnoremap <Leader>nm :OmniSharpRename<CR>
nnoremap <F2> :OmniSharpRename<CR>
" Rename without dialog - with cursor on the symbol to rename: `:Rename newname`
command! -nargs=1 Rename :call OmniSharp#RenameTo("<args>")

nnoremap <Leader>cf :OmniSharpCodeFormat<CR>

" Start the omnisharp server for the current solution
nnoremap <Leader>ss :OmniSharpStartServer<CR>
nnoremap <Leader>sp :OmniSharpStopServer<CR>

" Enable snippet completion
let g:OmniSharp_want_snippet=1

" For Rust
let g:racer_cmd = "/Users/oniel/.cargo/bin/racer"

Hey, I had a lot more success using coc-rust-analyzer than racer, so you might want to try that.

1 Like

Thanks, works like a charm!! Is it possible to bind it to a shortcut? So it only displays suggestions when I'm pressing specific keys. Or to customize the looks of it? Thanks.

1 Like

Take a look at the help page on :help coc-configuration or the readme of the github page.

1 Like