Skip to content

Vim安装与配置

Published: at 07:11 PM

Vi 是Unix时代的文字编辑利器,与之齐名的大概就只有 Emacs。这两款软件都可谓计算机行业的活化石(Vi首次发布于1976年,Emacs也差不多在同一时期作为宏指令集运行于 TECO编辑器之上)。如果你留意的话,国外一些须发花白的老人,依然津津乐道的对着30多英寸的4K显示器,敲击着“J”已经被磨损得差不多的键盘,在这两款上古编辑器里堆砌着算法的精妙,又或书写着人生的悲欢。

虽然都有各自的忠实用户,但坦白的说,他们的学习门槛都非常高,高到你需要认真的在严肃使用他们之前,思考一件事:你打算花1年时间学习和熟悉手上的编辑器,然后提高文字生产效率,以节省余生做更多有意义的事?还是打算好好珍惜这1年时间做点有意义的事?

Table of contents

Open Table of contents

Vim

1991年,Vi的改进版 Vim 首次发布,与两位前辈一样,时至今日依然保持更新,并拥有广泛的用户基础,以及同样广泛的笑话…

Q: How to generate a random sequence???

A: leave a newbie programmer to vim and ask him to exit it and track all the keystrokes by him!


既然被称为Vi IMproved,到底改进了什么呢?

Some of Vim’s enhancements include completion functions, comparison and merging of files (known as vimdiff), a comprehensive integrated help system, extended regular expressions, scripting languages (both native and through alternative scripting interpreters such as Perl, Python, Ruby, Tcl, etc.) including support for plugins, a graphical user interface (gvim), limited integrated development environment-like features, mouse interaction (both with and without the GUI), folding, editing of compressed or archived files in gzip, bzip2, zip, and tar format and files over network protocols such as SSH, FTP, and HTTP, session state preservation, spell checking, split (horizontal and vertical) and tabbed windows, Unicode and other multi-language support, syntax highlighting, trans-session command, search and cursor position histories, multiple level and branching undo/redo history which can persist across editing sessions, and visual mode.

While running, Vim saves the user’s changes in a swap file with the “.swp” extension. This file can be used to recover after a crash. If a user tries to open a file and a swap file already exists, Vim will warn the user, and if the user proceeds, Vim will use a swap file with the extension “.swo” (or, if there is already more than one swap file, “.swn”, “.swm”, etc.).[44][45] The feature can be disabled.[46]

对我来说,插件支持,语法高亮,无限undo是我目前使用率最高的改进。

why not …

因为大部分IDE太,处理同样的工作,你需要更高配置的电脑。

因为这些应用程序太,处理同样的工作,你需要更高配置的电脑。同时,输出的文件不是纯文本,无法或需要额外工作才能被第三方程序使用。

Markdown最应该替代的是Pages或者Word。另外,你完全可以在Vim里使用Markdown语法来书写文章。

安装Vim

我使用Mac,所以安装命令大部分与Mac相关,如果是Linux或Windows的用户,请自行搜索安装方式。Mac用户如果还没有安装软件包管理器Homebrew的,请转去官网先安装homebrew,或者直接google Vim的官网,自行下载对应操作系统的软件包。

brew install vim

我目前使用的版本是9.0.1544,如下:

Vim v9.0.1544

Vim是可以高度定制的软件。如果是通过brew安装的,其配置信息通常保存在 ~/.vimrc,可以通过下面的命令编辑Vim的配置文件 .vimrc

vim ~/.vimrc

配置Vim

我目前的配置文件内容如下:

" Disable compatibility with vi which can cause unexpected issues.
set nocompatible

" Enable type file detection. Vim will be able to try to detect the type of file in use.
filetype on

" Enable plugins and load plugin for the detected file type.
filetype plugin on

" Load an indent file for the detected file type.
filetype indent on

" Turn syntax highlighting on.
syntax on

" Add numbers to each line on the left-hand side.
set number

" Highlight cursor line underneath the cursor horizontally.
set cursorline

" Highlight cursor line underneath the cursor vertically.
" set cursorcolumn

" Set shift width to 4 spaces.
set shiftwidth=4

" Set tab width to 4 columns.
set tabstop=4

" Use space characters instead of tabs.
set expandtab

" Do not save backup files.
set nobackup

" Do not let cursor scroll below or above N number of lines when scrolling.
set scrolloff=10

" Do not wrap lines. Allow long lines to extend as far as the line goes.
set nowrap

" While searching though a file incrementally highlight matching characters as you type.
set incsearch

" Ignore capital letters during search.
set ignorecase

