Vim

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Vim and the translation is 39% complete.
Outdated translations are marked like this.

Vim (Vi improved) 是一个基于vi 的本文编辑器。它可以从命令行或作为一个具有图形用户界面的独立应用程序使用。

不要将 Vim 与 Neovim 混淆,后者是一个类似的可以替代其的程序。

另请参阅
The Vi article provides general information on vi-like editors. See the Vim guide for an introductory tutorial on vi-like editor usage. See the text editor article for general information on installing and configuring text editors in Gentoo.

USE 标记

USE flags for app-editors/vim Vim, an improved vi-style text editor

X Link console vim against X11 libraries to enable title and clipboard features in xterm
acl Add support for Access Control Lists
crypt Use dev-libs/libsodium for crypto support
cscope Enable cscope interface
debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
gpm Add support for sys-libs/gpm (Console-based mouse driver)
lua Enable Lua scripting support
minimal Install a very minimal build (disables, for example, plugins, fonts, most drivers, non-critical features)
nls Add Native Language Support (using gettext - GNU locale utilities)
perl Add optional support/bindings for the Perl language
python Add optional support/bindings for the Python language
racket Enable support for Scheme using dev-scheme/racket
ruby Add support/bindings for the Ruby language
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
sound Enable sound support
tcl Add support the Tcl language
terminal Enable terminal emulation support
vim-pager Install vimpager and vimmanpager links

Emerge

如果不需要 X Window System 支持,请安装app-editors/vim:

root #emerge --ask app-editors/vim

附加软件

Gvim

若要同时安装 ncurses-based 接口和图形界面接口 ( X Window System - /usr/bin/gvim),请安装 app-editors/gvim 包:

root #emerge --ask app-editors/gvim

软件包

Vim 拥有支持包,提供了一个本地机制来扩展功能。请阅读内置文档以了解更多信息::help packages

插件

app-vim 类别提供了大量额外的语法定义,插件和其他Vim相关的东西。

使用emergeeix 来获取 app-vim 类中可用包的概览:

user $emerge --search "%@^app-vim"
user $eix -cC app-vim

Gentoo软件库并不会提供所有的Vim插件。Vim现在包括 native packages,作为一种安装插件的方式,同时还有 plugin managers 用于Vim。

配置

文件

vim 可以根据每个用户或通过整个系统配置文件进行配置:

  • /etc/vim/vimrc - 整个系统(全局)设置文件。
  • ~/.vimrc - 用户特定的 (本地) 配置文件。符号(~) 表示它在用户的主目录。
  • ~/.vim/pack/foo - 插件安装位置 (vim 8 或更高)。[1] 用子目录 foo 代替每个插件的名称。

配色方案

大约有12种配色方案与基础 VIM 包一起打包。可以通过键入 colorscheme ,然后按 Ctrl+d 或按 Tab 键两次,在末行模式下列出它们:

:colorscheme
blue       darkblue   default    delek      desert     elflord    evening    industry   koehler    morning    murphy     pablo      peachpuff  ron        shine      slate      torte      zellner

可以在末行模式中通过使用 colorscheme (可以选用colo)命令改变他们:

:colorscheme peachpuff

可以在 .vimrc 文件中永久地应用配色方案 :

文件 ~/.vimrc
colorscheme peachpuff
syntax on

第一行设置默认配色方案,而最后一行激活配色方案。

选择 vi 编辑器以及系统默认编辑器

If Vim - and only Vim - is installed, the vi command should launch Vim. If other vi-like editors are installed, eselect may be used to choose which editor the vi command launches.

The text editor article may also be of interest for setting a system default editor, if Vim is to be set as the default.

Gentoo syntax

To enable support for the Gentoo syntax plugin (app-vim/gentoo-syntax) within vim, add the following lines to the user's ~/.vimrc file:

文件 ~/.vimrc
filetype plugin on
filetype indent on

leave_my_textwidth_alone

By default, Vim and Neovim do not hard-wrap lines[2], i.e., typing on a line longer than a certain length does not create a new line. However, Gentoo installs config files for Vim[3] and Neovim[4] that cause lines longer than 78 characters in .txt files to be hard-wrapped. To override this behavior, the following should be specified in the user's ~/.vimrc file:

文件 ~/.vimrc
let g:leave_my_textwidth_alone=1

bug #559800 contains some discussion around this issue.

使用

调用

命令行指令:

user $vim --help
vim --help
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Nov 26 2021 11:56:27)
 
Usage: vim [arguments] [file ..]       edit specified file(s)
   or: vim [arguments] -               read text from stdin
   or: vim [arguments] -t tag          edit file where tag is defined
   or: vim [arguments] -q [errorfile]  edit file with first error
 
