idea2act translated

This commit is contained in:
idea2act 2018-09-12 22:00:58 +08:00
parent ce59ed46f6
commit feffda0c0b
2 changed files with 356 additions and 345 deletions

View File

@ -1,345 +0,0 @@
idea2act translating
Turn your vi editor into a productivity powerhouse
======
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/checklist_hands_team_collaboration.png?itok=u82QepPk)
A versatile and powerful editor, vi includes a rich set of potent commands that make it a popular choice for many users. This article specifically looks at commands that are not enabled by default in vi but are nevertheless useful. The commands recommended here are expected to be set in a vi configuration file. Though it is possible to enable commands individually from each vi session, the purpose of this article is to create a highly productive environment out of the box.
### Before you begin
While "vim" is the technically correct name of the newer version of the vi editor, this article refers to it as "vi." vimrc is the configuration file used by vim.
The commands or configurations discussed here go into the vi startup configuration file, vimrc, located in the user home directory. Follow the instructions below to set the commands in vimrc:
(Note: The vimrc file is also used for system-wide configurations in Linux, such as `/etc/vimrc` or `/etc/vim/vimrc`. In this article, we'll consider only user-specific vimrc, present in user home folder.)
In Linux:
* Open the file with `vi $HOME/.vimrc`
* Type or copy/paste the commands in the cheat sheet at the end of this article
* Save and close (`:wq`)
In Windows:
* First, [install gvim][1]
* Open gvim
* Click Edit --> Startup settings, which opens the _vimrc file
* Type or copy/paste the commands in the cheat sheet at the end of this article
* Click File --> Save
Let's delve into the individual vi productivity commands. These commands are classified into the following categories:
1. Indentation & Tabs
2. Display & Format
3. Search
4. Browse & Scroll
5. Spell
6. Miscellaneous
### 1\. Indentation & Tabs
To automatically align the indentation of a line in a file:
```
set autoindent
```
Smart Indent uses the code syntax and style to align:
```
set smartindent
```
Tip: vi is language-aware and provides a default setting that works efficiently based on the programming language used in your file. There are many default configuration commands, including `axs cindent`, `cinoptions`, `indentexpr`, etc., which are not explained here. `syn` is a helpful command that shows or sets the file syntax.
To set the number of spaces to display for a tab:
```
set tabstop=4
```
To set the number of spaces to display for a “shift operation” (such as >> or <<):
```
set shiftwidth=4
```
If you prefer to use spaces instead of tabs, this option inserts spaces when the Tab key is pressed. This may cause problems for languages such as Python that rely on tabs instead of spaces. In such cases, you may set this option based on the file type (see `autocmd`).
```
set expandtab
```
### 2\. Display & Format
To show line numbers:
```
set number
```
![](https://opensource.com/sites/default/files/uploads/picture01.png)
To wrap text when it crosses the maximum line width:
```
set textwidth=80
```
To wrap text based on a number of columns from the right side:
```
set wrapmargin=2
```
To identify open and close brace positions when you traverse through the file:
```
set showmatch
```
![](https://opensource.com/sites/default/files/uploads/picture02-03.jpg)
### 3\. Search
To highlight the searched term in a file:
```
set hlsearch
```
![](https://opensource.com/sites/default/files/uploads/picture04.png)
To perform incremental searches as you type:
```
set incsearch
```
![](https://opensource.com/sites/default/files/picture05.png)
To search ignoring case (many users prefer not to use this command; set it only if you think it will be useful):
```
set ignorecase
```
To search without considering `ignorecase` when both `ignorecase` and `smartcase` are set and the search pattern contains uppercase:
```
set smartcase
```
For example, if the file contains:
test
Test
When both `ignorecase` and `smartcase` are set, a search for “test” finds and highlights both:
test
Test
A search for “Test” highlights or finds only the second line:
test
Test
### 4. Browse & Scroll
For a better visual experience, you may prefer to have the cursor somewhere in the middle rather than on the first line. The following option sets the cursor position to the 5th row.
```
set scrolloff=5
```
Example:
The first image is with scrolloff=0 and the second image is with scrolloff=5.
![](https://opensource.com/sites/default/files/uploads/picture06-07.jpg)
Tip: `set sidescrolloff` is useful if you also set `nowrap.`
To display a permanent status bar at the bottom of the vi screen showing the filename, row number, column number, etc.:
```
set laststatus=2
```
![](https://opensource.com/sites/default/files/picture08.png)
### 5. Spell
vi has a built-in spell-checker that is quite useful for text editing as well as coding. vi recognizes the file type and checks the spelling of comments only in code. Use the following command to turn on spell-check for the English language:
```
set spell spelllang=en_us
```
### 6. Miscellaneous
Disable creating backup file: When this option is on, vi creates a backup of the previous edit. If you do not want this feature, disable it as shown below. Backup files are named with a tilde (~) at the end of the filename.
```
set nobackup
```
Disable creating a swap file: When this option is on, vi creates a swap file that exists until you start editing the file. Swapfile is used to recover a file in the event of a crash or a use conflict. Swap files are hidden files that begin with `.` and end with `.swp`.
```
set noswapfile
```
Suppose you need to edit multiple files in the same vi session and switch between them. An annoying feature that's not readily apparent is that the working directory is the one from which you opened the first file. Often it is useful to automatically switch the working directory to that of the file being edited. To enable this option:
```
set autochdir
```
vi maintains an undo history that lets you undo changes. By default, this history is active only until the file is closed. vi includes a nifty feature that maintains the undo history even after the file is closed, which means you may undo your changes even after the file is saved, closed, and reopened. The undo file is a hidden file saved with the `.un~` extension.
```
set undofile
```
To set audible alert bells (which sound a warning if you try to scroll beyond the end of a line):
```
set errorbells
```
If you prefer, you may set visual alert bells:
```
set visualbell
```
### Bonus
vi provides long-format as well as short-format commands. Either format can be used to set or unset the configuration.
Long format for the `autoindent` command:
```
set autoindent
```
Short format for the `autoindent` command:
```
set ai
```
To see the current configuration setting of a command without changing its current value, use `?` at the end:
```
set autoindent?
```
To unset or turn off a command, most commands take `no` as a prefix:
```
set noautoindent
```
It is possible to set a command for one file but not for the global configuration. To do this, open the file and type `:`, followed by the `set` command. This configuration is effective only for the current file editing session.
![](https://opensource.com/sites/default/files/uploads/picture09.png)
For help on a command:
```
:help autoindent
```
![](https://opensource.com/sites/default/files/uploads/picture10-11.jpg)
Note: The commands listed here were tested on Linux with Vim version 7.4 (2013 Aug 10) and Windows with Vim 8.0 (2016 Sep 12).
These useful commands are sure to enhance your vi experience. Which other commands do you recommend?
### Cheat sheet
Copy/paste this list of commands in your vimrc file:
```
" Indentation & Tabs
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set smarttab
" Display & format
set number
set textwidth=80
set wrapmargin=2
set showmatch
" Search
set hlsearch
set incsearch
set ignorecase
set smartcase
" Browse & Scroll
set scrolloff=5
set laststatus=2
" Spell
set spell spelllang=en_us
" Miscellaneous
set nobackup
set noswapfile
set autochdir
set undofile
set visualbell
set errorbells
```
--------------------------------------------------------------------------------
via: https://opensource.com/article/18/9/vi-editor-productivity-powerhouse
作者:[Girish Managoli][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/gammay
[1]: https://www.vim.org/download.php#pc

View File

@ -0,0 +1,356 @@
增强 Vim 编辑器,提高编辑效率
======
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/checklist_hands_team_collaboration.png?itok=u82QepPk)
编者注:标题和文章最初提到的 `vi` 编辑器,现已更新为编辑器的正确名称:`Vim`。
`Vim` 作为一款功能强大、选项丰富的编辑器,为许多用户所热爱。本文介绍了一些在 `Vim` 中默认未启用但实际非常有用的选项。虽然可以在每个 `Vim` 会话中单独启用,但为了创建一个开箱即用的高效编辑环境,还是建议在 `Vim` 的配置文件中配置这些命令。
## 开始前的准备
这里所说的选项或配置均位于用户主目录中的 `Vim` 启动配置文件 `.vimrc`。 按照下面的说明在 `.vimrc` 中设置选项:
(注意:`vimrc` 文件也用于 `Linux` 中的全局配置,如 `/etc/vimrc``/etc/vim/vimrc`。本文所说的 `.vimrc` 均是指位于用户主目录中的 `.vimrc` 文件。)
`Linux` 系统中:
* 用 `Vim` 打开 `.vimrc` 文件: `vim ~/.vimrc`
* 复制本文最后的 `选项列表` 粘贴到 `.vimrc` 文件
* 保存并关闭 (`:wq`)
译者注:此处不建议使用 `Vim` 编辑 `.vimrc` 文件,因为很可能无法粘贴成功,可以选择 `gedit` 编辑器编辑 `.vimrc` 文件。
`Windows` 系统中:
* 首先,[安装 gvim][1]
* 打开 `gvim`
* 单击 `编辑` -> `启动设置`,打开 `.vimrc` 文件
* 复制本文最后的 `选项列表` 粘贴到 `.vimrc` 文件
* 单击 `文件` -> `保存`
译者注:此处应注意不要使用 `Windows` 自带的记事本编辑该 `.vimrc` 文件。
下面,我们将深入研究提高 `Vim` 编辑效率的选项。主要分为以下几类:
1. 缩进 & 制表符
2. 显示 & 格式化
3. 搜索
4. 浏览 & 滚动
5. 拼写
6. 其他选项
## 1. 缩进 & 制表符
使 `Vim` 在创建新行的时候使用与上一行同样的缩进:
```vim
set autoindent
```
创建新行时使用智能缩进,主要用于 `C` 语言一类的程序。通常,打开 `smartindent` 时也应该打开 `autoindent`
```vim
set smartindent
```
注意:`Vim` 具有语言感知功能,且其默认设置可以基于文件中的编程语言来改变配置以提高效率。有许多默认的配置选项,包括 `axs cindent``cinoptions``indentexpr` 等,没有在这里说明。 `syn` 是一个非常有用的命令,用于设置文件的语法以更改显示模式。
译者注:这里的 `syn` 是指 `syntax`,可用于设置文件所用的编程语言,开启对应的语法高亮,以及执行自动事件 (`autocmd`)。
设置文件里的制表符 `(TAB)` 的宽度(以空格的数量表示):
```vim
set tabstop=4
```
设置移位操作 `>>``<<` 的缩进长度(以空格的数量表示):
```vim
set shiftwidth=4
```
如果你更喜欢在编辑文件时使用空格而不是制表符,设置以下选项可以使 `Vim` 在你按下 `Tab` 键时用空格代替制表符。
```vim
set expandtab
```
注意:这可能会导致依赖于制表符的 `Python` 等编程语言出现问题。这时,你可以根据文件类型设置该选项(请参考 `autocmd`)。
## 2. 显示 & 格式化
要在每行的前面显示行号:
```vim
set number
```
![](https://opensource.com/sites/default/files/uploads/picture01.png)
要在文本行超过一定长度时自动换行:
```vim
set textwidth=80
```
要根据从窗口右侧向左数的列数来自动换行:
```vim
set wrapmargin=2
```
译者注:如果 `textwidth` 选项不等于零,本选项无效。
插入括号时,短暂地跳转到匹配的括号:
```vim
set showmatch
```
![](https://opensource.com/sites/default/files/uploads/picture02-03.jpg)
## 3. 搜索
高亮搜索内容的所有匹配位置:
```vim
set hlsearch
```
![](https://opensource.com/sites/default/files/uploads/picture04.png)
搜索过程中动态显示匹配内容:
```vim
set incsearch
```
![](https://opensource.com/sites/default/files/picture05.png)
搜索时忽略大小写:
```vim
set ignorecase
```
在打开 `ignorecase` 选项的条件下,搜索内容包含部分大写字符时,要使搜索大小写敏感:
```vim
set smartcase
```
例如,如果文件内容是:
> test\
> Test
当打开 `ignorecase``smartcase` 选项时,搜索 `test` 时的突出显示:
> <font color=yellow>test</font>\
> <font color=yellow>Test</font>
搜索 `Test` 时的突出显示:
> test\
> <font color=yellow>Test</font>
## 4. 浏览 & 滚动
为获得更好的视觉体验,你可能希望将光标放在窗口中间而不是第一行,以下选项使光标距窗口上下保留 5 行。
```vim
set scrolloff=5
```
一个例子:
第一张图中 `scrolloff=0`,第二张图中 `scrolloff=5`
![](https://opensource.com/sites/default/files/uploads/picture06-07.jpg)
提示:如果你没有设置选项 `nowrap`,那么设置 `sidescrolloff` 将非常有用。
`Vim` 窗口底部显示一个永久状态栏,可以显示文件名、行号和列号等内容:
```vim
set laststatus=2
```
![](https://opensource.com/sites/default/files/picture08.png)
## 5. 拼写
`Vim` 有一个内置的拼写检查器,对于文本编辑和编码非常有用。`Vim` 可以识别文件类型并仅对代码中的注释进行拼写检查。使用下面的选项打开英语拼写检查:
```vim
set spell spelllang=en_us
```
译者注:中文、日文或其它东亚语字符通常会在打开拼写检查时被标为拼写错误,因为拼写检查不支持这些语种,可以在 `spelllang` 选项中加入 `cjk` 来忽略这些错误标注。
## 6. 其他选项
禁止创建备份文件:启用此选项后,`Vim` 将在覆盖文件前创建一个备份,文件成功写入后保留该备份。如果不想保留该备份文件,可以按下面的方式关闭:
```vim
set nobackup
```
禁止创建交换文件:启用此选项后,`Vim` 将在编辑该文件时创建一个交换文件。 交换文件用于在崩溃或发生使用冲突时恢复文件。交换文件是以 `.` 开头并以 `.swp` 结尾的隐藏文件。
```vim
set noswapfile
```
如果需要在同一个 `Vim` 窗口中编辑多个文件并进行切换。默认情况下,工作目录是打开的第一个文件的目录。而将工作目录自动切换到正在编辑的文件的目录是非常有用的。要自动切换工作目录:
```vim
set autochdir
```
`Vim` 自动维护编辑的历史记录,允许撤消更改。默认情况下,该历史记录仅在文件关闭之前有效。`Vim` 包含一个增强功能,使得即使在文件关闭后也可以维护撤消历史记录,这意味着即使在保存、关闭和重新打开文件后,也可以撤消之前的更改。历史记录文件是使用 `.un~` 扩展名保存的隐藏文件。
```vim
set undofile
```
错误信息响铃,只对错误信息起作用:
```vim
set errorbells
```
如果你愿意,还可以设置错误视觉提示:
```vim
set visualbell
```
## 惊喜
vi provides long-format as well as short-format commands. Either format can be used to set or unset the configuration.
`Vim` 提供长格式和短格式命令,两种格式都可用于设置或取消选项配置。
`autoindent` 选项的长格式是:
```vim
set autoindent
```
`autoindent` 选项的短格式是:
```vim
set ai
```
To see the current configuration setting of a command without changing its current value, use `?` at the end:
要在不更改选项当前值的情况下查看其当前设置,可以在 `Vim` 的命令行上使用在末尾加上 `?` 的命令:
```vim
set autoindent?
```
在大多数选项前加上 `no` 前缀可以取消或关闭选项:
```vim
set noautoindent
```
可以为单独的文件配置选项,而不必修改全局配置文件。需要的话,请打开文件并输入 `:`,然后键入 `set`命令。这样的话,配置仅对当前的文件编辑会话有效。
![](https://opensource.com/sites/default/files/uploads/picture09.png)
使用命令行获取帮助:
```vim
:help autoindent
```
![](https://opensource.com/sites/default/files/uploads/picture10-11.jpg)
注意:此处列出的命令仅对 `Linux` 上的 `Vim 7.4` 版本和 `Windows` 上的 `Vim 8.0` 版本进行了测试。
这些有用的命令肯定会增强您的 `Vim` 使用体验。你会推荐哪些其他有用的命令?
## 选项列表
复制该选项列表粘贴到 `.vimrc` 文件中:
```vim
" Indentation & Tabs
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set smarttab
" Display & format
set number
set textwidth=80
set wrapmargin=2
set showmatch
" Search
set hlsearch
set incsearch
set ignorecase
set smartcase
" Browse & Scroll
set scrolloff=5
set laststatus=2
" Spell
set spell spelllang=en_us
" Miscellaneous
set nobackup
set noswapfile
set autochdir
set undofile
set visualbell
set errorbells
```
--------------------------------------------------------------------------------
via: https://opensource.com/article/18/9/vi-editor-productivity-powerhouse
作者:[Girish Managoli][a]
选题:[lujun9972](https://github.com/lujun9972)
译者:[idea2act](https://github.com/idea2act)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/gammay
[1]: https://www.vim.org/download.php#pc