完美的Vim学习体验:VimReference

Photo by Pixabay from Pexels

疫情期间在家办公啦,说实话对于软件编码这类工作没有多少影响,下班之后直接切换到娱乐或学习模式,很是方便。这样也有时间整理一下之前的一些想法和未完成的小计划。

这篇文章简单的记录了一些重新学习Vim的过程,虽然只是一个文本编辑器,但是因为日常使用频率极高,也是觉得可以提升编码体验的事情。

1. Vim编辑器(Vi IMproved)

在我个人日常的工作和学习中,Linux系统内80%以上文本编辑的工作使用Vim完成。

但是对于Vim的使用学习从大学一本Linux教科书开始,到后来购买了鸟哥的Linux私房菜系列书籍,一直都处于基本使用的状态,偶尔使用一些高级的命令和设置也是有在需要的时候寻找网络资料简单的学习一下和使用。一是书籍中的Linux使用介绍完全足够日常学习与开发,二是Vim的官方文档风格很简略,如下:

虽然个人学习技术都有优先看官方文档的习惯,但是第一次接触到Vim的官方文档时确实被这个极简的风格劝退了。直到前段时间在技术社区内,偶尔看到别人的截图中的Vim,居然有各种功能强大的插件。这才让我提起了兴趣,打算认真的把Vim这个使用率极高的编辑器好好的研究一下。

首先在Vim官方提供的下载链接中下载Vim的中文文档和PDF资料:https://sourceforge.net/projects/vimcdoc/

文档本地化作者的Github仓库地址:GitHub - yianwillis/vimcdoc: Vim 中文文档计划

注意这个插件需要安装对应Vim版本的,例如本地Vim是7.4版本,则对应Vimcdoc1.9;而Vim8.2则对应Vimcdoc2.3

因为默认安装Vim的help文档是英文的,有中文当然更好理解,将文件解压,进入目录后执行./vimcdoc.sh -i即可将中文帮助文档安装到Vim当中

2. Vimtutor和帮助文档

当我开始在Linux下仔细阅读这篇帮助文档时,我感觉之前和在之前网页上看到的文档完全不是一个东西,似乎让我回到了刚学Linux的时候。尤其是vimtutor这个教程,简直是学习Vim的绝佳练习文章,再把工作区调整一下,体验极佳:

两个窗口其实都是vim打开的,左边是vimtutor,只要正常安装了vim就可以直接打开。右边直接打开vim 的help窗口,并将help 窗口之外的窗口关闭即可。

现在跟着Vimtutor从零开始学习Vim,对于基本指令已经非常熟练的程序员来说,十分钟内就可以结束了。之后开始通篇阅读右边的帮助文档,文档支持tag跳转,类似于网页上的超链接( CTRL-]跳转链接,CTRL-O回退,注意回退是英文字母O,不是数字0)。

3. 自定义Vim

轻松的文档阅读与学习之后,开始DIV自己的Vim吧,这里只是尝试了几个比较常用的插件。如果不知道怎么安装插件,把现有的资料利用起来,从文档里查找方法。想学习知识与其从网上搜索各种碎片化的信息,不如仔细阅读文档和来自官方的Blog。

首先需要初始化一下自己的Vimrc文件,在Vim官网上的搜索框里可以找到一份示例的Vimrc配置:

" URL: https://vim.wikia.com/wiki/Example_vimrc
" Authors: https://vim.wikia.com/wiki/Vim_on_Libera_Chat
" Description: A minimal, but feature rich, example .vimrc. If you are a
"              newbie, basing your first .vimrc on this file is a good choice.
"              If you're a more advanced user, building your own .vimrc based
"              on this file is still a good idea.

"------------------------------------------------------------
" Features {{{1
"
" These options and commands enable some very useful features in Vim, that
" no user should have to live without.

" Set 'nocompatible' to ward off unexpected things that your distro might
" have made, as well as sanely reset options when re-sourcing .vimrc
set nocompatible

" Attempt to determine the type of a file based on its name and possibly its
" contents. Use this to allow intelligent auto-indenting for each filetype,
" and for plugins that are filetype specific.
if has('filetype')
  filetype indent plugin on
endif

" Enable syntax highlighting
if has('syntax')
  syntax on
endif

"------------------------------------------------------------
" Must have options {{{1
"
" These are highly recommended options.

" Vim with default settings does not allow easy switching between multiple files
" in the same editor window. Users can use multiple split windows or multiple
" tab pages to edit multiple files, but it is still best to enable an option to
" allow easier switching between files.
"
" One such option is the 'hidden' option, which allows you to re-use the same
" window and switch from an unsaved buffer without saving it first. Also allows
" you to keep an undo history for multiple files when re-using the same window
" in this way. Note that using persistent undo also lets you undo in multiple
" files even in the same window, but is less efficient and is actually designed
" for keeping undo history after closing Vim entirely. Vim will complain if you
" try to quit without saving, and swap files will keep you safe if your computer
" crashes.
set hidden