Arguments:
   --			Only file names after this
   -v			Vi mode (like "vi")
   -e			Ex mode (like "ex")
   -E			Improved Ex mode
   -s			Silent (batch) mode (only for "ex")
   -d			Diff mode (like "vimdiff")
   -y			Easy mode (like "evim", modeless)
   -R			Readonly mode (like "view")
   -Z			Restricted mode (like "rvim")
   -m			Modifications (writing files) not allowed
   -M			Modifications in text not allowed
   -b			Binary mode
   -l			Lisp mode
   -C			Compatible with Vi: 'compatible'
   -N			Not fully Vi compatible: 'nocompatible'
   -V[N][fname]		Be verbose [level N] [log messages to fname]
   -D			Debugging mode
   -n			No swap file, use memory only
   -r			List swap files and exit
   -r (with file name)	Recover crashed session
   -L			Same as -r
   -A			Start in Arabic mode
   -H			Start in Hebrew mode
   -T <terminal>	Set terminal type to <terminal>
   --not-a-term		Skip warning for input/output not being a terminal
   --ttyfail		Exit if input or output is not a terminal
   -u <vimrc>		Use <vimrc> instead of any .vimrc
   --noplugin		Don't load plugin scripts
   -p[N]		Open N tab pages (default: one for each file)
   -o[N]		Open N windows (default: one for each file)
   -O[N]		Like -o but split vertically
   +			Start at end of file
   +<lnum>		Start at line <lnum>
   --cmd <command>	Execute <command> before loading any vimrc file
   -c <command>		Execute <command> after loading the first file
   -S <session>		Source file <session> after loading the first file
   -s <scriptin>	Read Normal mode commands from file <scriptin>
   -w <scriptout>	Append all typed commands to file <scriptout>
   -W <scriptout>	Write all typed commands to file <scriptout>
   -x			Edit encrypted files
   --startuptime <file>	Write startup timing messages to <file>
   -i <viminfo>		Use <viminfo> instead of .viminfo
   --clean		'nocompatible', Vim defaults, no plugins, no viminfo
   -h  or  --help	Print Help (this message) and exit
   --version		Print version information and exit

The vi command may also be used to launch Vim, if so configured.

指定一个名称,打开一个现有文件,或创建一个新文件:

user $vim <filename>

教程

vim 有一个内置的教程, 大约需要花费 30 分钟来浏览它。使用 vimtutor 命令启动教程:

user $vimtutor

提示与技巧

像ex或ed一样从命令行使用Vim

可以使用 vim 进行 one-liners —可在脚本或命令行中使用的命令以无人值守的方式进行更改。

例如, 下面的命令将 # 添加到file.txt文件中每行的开头:

user $vim -c ":%s/^/#/g" -c ":x" file.txt

Vim会解释传递的命令 (通过 -c 参数).第一个命令是Vim的替换命令(与sed非常相似),第二个命令是Vim的保存和退出编辑器的指令。

改变文件编码

要将文件的文件编码更改为 UTF-8, 请使用以下命令 (在 vim 末行模式下):

:e ++enc=utf8

如前面的技巧所示, 也可以从命令行执行此操作:

user $vim -c ":wq! ++enc=utf8" file.txt

故障排除

震惊!困于vim中无法自拔!

对于误入vim邪恶巢穴的新手来说,如何逃离可能并非易事。

esc 数次,键入 :qenter。要想不保存而退出,按 esc 数次,键入 :q!enter

如果你打开了若干文件,尝试按 esc 数次,然后 :qaenter。退出但不保存,键入 esc 多次,然后 :qa!enter

If this doesn't help, and desperate measures are needed, something like killall vim may be a last ditch solution (from another terminal). Beware that this will terminate all the vim sessions for a user, without saving. If run as root, such a command will terminate all vim sessions for all users on the system.

Pastes are being tabbed

Say the following text is trying to be pasted into a Vim buffer:

代码 Sample tabbed data
some:
  yaml:
    - data
    - structured
  in:
    - this
    - shape
  that:
    - VIM
    - may 
not: like

Vim may interpret this with autotabbing, breaking the paste:

代码 Vim misinterpretation
some:
  yaml:
      - data
          - structured
	   in:
	       - this
		   - shape
		     that:
		         - VIM
			     - may 
			     not: like

To correct this, run the following command: :set paste to enable Vim's paste mode, once the paste is completed, :set nopaste can be used to go back to the previous settings.

E1187

user $vim foobar
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

Seems solved by destabilize 8.2.4328.

参考

  • Vim Guide -说明如何更详细地使用 vim。

外部资源

参考