跳转至

十分钟Spacevim入门#

来源
* github项目
* English version

这篇文章的主要目的是让用户快速了解Spacevim,尝试Spacevim并不会删除你的原有vim
配置,如果你不喜欢Spacevim,你随时可以安全快速地切换回原来的配置。

只需要10分钟,来尝试一下吧。

目录#

一、安装#

1. Install spacevim (Linux & macOS, for windows),它不会删除你原有配置

curl -sLf https://spacevim.org/install.sh | bash

然后,开始尝试吧

vim

第一次使用spacevim时,需要一些时间安装插件。

2. 卸载并回滚到原vim

curl -sLf https://spacevim.org/install.sh | bash -s -- --uninstall

3. 同时使用你原有的vim和spacevim

See FAQ:Have a try with SpaceVim without overwrite vimrc?

怎么进行安装的?

其实只是重命名了你的~/.vim,名字改成~/.vim_back,用下面的命令查看安装脚本的更多信息
curl -sLf https://spacevim.org/install.sh | bash -s -- -h

强烈建议:

更新你的vim到neovim(neovim体验更好)
或者 vim8.0 以便有更好的SpaceVim体验

二、基本概念#

SpaceVim中有非常多的快捷键,最常用的快捷键是空格键,我猜这是为什么它叫spacevim。接下来
我们用简称spc来表示空格键。

1. 快捷键#

有许多不同的快捷键,可以归为下面几类

Prefix Prefix name Example Description More info
space [SPC] spc f t 打开/关闭 文件浏览器 just press space key and wait 1s
s [WIN] s v 水平分割窗口,等价于:split link
\ <Leader> \ [0-9] 跳到其他tab(页面)或buffer(缓存) just press \ key and wait 1s
g go to g 0 跳到第一个tab link
z fold z a 触发代码折叠 link

进入spacevim后,按一下空格键,等待1秒,你可以看到有用的提示信息,通过这些
信息来浏览一下spacevim的更多快捷键吧。

2. 一些概念#

名称 解释
Window A window can split into more windows
Buffer A buffer is a file loaded into memory for editing
Tab/Frame A tab could contain different file
Tagbar Display tags of current file
Filetree File browser

对于vim新手

如果你仍然不熟悉vim,可以在linux终端中输入vimtutor,进入vim的实战教程,大概
需要花半小时掌握vim的基本技能。

对于vim的老用户

SpaceVim 有些快捷键是和原生vim冲突的,例如sg,如果你想使用原生vim的配置,
~/.SpaceVim.d/init.toml中加入

[options]
  vimcompatible = true

更多vim兼容模式信息请查看vimCompatible mode特别是你只想更正其中的某个快捷键而保持其他不变。

SpaceVim默认使用vimfiler,你可能更喜欢用nerdtree,同样,在~/.SpaceVim.d/init.toml中加入

[options]
  filemanager = "nerdtree"

三、开始尝试#

1. 最常用快捷键#

虽然快捷键看起来很多,但是记起来并不麻烦,而且SpaceVim有很多提示,所以不要担心

留意:如果你使用了vim兼容模式(vimcompatible=true),下面的一些快捷键可能不生效

Shortcuts Description
基本操作
h j k l move around
spc wait 1 second to get hint for more shortcuts
<F3> / spc f t trigger file tree
spc t t Open tab manager
F2 Open tagbar
<Leader> [1-9] switch to different tabs or buffers
spc [1-9] switch to different windows
ctrl j/h/k/l switch to different windows
文件浏览器 Link
spc f o open file tree and locate to current directory
s g open file and split window horizontally
s v open file split window vertically
注释代码 Link
spc c l comment/uncomment current line
spc c p/P comment/uncomment current paragraph
spc ; [num] j comment num lines
页面Tab管理器 Link
spc t t open tab manager
spc w F open a new tab, equal to spc t t + n
spc w o switch tab
显示相关 Link
spc s c clear search highlight
SPC t 8 highlight any character past the 80th column
SPC t h h toggle highlight of the current line
SPC t h c toggle highlight indentation current column
搜索 Link
spc s s Searching in current file
spc s d Searching in current directory
spc s b Searching in all loaded buffers
spc s p Searching in current proj, equal to spc /
有用的快捷键 Link
[ spc insert space above
] spc insert space below