" Override the ignorecase option if searching for capital letters.
" This will allow you to search specifically for capital letters.
set smartcase

" Show partial command you type in the last line of the screen.
set showcmd

" Show the mode you are on the last line.
set showmode

" Show matching words during a search.
set showmatch

" Use highlighting when doing a search.
set hlsearch

" Set the commands to save in history default number is 20.
set history=1000

" Enable auto completion menu after pressing TAB.
set wildmenu

" Make wildmenu behave like similar to Bash completion.
set wildmode=list:longest

" There are certain files that we would never want to edit with Vim.
" Wildmenu will ignore files with these extensions.
set wildignore=*.docx,*.jpg,*.png,*.gif,*.pdf,*.pyc,*.exe,*.flv,*.img,*.xlsx

set termguicolors

set laststatus=2

let g:lightline = {
    \ 'colorscheme': 'nord',
    \ 'active': {
    \    'left':  [ [ 'mode', 'paste' ],
    \               [ 'gitbranch', 'readonly', 'filename', 'modified' ] ],
    \    'right': [ [ 'lineinfo' ],
    \               [ 'percent' ],
    \               [ 'fileformat', 'fileencoding', 'filetype', 'charvaluehex' ] ]
    \ },
    \ 'component': {
    \    'charvaluehex': '0x%B'
    \ },
    \ 'component_function': {
    \    'gitbranch': 'gitbranch#name'
    \ },
    \ }

colorscheme nord

" to get rid of double ShowMode - one already comes from lightline.vim
set noshowmode

" PLUGINS ---------------------------------------------------------------- {{{

" Plugin code goes here.

call plug#begin('~/.vim/plugged')

    Plug 'arcticicestudio/nord-vim'
    
    Plug 'dense-analysis/ale'
    
    Plug 'preservim/nerdtree'
    
    Plug 'itchyny/lightline.vim'
    
    Plug 'itchyny/vim-gitbranch'

call plug#end()

" }}}

" MAPPINGS --------------------------------------------------------------- {{{

" Mappings code goes here.
" Set the backslash as the leader key.
let mapleader = "\\"

" Press \\ to jump back to the last cursor position.
nnoremap <leader>\ ``

" Press \p to print the current file to the default printer from a Linux operating system.
" View available printers:   lpstat -v
" Set default printer:       lpoptions -d <printer_name>
" <silent> means do not display output.
nnoremap <silent> <leader>p :%w !lp<CR>

" Type jj to exit insert mode quickly.
inoremap jj <Esc>

" Press the space bar to type the : character in command mode.
nnoremap <space> :

" Pressing the letter o will open a new line below the current one.
" Exit insert mode after creating a new line above or below the current line.
nnoremap o o<esc>
nnoremap O O<esc>

" Center the cursor vertically when moving to the next word during a search.
nnoremap n nzz
nnoremap N Nzz

" Yank from cursor to the end of line.
nnoremap Y y$

" Map the F5 key to run a Python script inside Vim.
" I map F5 to a chain of commands here.
" :w saves the file.
" <CR> (carriage return) is like pressing the enter key.
" !clear runs the external clear screen command.
" !python3 % executes the current file with Python.
nnoremap <f5> :w <CR>:!clear <CR>:!python3 % <CR>

" You can split the window in Vim by typing :split or :vsplit.
" Navigate the split view easier by pressing CTRL+j, CTRL+k, CTRL+h, or CTRL+l.
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-h> <c-w>h
nnoremap <c-l> <c-w>l

" Resize split windows using arrow keys by pressing:
" CTRL+UP, CTRL+DOWN, CTRL+LEFT, or CTRL+RIGHT.
nnoremap <c-up> <c-w>+
nnoremap <c-down> <c-w>-
nnoremap <c-left> <c-w>>
nnoremap <c-right> <c-w><

" NERDTree specific mappings.
" Map the F3 key to toggle NERDTree open and close.
nnoremap <F3> :NERDTreeToggle<cr>

" Have nerdtree ignore certain files and directories.
let NERDTreeIgnore=['\.git$', '\.jpg$', '\.mp4$', '\.ogg$', '\.iso$', '\.pdf$', '\.pyc$', '\.odt$', '\.png$', '\.gif$', '\.db$']

" }}}

在配置过程中,可以参考以下这篇文章,写得非常详尽,解释得也很清晰。

Vimrc Configuration Guide - How to Customize Your Vim Code Editor with Mappings, Vimscript, Status Line, and More

配置完 .vimrc 后,如果你安装了ALE,pylint和flake8,那么就可以开始愉快的在Vim里写屎山简洁的Python了…