Vim configuration, plugins and tips

 

As the saying goes: If you want to do a good job, you must first sharpen your tool. As a programmer, a commonly used tool is an editor . I choose an editor vim that can greatly improve my development efficiency (some people may choose emacs ). The vim editor has the following features:

  • Cross-platform and unified environment
    Whether in windows or *nix, vim is a perfect cross-platform text editor, and can even be directly configured and used on server platforms CentOS, Ubuntu, etc. The configuration files are similar, and the operating habits are basically the same.

  • Customizable and extensible
    vim provides a vimrc configuration file to configure vim, and you can customize some plugins to achieve file browsing ( NERD Tree ), code completion ( YouCompleteMe , syntax checking ( syntastic ), file fuzzy search ( ctrlp ), Display vim status bar ( Vim Powerline ), theme color ( Molokai ), display file structure ( tagbar ) and other functions.

  • Efficient command line
    Editing text with vim can only be done on the keyboard, no mouse at all. In the case of cursor movement, it is much more efficient to press a key to move in units of words, lines, blocks, or functions, compared to repeated keystrokes, character-by-character, or line-by-line movement. Sometimes some operations of repeated deletion and pasting can be completed with only one command, and you can even use key mapping to simplify or combine multiple commands to improve efficiency.

configure

If you need to configure vim, just create a ~/.vimrc file in the Home directory to configure vim, you can refer to my vimrc configuration file. Since I need to install plugins, and separate the list of plugins to be installed to another file ~/.vimrc.bundles , this file is also stored in the Home directory, and the content of the file can refer to vimrc.bundles . To load the ~/.vimrc.bundles file, you must add the following code snippet to the ~/.vimrc file:

if filereadable(expand("~/.vimrc.bundles"))
  source ~/.vimrc.bundles
endif

plugin

Plugin management tool vunble

Vundle is a plugin management tool for vim, it can search, install, update and remove vim plugins, no longer need to manually manage vim plugins.

  1. Create a ~/.vim directory and a .vimrc file in the Home directory (you can copy my vimrc file)
  2. install vundle

    git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
  3. Add vundle support in .vimrc configuration file
    filetype off
    set rtp+=~/.vim/bundle/vundle/
    call vundle#rc()
    But in fact, I added a ~/.vimrc.bundles file to save the configuration of all plugins. The following code snippet must be added to the ~/.vimrc file:
    if filereadable(expand("~/.vimrc.bundles"))
    source ~/.vimrc.bundles
    endif
    The content of the ~/.vimrc.bundles file must contain:
    filetype off
    set rtp+=~/.vim/bundle/vundle/
    call vundle#rc()
    You can copy my ~/.vimrc.bundles file to the Home directory.

Install the plugin

Bundles are divided into three categories, the more commonly used is the second :

  1. For repos under the Github vim-scripts user, just write the repos name
  2. For repos under other users on Github, you need to write "username/repos name"
  3. For plugins not on Github, you need to write out the full path of git

Bundle Type.png


Configure the installed plugins in ~/.vimrc , but I put the plugin configuration information in ~/.vimrc.bundles :

" Define bundles via Github repos
Bundle 'christoomey/vim-run-interactive'
Bundle 'Valloric/YouCompleteMe'
Bundle 'croaky/vim-colors-github'
Bundle 'danro/rename.vim'
Bundle 'majutsushi/tagbar'
Bundle 'kchmck/vim-coffee-script'
Bundle 'kien/ctrlp.vim'
Bundle 'pbrisbin/vim-mkdir'
Bundle 'scrooloose/syntastic'
Bundle 'slim-template/vim-slim'
Bundle 'thoughtbot/vim-rspec'
Bundle 'tpope/vim-bundler'
Bundle 'tpope/vim-endwise'
Bundle 'tpope/vim-fugitive'
Bundle 'tpope/vim-rails'
Bundle 'tpope/vim-surround'
Bundle 'vim-ruby/vim-ruby'
Bundle 'vim-scripts/ctags.vim'
Bundle 'vim-scripts/matchit.zip'
Bundle 'vim-scripts/tComment'
Bundle "mattn/emmet-vim"
Bundle "scrooloose/nerdtree"
Bundle "Lokaltog/vim-powerline"
Bundle "godlygeek/tabular"
Bundle "msanders/snipmate.vim"
Bundle "jelera/vim-javascript-syntax"
Bundle "altercation/vim-colors-solarized"
Bundle "othree/html5.vim"
Bundle "xsbeats/vim-blade"
Bundle "Raimondi/delimitMate"
Bundle "groenewege/vim-less"
Bundle "evanmiller/nginx-vim-syntax"
Bundle "Lokaltog/vim-easymotion"
Bundle "tomasr/molokai"
Bundle "klen/python-mode"

Open vim, run :BundleInstallor run directly in the shellvim +BundleInstall +qall


Install Bundle.png

After installing the plugin, there may be another problem: the vim version is not high enough


Vim version is not high enough.png

The vim version can be updated with the following command

brew install macvim --override-system-vim

Then run the following command to symlink to/Application

brew linkapps macvim

Finally .zshrcuse an alias in the config file to use the updated vim

#setup macvim alias
alias vim='/usr/local/opt/macvim/MacVim.app/Contents/MacOS/Vim'

Common plugins

NERD Tree

NERD Tree is a tree-shaped directory plug-in, which is convenient for browsing the directories and files in the current directory.


NERD Tree Plugin Bundle.png


I configure the NERD Tree in the ~/.vimrc file, setting a keymap that enables or disables the NERD Tree

nmap <F5> :NERDTreeToggle<cr>

NERD Tree Configuration.png


So you can enable or disable NERD Tree just by pressing the F5 key , NERD Tree provides some common shortcut keys to manipulate directories:

  • Move the cursor through hjkl
  • oOpen and close a file or directory, if you want to open a file, you must move the cursor to the file name
  • tOpen in tab
  • s and i can split the window horizontally or vertically to open the file
  • p to the upper directory
  • P to the root directory
  • K to the first node in the same directory
  • P to the last node in the same directory
YouCompleteMe & syntastic

YouCompleteMe is a fast vim code completion engine that supports fuzzy matching. Since it provides code hints for C/C++/Objective-C based on the Clang engine, it also supports code hinting engines in other languages, such as Jedi -based Python code completion, OmniSharp - based C# code completion, and Gocode - based Go code completion Complete.


YouCompleteMe.gif


Just type the code, it will automatically prompt the list of codes you want to enter, you can select one of them, and then the tab key can complete the code.

YouCompleteMe has integrated Syntastic , so once you write code with a syntax error, there will be a red error prompt


syntastic.png
ctrlp

I don’t know if you have encountered such a situation: in large-scale engineering projects, directories and files are deeply nested. To open a file, you need to enter the directory one by one to open it. In this case, it is time-consuming and inefficient. ctrlp redefines the way to hit directories and files, especially for browsing large-scale project files.

enable ctrlp

  • Run command :CtrlPor :CtrlP [starting-directory]to enable ctrlp in find file mode
  • Run the command :CtrlPBufferor :CtrlPMRUto enable ctrlp to find buffered or recently opened files mode
  • Run commands CtrlPMixedto find files, find buffers and recently opened files mixed mode to start ctrlp

basic use

  • Press <c-f>and <c-b>to toggle between the three search modes
  • Press <c-y>to create a new file and corresponding parent directory
  • Press <c-d>to switch to looking only for filenames instead of full paths
  • Press <c-j>, <c-k>or the arrow keys to move the search result list
  • Press <c-t>or <c-v>to <c-x>open the file in a new tab or split window
  • Press <c-z>to mark or unmark the file, then press <c-o>to open the file
  • Press <c-n>to <c-p>select the next/previous string in the prompt history

The demo video shows
how to use ctrlp, please refer to the demo video of happypetterd , the explanation is very clear.

Vim Powerline

Vim Powerline is a plug-in that displays vim status bar, it can display vim mode, operating environment, encoding format, number of lines/columns and other information


Vim Powerline.png
Molokai

Molokai is the vim color theme, the effect is as follows


Molokai Color Scheme for Vim.png

Common commands

For the basic commands of entry vim, you can refer to the concise Vim leveling guide . The following are my common commands such as moving the cursor , inserting/modifying , deleting , copying , pasting , undoing and restoring .

  • move the cursor

    1. For moving within a line , use f/F + 字符to move to a specific character, and then use .to repeat the command; fto move forward, and to move Fbackward. If you want to move directly to the beginning or end of a line, use ^or$
    2. For moving in multiple lines , there are multiple options: the first one is to move by ggspecifying the number of lines, which means to Gmove the first line of the file, which means to move the last line of the file, which means to move to a specific line. The second is to move through regular search , which means forward search, means reverse search, find the next matching result, means the previous matching result, and press to browse the search history. The third is to use the marker to move, the marker position (for a single file, if it is multiple files, use capital letters ), move to the column of the marker position, move to the beginning of the line at the marker position, and some special markers, Indicates the position of the cursor before the jump行数 + GggG行数 + G/string?stringnNup/downm + {a-z}{A-Z}`{mark}'{mark}'
  • select text
    virregular select select
    Vby row select
    Ctrl + Vby column

  • Insert/Modify
    ibefore the current character Insert
    Iat the beginning of the line Insert
    aafter the current character Insert
    Aat the end of the line Insert at
    othe next line of
    Othe current line Insert at the previous line of the current line

    rChange current character
    RChange multiple characters
    cw/cawChange word
    cf + 字符Change from current character to specified character
    c$Change from current character to end of line
    ccChange entire line

  • Delete
    xdelete character
    df + 字符delete from the current character to the specified character
    dw/dawdelete word
    d$delete from the current cursor to the end of the line
    dddelete a line

  • Cut and paste
    dd + pthe delete line, then place the delete line below the current cursor , then place the delete word
    dd + Pabove the current cursor , then place the delete word after the current cursor, then place the delete word before the current cursor Acceptable count prefix, repeat paste
    dw + p
    dw + P
    p/P

  • Copy
    ywCopy word
    yfCopy from the current character to the specified character
    y$Copy the current cursor to the end of the line
    yyCopy the entire line

  • Undo and Redo
    uUndo
    ctrl + rRedo

  • Repeat action
    数字+actionindicates how many times an action is performed to
    .repeat the previous action

  • Macro recording
    q + 寄存器(a-z)start recording
    录制动作
    qstop recording
    @ + 寄存器 / @@replay recorded macro



Text/Sam_Lau (author of Jianshu)
original link: http://www.jianshu.com/p/a0b452f8f720
The copyright belongs to the author, please contact the author for authorization, and mark "author of Jianshu".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326470254&siteId=291194637