打造 vim(一)

Ubuntu 版本为 20.04

一、安装 space-vim

  下载 vim:sudo apt-get install vim

  下载 space-vim:

    cd 

    git clone https://github.com/liuchengxu/space-vim.git

    cd .space-vim

    ./install.py --vim

    等待自动安装完成,不需要做任何操作

二、YouCompleteMe 自动补全安装

space-vim 安装 YouCompleteMe 不是很好,所以首先要提前安装好相应的环境,再去设置 space-vim。

2.1 环境安装

2.1.1 基本环境安装

     安装环境:

  sudo apt-get install build-essential cmake

  sudo apt-get install python-dev python3-dev

  虽然安装了 python2.7,但是当前版本建议用 python3,可以切换版本:

  当前系统中安装了3个版本的 python(绿色的,蓝色的软链接),使用 update-alternatives 命令安装版本:

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
update-alternatives --install /usr/bin/python python /usr/bin/python3.6m 3

  添加之后,切换版本:

1 update-alternatives --config python

   

2.1.2 llvm 安装

llvm 官方 apt 网站:https://apt.llvm.org/

先执行以下命令:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -

修改 sources.list:

sudo vim /etc/apt/sources.list

 1 Focal (20.04) - Last update : Sun, 24 May 2020 18:52:03 UTC / Revision: 20200524052636+1e7865d9464
 2 # i386 not available
 3 deb http://apt.llvm.org/focal/ llvm-toolchain-focal main
 4 deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main
 5 # 9
 6 deb http://apt.llvm.org/focal/ llvm-toolchain-focal-9 main
 7 deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-9 main
 8 # 10
 9 deb http://apt.llvm.org/focal/ llvm-toolchain-focal-10 main
10 deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-10 main

 执行 sudo apt-get update,然后执行安装:

sudo apt-get install clang-format clang-tidy clang-tools clang clangd libc++-dev libc++1 libc++abi-dev libc++abi1 libclang-dev libclang1 liblldb-11-dev libllvm-ocaml-dev libomp-dev libomp5 lld lldb llvm-dev llvm-runtime llvm python-clang

 2.1.3 安装 Momo(不需要可以不安装)

安装步骤参照:https://www.mono-project.com/download/stable/#download-lin

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update
sudo apt install mono-devel

2.1.4 安装 go(不需要可不安装)

下载地址:https://golang.google.cn/dl/

下载版本:go1.14.3.linux-amd64.tar.gz

安装参考:https://golang.google.cn/doc/install?download=go1.14.3.linux-amd64.tar.gz

我下载的地方在 ~/Download 中,cd ~/Download

执行安装命令:

1 sudo tar -C /usr/local -xzf go1.14.3.linux-amd64.tar.gz

添加环境变量,vim .~/.profile:

export PATH=$PATH:/usr/local/go/bin

  

 执行命令:

source $HOME/.profile

测试一下,写下代码如下,hello.go:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

编译:

go build hello.go

设置 GOPROXY 的环境变量,不然 go 补全安装的时候,会去被墙的网站下载,我们使用国内站点进行下载:

$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile

 2.1.5 安装 Node(不需要可不安装)

 下载地址:https://nodejs.org/dist/v12.16.3/

wget https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.gz 

安装指导:https://github.com/nodejs/help/wiki/Installation

sudo mkdir -p /usr/local/lib/nodejs
sudo tar -zxvf node-v12.16.3-linux-x64.tar.gz -C /usr/local/lib/nodejs 

修改环境变量: vim ~/.profile

#nodejs
VERSION=v12.16.3
DISTRO=linux-x64
export PATH=/usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin:$PATH

 刷新环境变量:

. ~/.profile

测试:

 将 node、npm 和 npx 软链接到 /usr/bin:

sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npm /usr/bin/npm
sudo ln -s /usr/local/lib/nodejs/node-$VERSION-$DISTRO/bin/npx /usr/bin/npx

npm 更新:sudo npm install npm@latest -g

2.1.6 安装 TypeScript

sudo npm install -g typescript

2.2 安装 YouCompleteMe

2.2.1 下载插件

进入  plug 目录:cd ~/.vim/plugged

1 git clone https://github.com/ycm-core/YouCompleteMe.git

git clone 此插件,然后在此插件的根目录下运行如下命令:git submodule update --init --recursive

2.2.2 执行安装

进入 YouCompleteMe 的目录,执行安装:

cd ~/.vim/plugged/YouCompleteMe
./install.py --all

2.2.3 

  执行命令 echo | clang -v -E -x c++ - 可以查看到自己需要添加的库:  

  

  将目录添加进 flags 数组的末尾,按照里面的 -isystem + 头文件目录格式填写即可。同事要注释掉下面的内容:  

  

三、添加插件

3.1 space-vim 中已经包含的插件

space-vim 里面 layers 包含了很多插件,我们只需要在 ~/.spacevim 中添加相应的 layers 即可将插件包含进来,并进行安装。

可配置的插件:https://github.com/liuchengxu/space-vim/blob/master/layers/LAYERS.md

vim ~/.spacevim

 1 " Comment the following line if you don't want Vim and NeoVim to share the
 2 " same plugin download directory.
 3 let g:spacevim_plug_home = '~/.vim/plugged'
 4 
 5 " Uncomment the following line to override the leader key. The default value is space key "<\Space>".
 6 " let g:spacevim_leader = "<\Space>"
 7 
 8 " Uncomment the following line to override the local leader key. The default value is comma ','.
 9 " let g:spacevim_localleader = ','
