跳至主要内容

博文

目前显示的是标签为“vim”的博文

Ubuntu 安装和配置 NeoVim

最近想试试 NeoVim,在 Windows 10 WSL 的 Ubuntu (20.04) 里安装 neovim 是非常方便的,[用 Snap](https://askubuntu.com/questions/430008/how-to-install-neovim-on-ubuntu) 就可以了。 sudo apt install neovim 如果需要将 Neovim 设置为常用的编辑器,最好[利用系统的 Alternatives 设定](https://blog.aktsbot.in/vim-to-neovim.html),以取代常见的默认程序 (如 `/usr/bin/vim.basic`)。 ```bash $ sudo update-alternatives --install /usr/bin/vi vi /usr/bin/nvim 60 $ sudo update-alternatives --install /usr/bin/vim vim /usr/bin/nvim 60 $ sudo update-alternatives --install /usr/bin/editor editor /usr/bin/nvim 60 ``` [除了以上这几个命令之外,还有 ex, vi, vim, editor, view, vimdiff 等等](https://stackoverflow.com/questions/65889273/how-do-i-add-nvim-to-update-alternatives-when-using-snap-under-ubuntu),都可以使用类似的命令来替换。 值得一提的是,不像 `vim`,`neovim` 不再支持各种 alias commands,而是推荐直接调用 `nvim` 时传入相应的 args 来进入需要的模式,例如 `nvim -R` 就类似于 `view`;`nvim -d` 类似于 `vimdiff`。可以参考这两个 GitHub Issues: - https://github.com/neovim/neovim/pull/2008 - https://github.com/neovim/neovim/issues/1646

SpaceVim with NeoVim in Docker

在Docker里玩了会儿neovim+spacevim。 ``` docker run --name nvim -it --rm spacevim/spacevim nvim ``` 版本是最新的 ``` NVIM v0.7.0-dev+792-gc46f7caad Build type: RelWithDebInfo LuaJIT 2.1.0-beta3 Compilation: /usr/bin/cc -DNVIM_TS_HAS_SET_MATCH_LIMIT -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wst rict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-st rong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 - DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/usr/src/neovim/build/config -I/usr/src/neovim/src -I/usr/src/neovim/.deps /usr/include -I/usr/include -I/usr/src/neovim/build/src/nvim/auto -I/usr/src/neovim/build/include Compiled by root@buildkitsandbox ``` 不过启动后老报错: ``` Error detected while processing BufEnter Autocommands for "*": E117: Unknown function: LaTeXtoUnicode#Refresh startify: Can't read viminfo file. Read :help startify-faq-02 `...

Auto format files based on type in VIM

It would be really convenient to utilise the editor to auto-format some lengthy lines, e.g. a super long SQL retrieved from logging output; or a wrapped JSON literal captured in your browser. Luckily it's easy to achieve this in VIM - Firat, you'll need to install a few modules using `pip`, including `sqlparse` and `json.tool`. And then in the `filetype.vim` (or your own `vimrc`), include below commands: autocmd FileType xml setlocal equalprg=xmllint\ --format\ --recover\ -\ 5>/dev/null autocmd FileType json setlocal equalprg=python\ -m\ json.tool autocmd FileType sql setlocal equalprg=python\ -m\ sqlparse\ -a\ -s\ -

Learning SpaceVim

最近一直试着用SpaceVim,在Mac上取消了自定义的vimrc,直接用 init.toml 作为配置文件。 体验下来感觉SpaceVim的配置是更高一层的,用户可以指定需要的 layer,不过有一些低一层的配置暂时还不晓得如何移植过去? ``` [options] realtime_leader_guide = 1 enable_tabline_filetype_icon = 1 enable_statusline_display_mode = 0 statusline_separator = 'slant' enable_os_fileformat_icon = 1 buffer_index_type = 1 colorscheme = 'koehler' enable_vimfiler_welcome = 1 enable_debug = 1 guifont = 'Hack\ Regular\ Nerd\ Font\ Complete:h14' bootstrap_before = "spcvm#autoformat" [[layers]] name = 'ctrlp' [[layers]] name = 'git' [[layers]] name = 'lang' [[layers]] name = 'lang#javascript' on_ft = ['js', 'javascript'] [[layers]] name = 'lang#typescript' on_ft = 'ts' [[layers]] name = 'lang#json' on_ft = ['json', 'jsonp'] [[layers]] name = 'lang#java' o...

Create a docker image for coding (2)

基本思路是基于alpine来安装一些软件,包括 zsh, tmux, vim 等。 ``` FROM alpine:latest RUN echo "start to build linux workspace" \ # Install Workspace related tools && apk add --update --no-cache git curl vim tmux zsh \ # Install VIM bundle && mkdir -p ~/.vim/bundle \ && git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` 接着开始配置这些工具,像是 vim 和 tmux 最好使用 GitHub 上开放的配置文件,或者如果自己有偏好设置也行。我使用的就是自己定制的一些配置文件。 ``` COPY vimrc /root/.vimrc COPY tmux.conf /root/.tmux.conf ``` zsh 的配置就没有太多的花头,大名鼎鼎的 oh-my-zsh 足矣。 ``` RUN echo "then configure tools" \ # By default the login shell for root user is /bin/ash in alpine, replace with zsh && sed -i -e "s/bin\/ash/bin\/zsh/" /etc/passwd \ # Install oh-my-zsh and configure zsh && (sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)" || true) \ && sed -i -- 's/robbyrussell/w...

Create a docker image for coding (1)

玩了会儿 Alpine 发现短小精悍, > docker run -it --rm -t alpine:latest /bin/sh 于是开始基于这个image打造自己的coding environment。安装了 zsh, tmux, vim 等工具。启动一个 container 就直接进入 tmux 的界面,可以随意开多视窗或是切割视窗,丰俭由人。 参考资料: 0. [Docker run reference](https://docs.docker.com/engine/reference/run/) 0. [Dockfile](https://github.com/JAremko/alpine-vim/blob/master/alpine-vim-base/Dockerfile) 0. [Oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh) 0. [Vundle for VIM](https://github.com/VundleVim/Vundle.vim) 0. [Making tmux Pretty and Usable](http://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/)

VIM Bundle - a plugins manager

[Vundle](https://github.com/VundleVim/Vundle.vim) 是一个为 VIM 设计的插件管理器, 这个名字其实就是 VIM Bundle 的缩写。 第一步是下载 Vundle.vim 文件,可以直接从 GitHub 上克隆: $ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim 首先要加载 Vundle,要为 VIM 的 runtime path 添加指向 Vundle.vim 的路径, filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins " call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' 接着指定需要哪些插件 (plugins) " The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo Plugin 'tpope/vim-fugitive' " plugin from http://vim-scripts.or...

解决 VIM 在 Windows 下打开文件乱码的问题

由于在新的 PC 上安装了 Windows 7,一些常用的软件也要跟着重装。突然发现 Vim 7.4 无法打开 UTF-8 编码的文本文件,满屏的乱码真是亮瞎了我的眼啊啊啊啊! 谷歌了几个解决方案,个人认为以下设置最佳: + 设置与编辑文件有关的编码格式 set fileencoding=utf-8 set fileencodings=ucs-bom,utf-8,chinese set encoding=utf-8 set termencoding=utf-8 + vim提示信息乱码的解决,包括了启动时显示的关于界面,以及状态栏等 language messages zh_CN.utf-8 + vim的菜单乱码解决 同样在 _vimrc文件里以上的中文设置后加上下列命令, source $VIMRUNTIME/delmenu.vim source $VIMRUNTIME/menu.vim 简单解释一下以上几个选项所代表的意义: 1. `encoding` Vim 内部使用的字符编码方式,包括 Vim 的 buffer (缓冲区)、菜单文本、消息文本等。 2. `fileencoding` Vim 中当前编辑的文件的字符编码方式,Vim 保存文件时也会将文件保存为这种字符编码方式 (不管是否新文件都如此)。 3. `fileencodings` Vim 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。因此最好将 Unicode 编码方式放到这个列表的最前面,将拉丁语系编码方式 latin1 放到最后面 4. `termencoding` Vim 所工作的终端 (或者 Windows 的 Console 窗口) 的字符编码方式。这个选项在 Windows 下对我们常用的 GUI 模式的 gVim 无效,而对 Console 模式的 Vim 而言就是 Windows 控制台的代码页,并且通常我们不需要改变它。

关于代码缩进的思考

在 PEP8 读到了关于代码缩进的指导建议: Code lay-out Indentation Use 4 spaces per indentation level. For really old code that you don't want to mess up, you can continue to use 8-space tabs. Tabs or Spaces? Never mix tabs and spaces. The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended! For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do. 在此之前我也仔细考虑过缩进应该使用哪个比较好——tab vs. white space.最终我觉得使用tab比较适合我,主要是基于以下几点考虑: 在行首的所有缩进都是用tab,在行内和行末只适用white space,这样无论代码使用哪一种IDE或是editor打开,无论设置的tab width是4或是8或是其他什么奇怪的数,每一行的缩进关系都可以正确的显示出来,差别只是宽度而已,不影响可读性。 我使...

VIM diff用法简介

进入 diff 模式编辑的最简单方法就是用 "vimdiff" 命令。它象通常启动 Vim 一样, 不过额外地,它设定一些参数来设定查看不同之处。 vimdiff file1 file2 [file3 [file4]] 这等同于: vim -d file1 file2 [file3 [file4]] 你也可以使用 "gvimdiff" 或 "vim -d -g"。GUI 就启动了。 你也可以使用 "viewdiff" 或 "gviewdiff" 。Vim 会进入只读模式。 有两条命令可用于在 diffs 之间跳转: * [c * [c 向后至上一个更改的开始。当加上个数字后,便重复执行相应 次。 * ]c * ]c 向前至下一个更改的开始。当加上个数字后,便重复执行相应 次。 如果不存在光标可以跳转到的更改,将产生错误。 有两个命令可用来在两个缓冲之间拷贝。相应的结果是,在一定的范围内两缓冲的内容 被统一。 * :diffg * * :diffget * : [range] diffg[et] [bufspec] 用另一个缓冲来修改当前的缓冲,消除不同之处。如果给定 [bufspec],就使用该缓冲。否则,它仅当有另一个在 diff 模式下 的缓冲时才起作用。 [range] 参考下面。 ...

VIM + CTags (2)

CTAGS工作的原理其实就是抓取源文件中所有的标签名字——包括关键字、变量名、方法名等,将它们排序后存储在标签列表文件中。VIM可以读取这个标签列表,根据用户的需要执行跳转。 CTAGS生成标签列表的方法很简单,以下命令展示了如何使用CTAGS抓取src目录中的所有文件生成标签列表: # ctags -f .\.tags -h .java -R .\src %JAVA_HOME%\src 这里用到了ctags的2个option(命令选项): -f [name]: 指定生成的标签列表的存储路径。 按照一定的规则存放所有的项目文件是一个好习惯。这里如果[name]指定为 - ,将会在stdout输出生成的标签列表。 -R: 等效于--recursive,要求递归对子目录进行抓取,一并生成标签。 对于Java、C#这些通过package支持命名空间的语言来说,这无疑是个非常高效的选项。想想com.jlazy.ctags.vim.Sample 这样的名字吧,对应到文件系统该需要多少个子目录,如果手动来抓取,那岂不是要抓到手抽筋…… -h [list]: 指定待处理的文件扩展名列表。 例如,".h.c.cpp.cxx.hpp"是为典型的C++程序指定的文件扩展名列表。 仔细打开ctags生成的tags文件看看,会发现头部其实定义了一些表明当前文件的状态的变量: !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ !_TAG_PROGRAM_NAME Exuberant Ctags // !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ !_TAG_PROGRAM_VERSION 5.5.4 // 通常直接生成的tags文件都是排过序的(sorted),显而易见排序的目的是方便vim通过查询算法快速定位标签。就像二分查找需要应...

VIM + CTags (1)

通常情况下,程序员在日常工作中打开的不可能只有一份文件——想想Java的package和class,C、C++的header files,还有.Net C#的partial class就会赞同这个观点。VIM这种强悍的编辑器自然充分考虑到程序员的需求 —— move around quickly。 VIM支持CTAGS程序,这是一个独立的程序,大多数Unix/Linux系统都安装了它的实现。只需要在$VIM/plugin中安装 taglist 插件即可借助于ctag的威力对所有文件进行tag的索引。值得一提的是,ctag本身在不同系统上的实现是可能有所不同的,推荐使用的是source forge上的开源项目 exuberant ctags。 C:\Program Files\GNU>ctags --version Exuberant Ctags 5.5.4, Copyright (C) 1996-2003 Darren Hiebert Compiled: Mar 29 2004, 22:21:09 Addresses: <dhiebert@users.sourceforge.net>, http://ctags.sourceforge.net Optional compiled features: +win32, +regex, +internal-sort 安装了CTAGS程序之后,可以在系统变量path中加入该程序的路径,比较方便。如果不加入path,则需要在vim中加入配置行: let Tlist_Ctags_Cmd='C:\Program Files\GNU\ctags.exe' let Tlist_Use_Right_Window=1 let Tlist_File_Fold_Auto_Close=1 使用taglist的第一步,就是调用CTAGS程序生成tag list。 > ctags.exe $SRC\*.cpp $SRC\*.h 如果需要针对给定目录下的所有代码文件都生成tag,可以采用: > ctags.exe -R $SRC\*.java 生成的tags文件就是所谓的tag list(标签列表文件)。VIM可以通过这个文件来查找任何一个标签被定义的位置并跳转。 如果知道一个标签的名字,可以通过命令...

Project in VIM

最近想要用VIM组织一下文件,搜索了几个插件,觉得project还不错。VIM本身朴素的文本界面决定了文件树的简洁特质。 Project插件为VIM提供了组织文件的功能。用户可以将常用的文件组织到一个文件列表中。这份列表将会被VIM显示在左侧的窗口中。可以按『回车』键或是双击文件名来打开文件。这比起另一个插件『file-explorer』要容易使用。 这个插件还可以帮助用户切换到另一个目录,并运行vim的脚本。这些脚本可以修改环境变量,将编译器包含到$PATH变量中。这使得需要操作多个项目变得轻松易行。 由于Project插件内置了对VIM的folder的支持,因此可以自由地使用折叠功能来控制各个directory结点。而且使用'/'或是'?'进行查找的时候,丝毫不受折叠的影响。真是太酷了! 其它特点包括: 加载或是卸载目录中所有的文件(\r)。 Grepping项目中所有的文件,这个特征还没有研究,不知道Windows下是否也支持grep。 运行一个用户自定义的脚本(非常有用的功能,可以用于定制编译环境)。 高度可定制。用户甚至可以自己编辑project的定义文件。 可以配合netrw插件,通过不同的协议(如ftp、rcp、scp或是http等)来操作不同的文件。