Ubuntu 16.04 Vim Installation and configuration "turn"

Source: Internet
Author: User
Tags parent directory set background

Transferred from: http://www.cnblogs.com/ace-wu/p/6273031.html

Install Vim

The Vim-tiny is already installed by default

Acewu@acewu-computer:~$ Locate VI | Grep' vi$ ' |xargs ls-allrwxrwxrwx1 root root17December921st:12/etc/alternatives/vi/usr/bin/vim.tinylrwxrwxrwx1 root root20December921st:13/usr/bin/VI/etc/alternatives/vi-rw-r--r--1 root root 59 12 month 23 2015 /usr/lib/x86_ 64-linux-gnu/espeak-data/voices/asia/vi-rw-r--r--1 root root 1415 11 month 11 2015 /usr/share/bash-completion/ completions/ldapvi-rw-r--r--1 root root 475 4 month 21 2016 /var/lib< Span class= "Hljs-regexp" >/dpkg/alternatives/vi          

Let's take Ubuntu14.04 (Unity) to install the full version of VIM (Vim-basic) as an example:

Enter the following command at the terminal and it will be installed automatically:

sudo apt-get install vim

After automatic installation, you can use the following command to view:

has been added to the system

update-alternatives --display vi

View version

--version
Vim Configuration

Note: Only for individual users in their own current directory. VIMRC modification, the content is only valid for the user, to be effective, you can modify the/ETC/VIM/VIMRC.

When using vim for editing under the terminal, by default, the editing interface is not displaying line numbers, syntax highlighting, smart indenting, and so on. In order to better work under Vim, a configuration file needs to be set manually:. vimrc.

When Vim is started, the. vimrc file under the current user's root directory is automatically read, and the file can contain settings and even scripts, so it is generally convenient to create a. vimrc file under the current user's root directory, which is the command created

$vi ~/.vimrc

After Setup: X or: X or Wq to save and exit.

Here is an example that lists the settings that are often used, and the details of the settings are given in the reference

Basic configuration [1] [2]
"开启语法高亮syntax on"依文件类型设置自动缩进filetype indent plugin on "显示当前的行号列号:set ruler"在状态栏显示正在输入的命令set showcmd "关闭/打开配对括号高亮"NoMatchParenDoMatchParen行号的显示与隐藏[3]:"显示行号:set number"为方便复制,用<F2>开启/关闭行号显示:nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>

Enable Modeline (that is, allow the edited file to set the VIM option in the form of annotations, see Vim Wiki:modeline Magic) [4]:

set modeline

If the terminal is using a dark background:

"为深色背景调整配色set background=dark
Plug-in Management

Many of the configurations below depend on the Vim plugin developed by third parties. In order to conveniently install and maintain these plug-ins, it is recommended to install a plug-in management tool, currently the more popular is vundle and pathogen. The two functions are similar, but the Vundle configuration is more flexible and the pathogen is relatively concise. Both work well with git, and it's easy to install plugins.

If you use Git to manage your profile, you can also combine pathogen or vundle with Git's submodule feature management plug-in [5], which makes it easy to sync plug-ins between multiple computers and update plugins locally.

# 在存放配置文件的主文件夹下,添加一个 submodulegit submodule add https://github.com/gmarik/vundle.git .vim/bundle/vundle # 用类似的办法添加多个插件后,以后升级插件只需:git submodule update # 如果其它电脑 checkout 出配置文件后,要先:git submodule init

The plug-in management tool does not need to be installed through the system's package management system, as long as the required files are placed in the ~/.vim/directory. In this way, a set of configuration files can also be easily used in many different system environments.

Code completion

Vim 7 has built-in code completion function [6], complete operation can be divided into two kinds:

Keyword completion

That is, it simply complements the words already in the document, and the shortcut keys are ctrl-n or ctrl-p.

Smart Completion

In Vim 7, Omni Complete is introduced, and the shortcut key is Ctrl-x Ctrl-o according to the semantic complement.
Vim's Completion menu operates in a different way than the general IDE, and can be added as follows [7]:

"Let Vim's completion menu behave in accordance with the General IDE (ref. VIMTIP1228) Set Completeopt+=longest" Automatically closes the preview window after leaving insert mode autocmd Insertleave * if pumvisible () = = 0| Pclose|endif "Enter to select the current item Inoremap<Expr><Cr> pumvisible ()? "\<C-y> ":" \<Cr> "" Up and down key behavior Inoremap<Expr><Down> pumvisible ()? "\<C-n> ":" \<Down> "Inoremap<Expr><Up> pumvisible ()? "\<C-p> ":" \<Up> "Inoremap<expr> < Pagedown> pumvisible ()? "\<pagedown>\< c-p>\<c-n> ":" \ <pagedown> "Inoremap <expr> <pageup> pumvisible ()? "\<pageup>\< c-p>\<c-n> ":" \ <pageup> "        

For automatic completion, you can install the Autocomplpop plug-in, after installation, if you want to display the document (preview), you can add the settings:

let g:acp_completeoptPreview = 1
Annotation Management

It is often cumbersome to comment out or uncomment a piece of code, especially for some languages that only support single-line comments, and a quick comment (or uncomment) of the code is useful.

