Vim tossing

Time:2022-8-14

Original address:http://www.ahonn.me/2017/02/1…

Yesterday, I saw Chi Jianqiang's "MacTalk Life Metaprogramming" on WeChat Reading, and then spent almost a day reading it. One of the articles about Vim brought up my interest in this editor again (yes, I Interested in it many times, lazy every time..).

I've been too lazy to configure it slowly, and I don't want to use someone else's configuration. The editor I use still wants to toss it myself, and then I hurriedly tossed it today while I still have a little enthusiasm for this.

MacVim

In fact, writing code directly on the command line is a bit unaccustomed, so I chose the GUI version of MacVim. In this way, it can be opened separately like other editors, and when you want to make small changes to the code, you can directly open the command line.

In the MacTalk article, there is a mention of the Chinese version of Vimhelp documentation, then download it. Sure enough, the Chinese is relatively easy to understand.

seen others before.vimrcConfiguration, there are some basic configurations of editors, and I don't know where to find them. It turns out that these configurations can be entered in the editor:help optionsto view.

indentation

Regarding the configuration of indentation, the main settings areautoindent, smartindentandexpandtabthese three options.

  • autoindentoption determines the indentation of new lines based on the previous line

  • smartindentOption smart auto-indent

  • expandtaboption convert tabs to spaces

set autoindent
set smartindent
set expandtab

line number

Displaying line numbers is a hard requirement, but unlike other editors, there are also relative line numbers on vim.

set number
set relativenumber

plugin

Obviously, installing vim plugins manually is a hassle and hassle. Similar to sublime text and the like, vim also has a similar plugin managerVundle

Create folder~/.vimand config file~/.vimrc, and install Vundle:

$ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

Create a new one for easier management.vimrc.bundlesTo store the list of plugins separately:

set nocompatible    " be iMproved, required
filetype off        " requried
set rtp+=~/.vim/bundle/vundle/

call vundle#begin()

" Let Vundle manage Vundle
Bundle 'gmarik/vundle'

" .... other plugins

call vundle#end()
" required
filetype plugin indent on

When adding plugins in the future, you only need to add a line to this file:

Bundle 'xxx'

Install plugin input:BundleInstallcommand, delete the corresponding configuration when uninstalling the plugin and run:BundleCleanRemove the plugin.

parenthesis completion

When using it for the first time, enter(I didn't even pop out another one by myself.). This is not scientific, it is necessary to be reasonable.

found that many people usedelimitMate,Add toBundle 'delimitMate'arrive.vimrc.bundles,runBundleInstall

After the installation is complete, the brackets can be automatically completed.

Emmet

Many browsers support the Emmet plugin, and so does vim. But this Emmet is a third party, calledEmmet.vim

After installation, it is mainly to set the shortcut key for departure. The default shortcut key isCtrl + y
I changed the shortcut key toCtrl + erelatively easy.

" Emmet.vim
let g:user_emmet_expandabbr_key = '<c-e>'

directory tree

The directory tree is basically just usingNERDTree

The shortcut key to open the directory tree is changed toCtrl + b, and then automatically exit when vim is left with only the directory tree displayed.

" NERDTree 
map <c-b> :NERDTreeToggle<CR>
autocmd vimenter * NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") &&b:NERDTreeType == "primary") | q | endif

Completion

Complementation is well-knownYouCompleteMe.

There are many plug-ins on the Internet about this plug-in, so I won't dwell on it. Here is mainly the configuration of Baidu EFE copied:

" YouCompleteMe
" http://efe.baidu.com/blog/vim-javascript-completion/#youcompleteme
let g:ycm_min_num_of_chars_for_completion = 3 
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_complete_in_comments = 1
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
" Use tab
function! TabFunction ()
    let line = getline('.')
    let substr = strpart(line, -1, col('.')+1)
    let substr = matchstr(substr, "[^ \t]*$")
    if strlen(substr) == 0
        return "\<tab>"
    endif
    return pumvisible() ? "\<c-n>" : "\<c-x>\<c-o>"
endfunction
inoremap <tab> <c-r>=TabFunction()<cr>

let g:ycm_semantic_triggers = {
    \   'css': [ 're!^\s{2}', 're!:\s+'],
    \   'html': [ '</' ],
    \ }

Summarize

After tossing for an afternoon, it was finally done. In fact, the process is quite fun, and if there is a chance, I want to write a plug-in to play.

After that, I abandoned VS Code and started the journey of Vim to experience the feeling of high efficiency without leaving my hands on the keyboard.