10 
11 " Enable the existing layers in space-vim.
12 " Refer to https://github.com/liuchengxu/space-vim/blob/master/layers/LAYERS.md for all available layers.
13 let g:spacevim_layers = [
14       \ 'syntax-checking',
15       \ 'better-defaults', 'spacevim',
16       \ 'emoji', 'games', 'goyo',
17       \ 'c-c++', 'haskell', 'html', 'java', 'javascript',
18       \ 'markdown', 'python', 'rust', 'scala', 'typescript',
19       \ 'chinese',
20       \ 'code-snippets', 'editing', 'formatting', 'programming',
21       \ 'airline',
22       \ 'cscope', 'ctags', 'docker', 'file-manager', 'fzf', 'lsp', 'tmux', 'ycmd',
23       \ 'git', 'github',
24       \ 'better-motion', 'ctrlp', 'text-align', 'which-key',
25       \ ]
26 
27 " Uncomment the following line if your terminal(-emulator) supports true colors.
28 let g:spacevim_enable_true_color = 1
29 
30 " Uncomment the following if you have some nerd font installed.
31 let g:spacevim_nerd_fonts = 1
32 
33 " If you want to have more control over the layer, try using Layer command.
34 if g:spacevim.gui
35   Layer 'airline'
36 endif
37 
38 " Manage your own plugins.
39 " Refer to https://github.com/junegunn/vim-plug for more detials.
40 function! UserInit()
41 
42   " Add your own plugin via Plug command.
43   Plug 'junegunn/seoul256.vim'
44 
45 endfunction
46 
47 " Override the default settings from space-vim as well as adding extras
48 function! UserConfig()
49 
50   " Override the default settings.
51   " Uncomment the following line to disable relative number.
52   " set norelativenumber
53 
54   " Adding extras.
55   " Uncomment the following line If you have installed the powerline fonts.
56   " It is good for airline layer.
57   let g:airline_powerline_fonts = 1
58   let g:spacevim_lsp_engine = 'vim_lsp'
59 
60 endfunction

修改完成后,随便用 vim 打开一个文件,会弹出一个对话框,选择 Y ,就开始自动进行下载安装。

安装完成后,还需要安装一个字体库:sudo apt-get install powerline

这个字体库是提供给 airline 使用。

3.2 添加非包含插件

 

四、vim 自带的插件

4.1 terminal 插件

  terminal 插件是用于在 vim 中开一个 terminal 窗口,供操作 bash 命令。可以在 vim  中使用 :terminal 命令打开此窗口。

  不过有开源的辅助插件可以使用。

  在.vimrc 中的 vundle 结构中添加:

 1 Plugin 'PangPangPangPangPang/vim-terminal'
 2 map <silent> <F12> :VSTerminalToggle<cr>
 3 if has('nvim')
 4     tnoremap <F12> <C-\><C-n> :VSTerminalToggle<cr>
 5     tnoremap <C-w> <C-\><C-n><C-w>
 6 else
 7     tmap <silent> <F12> <c-w>:VSTerminalToggle<cr>
 8 endif
 9 let g:vs_terminal_custom_height = 50
10 let g:vs_terminal_custom_pos = 'bottom'

  运行 PluginInstall 安装插件。

  效果:

  

  要退出终端,选择终端,在终端执行 exit,然后 :q 即可

4.2 termdebug 插件

  termdebug 是 vim 8.0 以后提供的自带的新插件,可以在 vim 窗口实现以前 vimgdb 的调试功能,相比较 vimgdb ,感觉功能更强大。

  此插件必须要安装 terminal,在 vim 窗口运行:echo has('terminal') ,如果返回值是 1 ,则当前系统中有 terminal。

  termdebug 插件全称即是 terminal debug Plugin,用于用 gdb 调试一个程序并且可在 vim 窗口中观察。

4.2.1 termdebug 插件启动

  要想启动此插件,首先在 vim 中运行命令:packadd termdebug

  对自己编译出来的二进制文件,注意 vim 打开的程序的源码目录必须是根目录,如果二进制文件为 hello ,运行:Termdebug hello

  此时会打开另外两个窗口:

    • gdb 窗口:gdb 窗口提供 gdb 的原生操作
    • 程序窗口:程序窗口供被调试的程序使用
    • 源码窗口:提供源码交互

 4.2.2 调试程序

  我们既可以在 GDB 窗口中调试,也可以在源码窗口中调试。

  • GDB 窗口调试常用指令:
    • file bin 加载名为 bin 的二进制文件
    • CTRL-C 中断程序
    • run/r 运行
    • next/n 执行当前行,停在下一行 (step over)
    • step/s 执行当前行,进入下一层函数 (step in)
    • finish 执行直至离开当前函数
    • where 显示栈
    • continue/c 继续执行
    • break/b N 在第 N 行加断点
    • break/b f 在函数 f 处加断点
    • delete 删除所有断点

  更多的命令就需要查阅官方文档了

  • 在程序窗口调试:
    • :Run [args] 运行程序,可带参数 [args],或沿用上一次运行的参数
    • :Arguments {args} 设置下一次运行所用参数
    • :Break 在当前行加断点
    • :Clear 删除当前行的一个断点
    • :Step = gdb "step"
    • :Over = gdb "next"
    • :Finish = gdb "finish"
    • :Continue = gdb "continue"
    • :Stop 中断程序
posted @ 2017-04-11 23:57  游戏进行中  阅读(847)  评论(0编辑  收藏  举报