The common annotation management plug-in is the NERD commenter, which supports multiple languages by default, and is easy to use: simply select a piece of code in the visual (V) mode, press \CC to annotate, \cu uncomment, \CM add block comments.

If there are other needs, can also consider comments, enhcommentify and other plug-ins.

Indent in

The use of automatic indentation may need to be set, vim in the details of the automatic indentation is described in the Vim Code indentation settings.

There are different ways to adjust indents in different modes:

Insert mode

Ctrl-t increases indentation, ctrl-d decreases indentation.

Command mode
>> 右缩进, << 左缩进,注意n<< 或 n>>是缩进多行,如4>>
Visual mode

<, > for the left and right indentation, n<, n> can be more than a section indentation, such as 2>.
In addition, = The selected part can be automatically indented,]p can implement the Paste function of P, and automatically indent.

Code browsing and jumping

Code jumps are similar to the CTRL + click feature in the IDE. Similar to code completion, code-browsing tools can be categorized as text-based analysis and code-based understanding.

Simple code Jump

The following commands can be used directly in command mode: [8]

Jump to Definition

GD to the definition of local variable, GD to global variable definition

Search
# 可对光标处的词向前/向后做全词搜索,g*, g# 做相对应的非全词匹配搜索
Code block end

[[,]] jumps to the brace at the beginning or end of the current code block.

Brackets End to end

% can jump in pairs of parentheses, before the end of the block.

Location History

Ctrl-o in the history of the backstage, Ctrl-i is forward.

A jump based on code understanding

This feature relies on the Ctags tool. [9] After installing the ctags, run in the folder where the code is stored

ctags -R .

You can generate a tags file that describes the structure of your code.

Tip: Ctags is a powerful and detailed configuration, please refer to its documentation.

It is recommended to add the following configuration in ~/.VIMRC to enable Vim to find the tags file in the parent directory [10]:

set tags=tags;/

Once set up, you can use the following functions in VIM: Ctrl] go to the appropriate tag,ctrl-t for best bets to return to the previous match. If there are multiple matches, G-CTRL] Displays all of the available tags. If necessary, interchangeable Ctrl] and G-CTRL] [11]:

"在普通和可视模式中,将<c-]>与g<c-]>互换nnoremap <c-]> g<c-]>vnoremap <c-]> g<c-]> nnoremap g<c-]> <c-]>vnoremap g<c-]> <c-]>
In-place compilation and error handling

Use: Make can call the build command compiler, with vim built-in quickfix function, you can open a window like the IDE to show compilation errors and warnings, it can also easily jump to the location of the compilation error.

Commonly used commands are: Cw[indow] (if there is an error opening the Quickfix window),: CN (Skip to the next error),: CP (skip to the previous error), etc., specifically: Help Quickfix.

If you want to automatically open the Quickfix window when there is an error [12]:

" 编译后,如有错误则打开quickfix窗口。(光标仍停留在源码窗口)"" 注意:需要开启netsting autocmdautocmd QuickFixCmdPost [^l]* nested cwindowautocmd QuickFixCmdPost l* nested lwindow

By default,: Mak[e] jumps to the first error, and if you do not want to enable this feature, use: make!.

Code Folding

Code folding is enabled when the code is long, such as highlighting elements collapsed by syntax:

set foldmethod=syntax"默认情况下不折叠set foldlevel=99

You can then use the Z-series commands to manage code folding. If za flips the collapsed state of the current position, za recursively flips the collapsed state of all the code in the current layer. Of course, you can also map function keys to za:

map <F3> za
View Documents

The K key can be used to view the document for the current function.

Other plugins

The code that is still actively maintained recently is hosted on GITHUB plug-ins [13]:

    1. Fugitive allows Vim to better integrate with Git.
    2. Powerline's practical status
    3. Tagbar more modern code structure browsing tool than taglist
    4. The NERD tree to browse files in a folder
    5. Syntastic Grammar Check
    6. Surround.vim quickly delete/modify pairs of parentheses around the cursor
      Other common plug-ins [14]:

Project
Facilitates the management of documents in the project

Snipmate.vim
Implementing template expansion in editing

A.vim
In the. Cpp/.h and other file pairs jump

Matchit.zip
Enhanced% of functionality

Winmanager
Stack the File Management window and the TagList.

Related documents
      1. Configuring a VIM-based Python programming environment
      2. Let vim automatically determine the Chinese code

        Resources
      3. How to setup VIM properly for editing Python files-*.py (StackOverflow)
      4. Amix's VIMRC
      5. Python and Vim:make your own IDE (2009.2)
      6. Introduction to the Python website vim
      7. Git Tools-submodules
      8. About the new features of Vim 7-linuxtoy
      9. Vimtip 1228:improve Completion popup Menu
      10. Vim documentation
      11. Browsing programs with tags--Vim Tips Wiki
      12. Vim and Ctags tips and tricks
      13. Use:tjump instead of:tag vim on pressing CTRL] (StackOverflow)
      14. Automatically open the Quickfix window On:make
      15. Linuxtoy Xu Xiaodong's vimenv
      16. Teach you how to convert vim into an IDE programming environment (GRAPHIC)

Ubuntu 16.04 Vim Installation and configuration "turn"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.