Transferred from: http://blog.jobbole.com/58978/
There may be a period of time to write Linxu, inevitably with vim, keep, find time to actually exercise
Source: Marchtea's Blog
The first time I heard this plugin or in the occasional case to see someone else's blog, heard the name of the plugin. Originally intended to complete the installation during the training, but the network is not to force , but also dragged to the home. This plugin is not easy to install when it comes to getting ready for work, and it is well-deserved to install. (For information on this, please see another article) However, there is always a return on the pay, installed after the use of this plug-in, sincerely for the powerful of this plugin to be impressed.
So what's the difference between this plugin? What's special about youcompleteme based on semantic complements
As is known to all,Vimit is a text editor. In other words, its most basic work is to edit the text, regardless of the content of the text.Vimafter being used by programmers, it is slowly being entrusted with the same work as the IDE, text auto-completion (ieacp,omnicppcompleter), code checking () andSyntasticso on.
There are two main implementations of this function for text auto-completion .
What we often use, theomnicppcompleteracpway vim comes into being,c-x, c-nis based on text. A more popular statement is actually a word: guess
It uses text to make some regular expression matching, and then according to the generated tags (using thectagsbuild) to achieve the effect of auto-completion.
As the name implies, it is through the analysis of source files, after the completion of the grammar analysis . Because of the analysis of the source files, semantic-based complements can be very precise. But this is clearly not supported by VIM. And after so many years of development, because the grammatical analysis has very high difficulty, also has not been suitable tool to appear. Until, backed by Apple , turned outclang/llvm. IsYouCompleteMeclang/llvmbuilt on the basis of it. Integrated implementation of a variety of plug-ins
- Clang_complete
- Autocomplpop
- Supertab
- Neocomplcache
- Syntastic (similar function, only for C/C++/OBJ-C code)
Supported languages
For other languages, the Vim settings are calledomnifuncto match, so the same supportsphp, and sorubyon.
Known to have * Javascript--tern_for_vim * Ruby/java--eclim use
Use feeling
- As with the IDE, auto-complete,
- includeComplete completion of the file according to
- No more lame to build tags
- The completion is very accurate, and the speed is very fast, there is no delay (previously on large projects, ACP used to be very card)
- Supports tags-like jumps, jumps to definitions and places of use
- The error hint is smart, and it's really silky smooth, no input:wfor mandatory testing.
Installation
With all the good things going on, it's about installation . Unlike other Vim plugins in the past, YCM is a compiled plug-in. After you download it, you need to compile it manually before you can use it. For other plugins, simply drop the. vim file into the appropriate folder. This also increases the difficulty of using YCM. Installation Preparation
- Latest version of Vim (7.3.584+), compile-time Add +python flag (already installed can bevim --versionviewed)
- CMake (Mac can be installed via homebrewbrew install cmake, Ubuntu can be passedsudo apt-get install cmake)
- Installing the Vundle plug-in for installing a vim-management plugin
Quick Install under Mac
.vimrcAdd the following code in the
Shell
Bundle ' Valloric/youcompleteme '
After saving exit, open vim and enter in normal mode
Shell
: Bundleinstall
Wait forvundlethe Youcompleteme installation to complete
Then compile the installation:
Shell
CD ~/.vim/bundle/youcompleteme
./install--clang-completer
If you do not need to complete the c-family, you can remove the--clang-completer。completion if necessary c#, please add--omnisharp-completer。
Normally, ycm will download the clang package, and if it does, it can also use the system--system-libclang。
This is the end of the installation. Open Vim, if not prompted YCM is not compiled, the installation has been successful. Manually compiling the installation
The installed scripts are not always useful at all, at least for me. There is a problem after installation, refer to issue#809.
:BundleInstallComplete or use the installation
Shell
1
git clone--recursive https://github.com/Valloric/YouCompleteMe.git
Get the latest warehouse and then use thegit submodule update --init --recursiveconfirm warehouse integrity to begin the installation process.
- Download the latest clang binaries ycm requiresclangversion > 3.2, which is generally the latest download.
- Install Python-dev. (used under Ubuntusudo apt-get install python-dev, Mac is provided by default, otherwise please install command line tools)
- Compile
Shell
1
2
3
4
CD ~
mkdir Ycm_build
CD Ycm_build
Cmake-g "Unix makefiles"-dpath_to_llvm_root=~/ycm_temp/llvm_root_dir. ~/.vim/bundle/youcompleteme/cpp make Ycm_support_libs
It is important to note that the ~/ycm_temp/llvm_root_dir contains the contents (includinginclude, and so on) extracted from the compressed package downloaded in the first stepbin.
This completes and begins to feel that the YCM offers a complete set of auto-completion features that are not inferior to those provided by large Ides. Configuration
Unlike many Vim plugins, YCM needs to be compiled first, and it needs to be configured. After Vim starts, YCM will look for the current path and the upper path.ycm_extra_conf.py. The~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.pydefault template is provided in. You can also refer to my (just change it on the template). However, this solves the problem that the standard library hints could not be found.
In general, I will~drop a default template in the directory and then copy.ycm_extra_conf.py。it in the current directory according to the different items.
Shell
# This file isn't licensed under the GPLv3, which is the license for the rest
# of Youcompleteme.
#
# Here's the license text for this file:
#
# This was free and unencumbered software released to the public domain.
#
# anyone is free-to-copy, modify, publish, use, compile, sell, or
# Distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# in jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate "any" and "all" copyright interest in the
# Software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend This dedication is an overt act of
# relinquishment in perpetuity of any present and future rights to this
# Software under copyright law.
#
# The software is provided ' as is ', without WARRANTY of any KIND,
# EXPRESS OR implied, including but not LIMITED to the warranties of
# merchantability, FITNESS for A particular PURPOSE and noninfringement.
# in NO EVENT shall the AUTHORS is liable for any CLAIM, damages OR
# Other liability, WHETHER in an ACTION of contract, TORT OR OTHERWISE,
# arising from, out of or in CONNECTION with the software or the use or
# Other dealings in the software.
#
# For more information, refer to
Import OS
Import Ycm_core
# These is the compilation flags that would be used in case there ' s no
# Compilation database Set (by default, one was not set).
# change this LIST of FLAGS. YES, this is the DROID that has BEEN looking for.
Flags = [
'-wall ',
'-wextra ',
# '-werror ',
# '-wc++98-compat ',
'-wno-long-long ',
'-wno-variadic-macros ',
'-fexceptions ',
'-stdlib=libc++ ',
# This is important! Without a "-std=<something>" flag, Clang won ' t know which
# language to use when compiling headers. So it would guess. Badly. So C + +
# headers'll be compiled as C headers. You don ' t want, that's always specify
# a "-std=<something>".
# for a C project, you would set this to something like ' C99 ' instead of
# ' c++11 '.
'-std=c++11 ',
# ... and the same thing goes for the magic-x option which specifies the
# language that the files to is compiled is written in. This is mostly
# relevant for C + + headers.
# for a C project, you would set the this to ' C ' instead of ' C + + '.
'-X ',
' C + + ',
'-I. ',
‘.‘,
'-isystem ',
'/usr/include ',
'-isystem ',
'/usr/local/include ',
'-isystem ',
'/library/developer/commandlinetools/usr/include ',
'-isystem ',
'/library/developer/commandlinetools/usr/bin/. /lib/c++/v1 ',
]
# Set the absolute path to the folder (not the file!) containing the
# Compile_commands.json file to use that instead of ' flags '. See here for
# more Details:http://clang.llvm.org/docs/jsoncompilationdatabase.html
#
# Most projects won't need to set this to anything; Can just change the
# ' Flags ' list of compilation flags. Notice that YCM itself uses that approach.
Compilation_database_folder = ' '
If Os.path.exists (Compilation_database_folder):
Database = Ycm_core.compilationdatabase (Compilation_database_folder)
Else
Database = None
Source_extensions = ['. cpp ', '. cxx ', '. CC ', '. C ', '. m ', '. MM ']
Def directoryofthisscript ():
Return Os.path.dirname (Os.path.abspath (__file__))
def makerelativepathsinflagsabsolute (Flags, working_directory):
If not working_directory:
return list (Flags)
New_flags = []
Make_next_absolute = False
Path_flags = ['-isystem ', '-I ', '-iquote ', '--sysroot= ']
For flag in Flags:
New_flag = Flag
If Make_next_absolute:
Make_next_absolute = False
If not flag.startswith ('/'):
New_flag = Os.path.join (working_directory, flag)
For Path_flag in Path_flags:
If flag = = Path_flag:
Make_next_absolute = True
Break
If Flag.startswith (Path_flag):
Path = flag[len (path_flag):]
New_flag = Path_flag + os.path.join (working_directory, Path)
Break
If New_flag:
New_flags.append (New_flag)
Return New_flags
def isheaderfile (filename):
Extension = os.path.splitext (filename) [1]
return extension in ['. h ', '. hXX ', '. hpp ', '. HH ']
def getcompilationinfoforfile (filename):
# The Compilation_commands.json file generated by CMake does not has entries
# for header files. So we did our best by asking the DB for flags for a
# corresponding source file, if any. If one exists, the flags for that file
# should be good enough.
If Isheaderfile (filename):
basename = os.path.splitext (filename) [0]
For extension in Source_extensions:
Replacement_file = basename + extension
If Os.path.exists (replacement_file):
Compilation_info = database. Getcompilationinfoforfile (
Replacement_file)
If Compilation_info.compiler_flags_:
Return Compilation_info
Return None
return database. Getcompilationinfoforfile (filename)
def flagsforfile (filename, **kwargs):
If database:
# Compilation_info.compiler_flags_ does not return a
# python list, but a "list-like" Stringvec object
Compilation_info = getcompilationinfoforfile (filename)
If not compilation_info:
Return None
Final_flags = Makerelativepathsinflagsabsolute (
Compilation_info.compiler_flags_,
COMPILATION_INFO.COMPILER_WORKING_DIR_)
# note:this is just for youcompleteme; It ' s highly likely that your project
# does not need to remove the STDLIB flag. Do not use the this in YOUR
# ycm_extra_conf IF You ' RE isn't 100% sure you need IT.
#try:
# final_flags.remove ('-stdlib=libc++ ')
#except ValueError:
# Pass
Else
relative_to = Directoryofthisscript ()
Final_flags = Makerelativepathsinflagsabsolute (Flags, relative_to)
return {
' Flags ': Final_flags,
' Do_cache ': True
Additional features provided by Youcompleteme
YCM In addition to providing the basic completion function, automatic prompt error function, but also provides a similar tags function:
- Jump to DefinitionGoToDefinition
- Jump to DeclarationGoToDeclaration
- And the combination of the two.GoToDefinitionElseDeclaration
You can configure the appropriate shortcut keys in the. vimrc.
Shell
1
2
3
Nnoremap <leader>gl:ycmcompleter gotodeclaration<cr>
Nnoremap <leader>gf:ycmcompleter gotodefinition<cr>
Nnoremap <leader>gg:ycmcompleter gotodefinitionelsedeclaration<cr>
In addition, YCM offers a wide range of configuration options, also configured in. vimrc. For details, please refer to
Shell
1
2
Let G:ycm_error_symbol = ' >> '
Let G:ycm_warning_symbol = ' >* '
At the same time, YCM can open location-list to display warnings and error messages:YcmDiags。personal about the YCM configuration as follows:
Shell
1
2
3
4
5
6
7
"For YCM
Let G:ycm_error_symbol = ' >> '
Let G:ycm_warning_symbol = ' >* '
Nnoremap <leader>gl:ycmcompleter gotodeclaration<cr>
Nnoremap <leader>gf:ycmcompleter gotodefinition<cr>
Nnoremap <leader>gg:ycmcompleter gotodefinitionelsedeclaration<cr>
Nmap <F4>:ycmdiags<cr>
The YCM provides a jump function using Vim'sjumplist,forward and backward shortcuts asCtrl+Owell as aCtrl+I。summary
YouCompleteMeI used the most refreshing of an auto-complete plug-in. Before using the ACP, encountered large files are basically dead, so that they do not dare to use. Due to the ycm use of the C/s structure, part of the use of Vim scripting, part of the thought of native code, making it fast running.
Abandon Vim comes with the pit father's complement, abandon ctagsit, abandon cscopeIt, YCM is the ultimate 补全神器。
During the installation process, I also encountered a lot of pits. An article will be sent to solve these pits.
Finally, I wish you a smooth code year, a yard of plains, yards to work.
Vim Auto Complete artifact: youcompleteme (Turn)