" Note that not everyone likes working this way (with the hidden option).
" Alternatives include using tabs or split windows instead of re-using the same
" window as mentioned above, and/or either of the following options:
" set confirm
" set autowriteall

" Better command-line completion
set wildmenu

" Show partial commands in the last line of the screen
set showcmd

" Highlight searches (use <C-L> to temporarily turn off highlighting; see the
" mapping of <C-L> below)
set hlsearch

" Modelines have historically been a source of security vulnerabilities. As
" such, it may be a good idea to disable them and use the securemodelines
" script, <http://www.vim.org/scripts/script.php?script_id=1876>.
" set nomodeline


"------------------------------------------------------------
" Usability options {{{1
"
" These are options that users frequently set in their .vimrc. Some of them
" change Vim's behaviour in ways which deviate from the true Vi way, but
" which are considered to add usability. Which, if any, of these options to
" use is very much a personal preference, but they are harmless.

" Use case insensitive search, except when using capital letters
set ignorecase
set smartcase

" Allow backspacing over autoindent, line breaks and start of insert action
set backspace=indent,eol,start

" When opening a new line and no filetype-specific indenting is enabled, keep
" the same indent as the line you're currently on. Useful for READMEs, etc.
set autoindent

" Stop certain movements from always going to the first character of a line.
" While this behaviour deviates from that of Vi, it does what most users
" coming from other editors would expect.
set nostartofline

" Display the cursor position on the last line of the screen or in the status
" line of a window
set ruler

" Always display the status line, even if only one window is displayed
set laststatus=2

" Instead of failing a command because of unsaved changes, instead raise a
" dialogue asking if you wish to save changed files.
set confirm

" Use visual bell instead of beeping when doing something wrong
set visualbell

" And reset the terminal code for the visual bell. If visualbell is set, and
" this line is also included, vim will neither flash nor beep. If visualbell
" is unset, this does nothing.
set t_vb=

" Enable use of the mouse for all modes
if has('mouse')
  set mouse=a
endif

" Set the command window height to 2 lines, to avoid many cases of having to
" "press <Enter> to continue"
set cmdheight=2

" Display line numbers on the left
set number

" Quickly time out on keycodes, but never time out on mappings
set notimeout ttimeout ttimeoutlen=200

" Use <F11> to toggle between 'paste' and 'nopaste'
set pastetoggle=<F11>


"------------------------------------------------------------
" Indentation options {{{1
"
" Indentation settings according to personal preference.

" Indentation settings for using 4 spaces instead of tabs.
" Do not change 'tabstop' from its default value of 8 with this setup.
set shiftwidth=4
set softtabstop=4
set expandtab

" Indentation settings for using hard tabs for indent. Display tabs as
" four characters wide.
"set shiftwidth=4
"set tabstop=4


"------------------------------------------------------------
" Mappings {{{1
"
" Useful mappings

" Map Y to act like D and C, i.e. to yank until EOL, rather than act as yy,
" which is the default
map Y y$

" Map <C-L> (redraw screen) to also turn off search highlighting until the
" next search
nnoremap <C-L> :nohl<CR><C-L>

"------------------------------------------------------------

就以这份示例的Vimrc为起点,将其写入~/.vimrc文件中即可。另外,由于日常工作和学习中使用的CentOS 7 ,默认安装的Vim就是Vim7.4,我并不打算升级到Vim8。

所以需要有一个Vim插件管理器,这里选择Vim-plug:https://github.com/junegunn/vim-plug。

[landscape@localhost vim_plugin]$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs 
>     https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 82679  100 82679    0     0  26332      0  0:00:03  0:00:03 --:--:-- 26339

之后安装一下nerdtree这个目录插件。https://github.com/preservim/nerdtree

虽然官方文档里说明向vimrc文件中写入:

call plug#begin()
  Plug 'preservim/nerdtree'
call plug#end()

但是因为网络原因访问源仓库非常慢,所以这里可以把它换成Gitee镜像:

call plug#begin()
  Plug 'https://gitee.com/mirrors/nerdtree'
call plug#end()

然后进入vim执行:PlugInstall,nerdtree插件就安装好了

之后又安装了几个看上去有用的插件,如Java文件支持、自动对齐等,不做重复记录。

4. 小结

Vim 几乎毫无争议是 Linux 中最受欢迎的命令行编辑器之一。除了是一个多功能编辑器外,很多开发者将 Vim 当做 IDE来使用。而这背后Vim完善全面的文档以及强大的插件扩展能力功不可没,就如同Vim文档页面的第一句话说的:

The most useful software is sometimes rendered useless by poor or altogether missing documentation.

最有用的软件有时会因为文档的贫乏或完全缺失而变得无用。

这是一条签名的小尾巴: 任何变化都不是突然发生的,都是自己无意间一点一点选择的。
原文地址:https://www.cnblogs.com/novwind/p/15084493.html