Merge pull request #5372 from Cathon/master

[Translated]20170302 How to use markers and perform text selection in…
This commit is contained in:
VicYu 2017-03-30 14:17:26 +08:00 committed by GitHub
commit a55d3d88ee
2 changed files with 113 additions and 128 deletions

View File

@ -1,128 +0,0 @@
Cathon is translating
How to use markers and perform text selection in Vim
============================================================
When using GUI-based text/source code editors, some features are a given, such as selecting text. I mean, most of us won't even consider this a feature anymore. But that's not the case with command line based editors like Vim. Specifically for Vim, when only using keyboard, you'll have to learn certain commands in order to select text the way you want. In this tutorial we will discuss this feature as well as the 'marks' feature of Vim in detail.
But before we start doing that, it's worth mentioning that all the examples, commands, and instructions mentioned in this tutorial have been tested on Ubuntu 16.04, and the Vim version we've used is 7.4.
# Text selection options in Vim
Assuming that you have the basic knowledge about the Vim editor (No? no problem, just head [here][2]), you would be knowing that the 'd' command lets you cut/delete a line. But what if you want to cut, say, 3 lines? Your answer could be: 'repeat the command thrice'. Fine, but what if the requirement is to cut 15 lines? Is running the 'd' command 15 times a practical solution?
No, it's not. A better solution, in this case, would be to select the lines you want to cut/delete, and then run the 'd' command just once. Here's an example:
Suppose I want to cut/delete the complete first paragraph of the INTRODUCTION section shown in the screenshot below:
[
![Text edited in VIM](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-example.png)
][3]
So what I'll do is, I'll bring the cursor in the beginning of the first line, and (making sure I am out of Insert mode) type the 'V' (Shift+v) command. This will result in the first line being selected and Vim enabling the Visual Line mode.
[
![Select a line with VIM](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-initiated.png)
][4]
Now, all I have to do is to use the down arrow key to select the whole paragraph.
[
![Select multiple lines with Vim](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-working.png)
][5]
So that's what we wanted, right? Now just press 'd' and the selected paragraph will be cut/deleted. Needless to say, aside from cut/delete, you can perform any other option on the selected text.
This brings us to another important aspect: not every time we need to delete the complete line or lines; what to do in those cases?  What that means is, the solution we just discussed only works when you want to perform operation on complete line(s). What if the requirement is to delete the first three sentences in a paragraph?
Well, there's a command for this as well - just use 'v' instead of 'V' (without single quotes of course). Following is an example where-in I used 'v' and then selected the first three sentences in the paragraph:
[
![Select the first three sentences in Vim](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-partial-lines.png)
][6]
Moving on, sometimes the data you are dealing with consists of separate columns, and the requirement may be to select a particular column. For example, consider the following screenshot:
[
![Columns in Vom](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-columns.png)
][7]
Suppose the requirement is to only select the name of countries, which mean the second column of the text. So what you can do in this case is, bring your cursor under the first element of the column in question and press Ctrl+v once. Now, using the down arrow key, select the first letter of each country name:
[
![Select the first char of a column](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-column-1.png)
][8]
And then using the right arrow key, select the complete column, or the whole names.
[
![Select a whole column in Vim](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-column-2.png)
][9]
**Tip**: In case you de-selected a block of text for some reason, and now want to again select it, just press 'gv' in the command mode.
# Using marks
Sometimes, while working on a large file (say, a source code file or a shell script), you might want to switch to particular location and then come back to the line where you were originally. That's not an issue if the lines in question aren't far away, or you have to do this occasionally.
But what is it's the other way round - you have to frequently jump between your present location and various far off lines in the file. Well, the solution in that case is to use marks. Just mark your current location, then come back to this location from anywhere in the file by just mentioning the name of the mark.
To mark a line in vim, use the m command followed by an alphabet that represents the name of the mark (available options are a-z in lowercase). For example, ma. Now, to come back to the mark a, use the 'a command (single quote included).
**Tip**: You can use apostrophe (`'`) or backtick `(`) depending on whether you want to jump to the beginning of the marked line, or specifically to` the line and column of the mark.
There can be various other useful applications of Vim markers. For example, you can put a mark on a line, then go to some other line and run the following command:
```
d'[mark-name]
```
 to delete everything between your current position and the marked line.
Moving on, here's an important tid-bid from the Vim's official documentation:
```
Each file has a set of marks identified by lowercase letters (a-z). In addition there is a global set of marks identified by uppercase letters (A-Z) that identify a position within a particular file. For example, you may be editing ten files. Each file could have mark a, but only one file can have mark A. 
```
So while we have discussed the basic usage of lowercase alphabets as Vim marks, how and where the uppercase letters are useful. Well, the following excerpt makes it amply clear:
```
Because of their limitations, uppercase marks may at first glance seem less versatile than their lowercase counterpart, but this feature allows them to be used as a quick sort of "file bookmark." For example, open your .vimrc, press mV, and close Vim. The next time you want to edit your .vimrc, just press 'V to open it.
```
And finally, to delete a mark, use the 'delmarks' command. For example:
```
:delmarks a
```
The aforementioned command will delete the mark a from the file. Of course, if you delete a line containing a mark, then that mark is also deleted automatically. For more information on marks, head to the [Vim documentation][11].
# Conclusion
As you start using Vim as your primary editor, features like the ones explained in this tutorial become useful tools that save a lot of your time. As you'd agree, there's not much of a learning curve involved with selection and marks features explained here - all that's required is a bit of practice.
For the complete coverage of Vim-related articles on HowtoForge, head [here][1].
--------------------------------------------------------------------------------
via: https://www.howtoforge.com/tutorial/how-to-use-markers-and-perform-text-selection-in-vim/
作者:[Himanshu Arora][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.howtoforge.com/tutorial/how-to-use-markers-and-perform-text-selection-in-vim/
[1]:https://www.howtoforge.com/tutorials/shell/
[2]:https://www.howtoforge.com/vim-basics
[3]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-example.png
[4]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-initiated.png
[5]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-working.png
[6]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-partial-lines.png
[7]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-columns.png
[8]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-column-1.png
[9]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-column-2.png
[10]:http://vim.wikia.com/wiki/Vimrc
[11]:http://vim.wikia.com/wiki/Using_marks

View File

@ -0,0 +1,113 @@
如何在 Vim 中使用标志和文本选择操作
============================================================
基于图形界面的文本或源代码编辑器,提供了一些诸如文本选择的功能。我是想说,可能大多数人不觉得这是一个功能。不过像 Vim 这种基于命令行的编辑器就不是这样。当你仅使用键盘操作 Vim 的时候,就需要学习特定的命令来选择你想要的文本。在这个教程中,我们将详细讨论文本选择这一功能以及 Vim 中的标志功能。
在此之前需要说明的是,本教程中所提到的例子、命令和指令都是在 Ubuntu 16.04 的环境下测试的。Vim 的版本是 7.4。
# Vim 的文本选择功能
我们假设你已经具备了 Vim 编辑器的基本知识(如果没有,可以先阅读[这篇文章][2])。你应该知道,'d' 命令能够剪切/删除一行内容。如果你想要剪切 3 行的话,可以重复命令 3 次。不过,如果需要剪切 15 行呢?重复 d 命令 15 次是个实用的解决方法吗?
显然不是。这种情况下的最佳方法是,选中你想要剪切/删除的行,再运行 d 命令。举个例子:
假如我想要剪切/删除下面截图中 INTRODUCTION 小节的第一段:
[![Text edited in VIM](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-example.png)][3]
那么我的做法是:将光标放在第一行的开始,(确保退出了 Insert 模式)按下 'V'Shift+v命令。这时 Vim 会开启视图模式,并选中第一行。
[![Select a line with VIM](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-initiated.png)][4]
现在,我可以使用方向键'下',来选中整个段落。
[![Select multiple lines with Vim](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-working.png)][5]
这就是我们想要的,对吧!现在只需按 'd'键,就可以剪切/删除选中的段落了。当然,除了剪切/删除,你可以对选中的文本做任何操作。
这给我们带来了另一个重要的问题:当我们不需要删除整行的时候,该怎么做呢?也就是说,我们刚才讨论的解决方法,仅适用于想要对整行做操作的情况。那么如果我们只想删除段落的前三句话呢?
其实也有相应的命令 - 只需用 'v' 来代替 'V'(不包括单引号)即可。在下面的例子中,我使用 'v' 来选中段落的前三句话:
[![Select the first three sentences in Vim](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-partial-lines.png)][6]
有时候,你需要处理的数据由单独的列组成,你的需求可能是选择特定的一列。考虑下面的截图:
[![Columns in Vom](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-columns.png)][7]
假设我们只需选择文本的第二列,即国家的名字。这种情况下,你可以将光标放在这一列的第一个字母上,按 Ctrl+v 一次。然后,按方向键'下',选中每个国家名字的第一个字母:
[![Select the first char of a column](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-column-1.png)][8]
然后按方向键'右',选中这一列。
[![Select a whole column in Vim](https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/vim-select-column-2.png)][9]
**小窍门**:如果你之前选中了某个文本块,现在想重新选中那个文本块,只需在命令模式下按 'gv' 即可。
# 使用标志
有时候,你在处理一个很大的文件(例如源代码文件或者一个 shell 脚本),可能想要切换到一个特定的位置,然后再回到刚才所在的行。如果这两行的位置不远,或者你并不常做这类操作,那么这不是什么问题。
但是,如果你需要频繁地在当前位置和一些较远的行之间切换,那么最好的方法就是使用标志。你只需标记当前的位置,然后就能够通过标志名,从文件的任意位置回到当前的位置。
在 Vim 中,我们使用 m 命令紧跟一个字母来标记一行(字母表示标志名,可用小写的 a-z。例如 ma。然后你可以使用命令 'a (包括左单引号)回到标志为 a 的行。
**小窍门**:你可以使用单引号来跳转到标志行的第一个字符,或使用反引号来跳转到标志行的特定列。
Vim 的标志功能还有很多其他的用法。例如,你可以先标记一行,然后将光标移到其他行,运行下面的命令:
```
d'[标志名]
```
来删除当前位置和标志行之间的所有内容。
在 Vim 官方文档中,有一个重要的内容:
```
每个文件有一些由小写字母a-z定义的标志。此外还存在一些由大写字母A-Z定义的全局标志它们定义了一个特定文件的某个位置。例如你可能在同时编辑十个文件每个文件都可以有标志 a但是只有一个文件能够有标志 A。
```
我们已经讨论了使用小写字母作为 Vim 标志的基本用法,以及它们的便捷之处。下面的这段摘录讲解的足够清晰:
```
由于种种局限性,大写字母标志可能乍一看不如小写字母标志好用,但它可以用作一种快速的文件书签。例如,打开 .vimrc 文件,按下 mV然后退出。下次再想要编辑 .vimrc 文件的时候,按下 'V 就能够打开它。
```
最后,我们使用 'delmarks' 命令来删除标志。例如:
```
:delmarks a
```
这一命令将从文件中删除一个标志。当然,你也可以删除标志行,这样标志将被自动删除。你可以在 [Vim 文档][11] 中找到关于标志的更多信息。
# 总结
当你开始使用 Vim 作为首选编辑器的时候,类似于这篇教程中提到的功能将会是非常有用的工具,能够节省大量的时间。你得承认,这里介绍的文本选择和标志功能几乎不怎么需要学习,所需要的只是一点练习。
你可以在 [HowtoForge][1] 上找到更多有关 Vim 的文章。
--------------------------------------------------------------------------------
via: https://www.howtoforge.com/tutorial/how-to-use-markers-and-perform-text-selection-in-vim/
作者:[Himanshu Arora][a]
译者:[Cathon](https://github.com/Cathon)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.howtoforge.com/tutorial/how-to-use-markers-and-perform-text-selection-in-vim/
[1]:https://www.howtoforge.com/tutorials/shell/
[2]:https://www.howtoforge.com/vim-basics
[3]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-example.png
[4]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-initiated.png
[5]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-working.png
[6]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-partial-lines.png
[7]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-columns.png
[8]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-column-1.png
[9]:https://www.howtoforge.com/images/how-to-use-markers-and-perform-text-selection-in-vim/big/vim-select-column-2.png
[10]:http://vim.wikia.com/wiki/Vimrc
[11]:http://vim.wikia.com/wiki/Using_marks