Vim/YouCompleteMe

From ArchWiki
< Vim

YouCompleteMe (shortened as YCM) is a code-completion engine for Vim. It supports the following languages:

  • C/C++/Objective-C/Objective-C++
  • Python
  • C#
  • Go
  • Rust
  • Java
  • JavaScript
  • TypeScript
  • Other languages (Ruby, PHP etc.) through the use of omnicompletion system

Installation

Install the vim-youcompleteme-gitAUR package. For an alternative manual way of installing YouCompleteMe see upstream instructions.

Configuration

C/C++

YCM uses a python script called .ycm_extra_conf.py to set project wide settings, needed to provide completion and syntax check. A brief introduction to essential configuration follows, for details and advanced options see the upstream documentation.

Alternatively, provide a compile_commands.json in the project's root.

Extra conf structure

A sample .ycm_extra_conf.py may be found in [1]. You should save a copy of this file in your project folder and customize it with adequate settings for your source files.

The most important settings (which usually suffices for a minimal configuration) are the -x and --std options, which respectively tells the syntax checker the language used in the project and the standard followed. The -x value may be set to c or c++, while common values for the --std are --std=c89, --std=c99, --std=c11, --std=c14 and their respective c++ version. The standard parameter will determine the warning and the errors in the syntax check (e.g., a line commented with // will be marked as unallowed in C89, but not under following versions of the standard).

A third party script and vim plugin for the automatic generation of the .ycm_extra_conf.py is available on this repo.

Extra conf location

The program searches for the .ycm_extra_conf.py file on startup in the current source file directory and in its parent folders. If the file is not found, YCM features are not available. A global file (used as fallback when a local extra conf file is not found) may be set adding the following to ~/.vimrc:

~/.vimrc
let g:ycm_global_ycm_extra_conf = '/path/to/the/file'

Being the extra conf file a python script, when a file is found a confirmation is asked for security reason before to load it. This behaviour may be disabled adding the following to ~/.vimrc:

~/.vimrc
let g:ycm_confirm_extra_conf = 0

For a less unsecure solution, when the confirmation is enabled an extra conf file blacklist/whitelist may be set assigning a list of patterns to the ycm_extra_conf_globlist variable. A file matching one pattern is blacklisted if the pattern begins with !, whitelisted otherwise, confirmation is asked if the file does not match any pattern. Rule precedence is determined by the order, and the first match is applied. Some glob pattern rules are available:

  • * matches everything
  • ? matches any single character
  • [seq] matches any character in seq
  • [!seq] matches any char not in seq

In example, with the following setting

~/.vimrc
let g:ycm_extra_conf_globlist = ['~/dev/*','!~/*']

any file in ~/dev will be whitelisted, any in ~/ will be blacklisted, and due to order precedence any file in ~/ excepted the ~/dev folder will be blacklisted.

Java

YCM has integrated support for jdt.ls which can be installed by passing --java-completer to the install.py script.

Java - alternative

For Java completion, a project file should be present and Eclim headless server must be running.

Install eclimAUR, then edit your .vimrc as follows:

~/.vimrc
let g:EclimCompletionMethod = 'omnifunc'

Start eclimd script in a separate terminal:

$ /usr/lib/eclipse/plugins/org.eclim_$pkgver/bin/eclimd

Create a file named .project in the same directory as your Java files:

.project
<projectDescription>
    <name>PROJECTNAME</name>
</projectDescription>

Open your Java file in Vim and run:

:ProjectCreate . -n java

To compile the project:

:ProjectBuild

To run the project:

:Java

To run only current file:

:Java %

A list of available commands may be found here.

C#

Before starting work with a C# project, ensure that mono-msbuild is installed on your system as it is a required dependency of Omnisharp-Roslyn, the C# completion engine used by YouCompleteMe. More information can be found in Omnisharp-Roslyn's README and the following Github issue.

Note: For C# completion to "just work" simply create an empty .sln file in current or parent directory. The rest of this section explains how to manually create a C# project which can also be built from command line with xbuild.

First create a solution file:

SOLUTION.sln
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{00000000-0000-0000-0000-000000000000}") = "PROJECT", "PROJECT\PROJECT.csproj", "{11111111-1111-1111-1111-111111111111}"
EndProject
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|x86 = Debug|x86
		Release|x86 = Release|x86
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{11111111-1111-1111-1111-111111111111}.Debug|x86.ActiveCfg = Debug|x86
		{11111111-1111-1111-1111-111111111111}.Debug|x86.Build.0 = Debug|x86
		{11111111-1111-1111-1111-111111111111}.Release|x86.ActiveCfg = Release|x86
		{11111111-1111-1111-1111-111111111111}.Release|x86.Build.0 = Release|x86
	EndGlobalSection
EndGlobal

Then create a directory named PROJECT and in it a file named PROJECT.csproj:

PROJECT/PROJECT.csproj
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProductVersion>10.0.0</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{11111111-1111-1111-1111-111111111111}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>PROJECT</RootNamespace>
    <AssemblyName>PROJECT</AssemblyName>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug</OutputPath>
    <DefineConstants>DEBUG;</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <ConsolePause>false</ConsolePause>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <DebugType>full</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release</OutputPath>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <ConsolePause>false</ConsolePause>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <ItemGroup>
    <Compile Include="HelloWorld.cs" />
    <Compile Include="CSharpFile1.cs" />
    <Compile Include="CSharpFile2.cs" />
  </ItemGroup>
</Project>

Place your C# files in PROJECT directory and do not forget to manually add them at the bottom of PROJECT/PROJECT.csproj.

Now YouCompleteMe should work for C# files in that directory and you can build the project. To compile the project from inside Vim:

:!xbuild

Troubleshooting

Remember that it might take some time for YouCompleteMe to generate a list of completion strings.

The following commands are available for diagnostics:

  • :messages - show previous errors or messages from Vim
  • :YcmDiags
  • :YcmDebugInfo

E764: Option 'omnifunc' is not set

If this happens for Java files, you forgot to put this in your .vimrc:

~/.vimrc
let g:EclimCompletionMethod = 'omnifunc'

No completion in Java files

Make sure eclimd daemon is running:

$ ps -ax|grep eclimd

and that you have first generated project files.

URLError: <urlopen error [Errno 111] Connection refused>

This error appears when you do not have a .sln file in current or parent directory.

RuntimeError: Error starting OmniSharp server: no solutionfile found

Same as above.

See also