探索快捷键的最好方式依然是在spacevim中按下空格键等待一秒,它会弹出有用的提示框,亲自尝试其中的快捷键才能更加准确知道它的效果。

2. 高级设置#

(1) 特定编程语言支持#

这里以python为例,其他语言可见SpaceVim layers

~/.SpaceVim.d/init.toml中加入

[[layers]]
  name = "lang#python"

安装依赖

pip install --user flake8 yapf autoflake isort
  • flake8 : for syntax checking feature
  • yapf : for formatting code
  • autoflake : for uppress unused imports
  • isort : for sort your imports
Shortcuts Description
g d Go to function definition
spc b f format code according to pep8 standard
spc l g d generate docstring
spc l s i open ipython
spc l r run python code

(2) 模糊搜索#

模糊搜索是一个比较强大的功能,参见 Fuzzy finder.

这里使用unite作为例子,在~/.SpaceVim.d/init.toml中加入

[[layers]]
  name = "unite"

在终端中

sudo apt-get install silversearcher-ag

对于linux用户,需要使终端的ctrl-s(停止屏幕变化)失效才能方便使用模糊搜索的功能。
在初始化脚本如(~/.bashrc) 中加入 stty -ixon

最后,开始使用模糊搜索,如模糊搜索当前文件夹下的文件

spc f f
Shortcuts Description
<Tab> / Ctrl-j Select next line
Shift-Tab / Ctrl-k Select previous line
j k Leave Insert mode (Only for denite/unite)
Ctrl-w Delete backward path
<Enter> Run default action
Ctrl-s Open in a split
Ctrl-v Open in a vertical split
Ctrl-t Open in a new tab
Ctrl-g Exit unite

四、探索和自定义#

1. 自定义#

(1) Config ~/.SpaceVim.d/init.toml#

你可能想让默认的缩进为4个空格,那么在~/.SpaceVim.d/init.toml中加入

[options]
  default_indent = 4

实际上,上面的修改等价于在~/.SpaceVim/autoload/SpaceVim.vim中修改

    let g:spacevim_default_indent = 4

两种方式效果是一样的,只不过第一种多了前缀 let g:spacevim_

所以可设置的Spacevim选项可以在:h SpaceVim-config 或者文件~/.SpaceVim/autoload/SpaceVim.vim中找到

(2) Add your own vim script: bootstrap-functions#

举例说明如何使用bootstrap-functions

比如你想要每次vim启动的时候都自动换行set wrap,首先,新建一个文件.SpaceVim.d/autoload/myspacevim.vim,在里面添加如下内容

func! myspacevim#before() abort
  set wrap
endf

然后在~/.SpaceVim.d/init.toml中添加

[options]
  bootstrap_before = "myspacevim#before"

before 替换为 after如果你想要你自定义的函数在spacevim主脚本运行后再运行。

好吧,我知道上述的步骤有些麻烦。

(3) Change your colorschemes#

2. 探索#

  1. 更详细更全面的官方文档 Official document
  2. SpaceVim的扩展组件 Available layers
  3. 如果你有问题,不妨先看一下Frequently asked questions FAQ

需要注意的是,官方文档中,中文版本相对英文版本部分地方可能会有些过时。

3. 更多#

  1. 更新SpaceVim :SPUpdate
  2. SpaceVim debug 信息SPDebugInfo! or spc h I, 如果你想要提交一个issue,这将会很有帮助

参考#

  1. Spacevim tutorial 中文
  2. Hack-SpaceVim