A Byte Of Vim cn:移动光标

出自Geckos wiki

跳转到: 导航, 搜索

目录

简介

Once you've written the initial text, editing and rewriting requires a lot of movement between the various parts of the document. For example, you're writing a story and you suddenly get an idea for a new plot, but to develop this plot you need to go back to the part where the protagonist enters the new city (or something like that)... how do you quickly move around the text so that you don't lose your train of thought?

一旦你写了一些初期的文字,对于编辑和重写来说需要在文档的各个部分中进行大量的移动。例如,你在写一部小说的时候突然想到了一个新的情节,但是要发展这段情节必须回到主角到达那个新城市的时候(或类似的情况)... 你如何快速地在文本间移动以不至于丢失思路?

Let's see a few examples of how Vim makes this fast.

让我们看一些例子,看看Vim是如何快速地做到的。

  • Want to move the cursor to the next word? Press w.
  • Want to move to to the next paragraph? Press }.
  • Want to move to the 3rd occurrence of the letter 'h'? Press 3fh.
  • Want to move 35 lines downwards? Press 35j.
  • After one of the above movements, want to jump back to the previous location? Press ctrl-o.
  • Want to learn how all these work? Let's dive in.
  • 想把光标移动到下一个单词? 按下 w
  • 想移动到下一个段落? 按下 }
  • 想移动到第三个'h'字母出现的地方? 按下 3fh
  • 想往下移动35行? 按下 35j
  • 在以上某一个移动操作后,想跳回之前所在的位置? 按下 ctrl-o
  • 想知道这些是如何工作的?让我们深入学习吧。


First, open a file called chandrayaan.txt and type the following text (taken from Wikipedia):

首先,打开一个叫 chandrayaan.txt 的文件,并且输入以下文字(取自 Wikipedia)。

Chandrayaan-1 is India's first mission to the moon. Launched by India's national space agency the Indian Space Research Organisation (ISRO). The unmanned lunar exploration mission includes a lunar orbiter and an impactor. The spacecraft was launched by a modified version of the PSLV XL on 22 October 2008 from Satish Dhawan Space Centre, Sriharikota, Andhra Pradesh at 06:23 IST (00:52 UTC). The vehicle was successfully inserted into lunar orbit on 8 November 2008. The Moon Impact Probe was successfully impacted at the lunar south pole at 20:31 hours on 14 November 2008.

The remote sensing satellite had a mass of 1,380 kilograms (3,042 lb) at launch and 675 kilograms (1,488 lb) at lunar orbit and carries high resolution remote sensing equipment for visible, near infrared, and soft and hard X-ray frequencies. Over a two-year period, it is intended to survey the lunar surface to produce a complete map of its chemical characteristics and 3-dimensional topography. The polar regions are of special interest, as they might contain ice. The lunar mission carries five ISRO payloads and six payloads from other international space agencies including NASA, ESA, and the Bulgarian Aerospace Agency, which were carried free of cost.

用Vim的方式移动光标

The most basic keys that you should use are the 'hjkl' keys. These 4 keys correspond to the left, down, up and right arrow keys respectively. Notice these keys are situated directly under your right hand when they are placed on the home row.

你要用到的最基本的按键是'hjkl'键。这4个按键分别对应与 左, 下, 上 和 右。注意当你的右手放在起始行的时候,这些按键就在你的右手下面。

But why not use the arrow keys themselves? The problem is that they are located in a separate location in the keyboard and it requires as much hand movement as it requires to use a mouse.

但是为啥不去用方向键呢?问题在于方向键位于键盘的另一个区域,要移动到那里就如同要使用鼠标一样麻烦。

Remember, that the right hand fingers should always be placed on jkl; keys (and the thumb on the space bar). Now, let's see how to use these 4 keys:

要牢记,右手的手指应该总是放在 jkl; 按键上(大拇指放在空格键上)。现在,我们来看看如何使用这4个按键:

Using h,j,k,l instead of arrow keys
h You have to stretch your index finger (which is on 'j') to the left to press the 'h'. This is the left-most key and signifies going left.

你必须伸出食指(位于'j'键上)去按'h'键。这是这4个键中最左边的一个并且意味着向左。

j The drooping 'j' key signifies going down.
'j' 键的形状表示向下。
k The upward pointing 'k' key signifies going up.
向上指的 'k' 键表示向上。
l The right-most 'l' key signifies going right.
最右边的 'l' 键表示向右。

Note that we can repeat the operation by prefixing a count. For example, 2j will repeat the j operation 2 times.

注意我们可以使用一个计数前缀来重复操作。比如, 2j 会重复 j 的操作 2 次.

Open up the chandrayaan.txt text document and start practicing these keys:

打开 chandrayaan.txt 文本文件并且开始练习使用这些按键:


  • Position your cursor at the first letter 'C' of the document.
  • Press 2j and it should skip the current long line, the blank line and go to the second line, i.e., second paragraph.
  • Press 2k to get back to where we were. Or alternatively, press ctrl-o to jump back.
  • Press 5l to move 5 characters to the right.
  • Press 5h to move left by 5 characters. Or alternatively, press ctrl-o to jump back.
  • 把你的光标定位到文档的第一个字母 'C' 上。
  • 按下 2j, 光标应该跳过当前这长长的一行和一个空行,到达了第二行,或者说是 第二个段落。
  • 按下 2k 回到之前的位置。 或者,按 ctrl-o 跳回去也可以。
  • 按下 5l 向右移动5个字符。
  • 按下 5h 向左移动5个字符。 或者按 ctrl-o 跳回去。

Make it a habit to use the 'hjkl' keys instead of the arrow keys. Within a few tries, you'll notice how much faster you can be using these keys.

养成用 'hjkl' 键来替代用方向键的习惯。通过一些尝试,你就会发现使用这些按键有多快了。

Similarly, there are more simple keys that replace the following special movements. Notice that this again is intended to reduce hand movement. In this particular case, people are prone to searching and hunting for these special keys, so we can avoid that altogether.

类似的,还有一些简单的按键替换了特殊的移动按键。请注意这样重复设计的意义是为了减少复杂的移动。在这种情况下,人们很容易为了这些特殊键去查找和探寻,因此,我们可以完全避免这种情况。(译:没明白 - -)

传统按键 Vim
'home' key moves to the start of the line
'home'键用于移动到行首
^ key (think 'anchored to the start')
^ 键 (想象成‘指向开头’)
'end' key moves to the end of the line
'end' 键用于移动到行尾
$ key (think 'the buck stops here')
$ 键 (想象成'the buck stops here'[译:没明白,读者网上查一下吧,应该就是停止的意思] )
'pgup' key moves one screen up
'pgup' 键用于向上移动一屏
ctrl-b which means move one screen 'b'ackward
ctrl-b 意思是回滚('b'ackward)一屏
'pgdn' key moves one screen down
'pgdn' 用于向下移动一屏
ctrl-f which means move one screen 'f'orward
ctrl-f意思是向前('f'orward)移动一屏

If you know the absolute line number that you want to jump to, say line 50, press 50G and Vim will jump to the 50th line. If no number is specified, G will take you to the last line of the file. How do you get to the top of the file? Simple, press 1G. Notice how a single key can do so much.

如果你想跳到指定的行号,比如第50行,按下 50G , Vim就会跳到第50行。如果不指定行号, G 键会跳到文件的最后一行。那你如何跳到文件的第一行呢?简单阿,按下 1G。发觉一个键可以做那么多事阿。

  • Move the cursor to the first line by pressing 1G.
  • Move 20 characters to the right by pressing 20l.
  • Move back to the first character by pressing ^.
  • Jump to the last character by pressing $.
  • Press G to jump to the last line.
  • 按下 1G 将光标移动到第一行。
  • 按下 20l 向右移动20个字符。
  • 按下 ^ 移动到第一个字符。
  • 按下 $ 跳到最后一个字符。
  • 按下 G 跳到最后一行。

What if you wanted to the middle of the text that is currently being shown in the window?

如果你想移动到当前窗口中显示文字的中间呢?

  • Press H to jump as 'h'igh as possible (first line of the window)
  • Press M to jump to the 'm'iddle of the window
  • Press L to jump as 'l'ow as possible (last line being displayed)
  • 按下 H 跳到最高('h'igh)处 (窗口中的第一行)
  • 按下 M 跳到窗口的中间('m'iddle)
  • 按下 L 跳到最低('l'ow)处 (所显示文字的最后一行)


You must have started to notice the emphasis on touch-typing and never having to move your hands off the main area. That's a good thing.

你一定已经注意到了,你的手在打字的时候从未离开过键盘的主要区域。这是件好事。

单词,语句,段落

We have seen how to move by characters and lines. But we tend to think of our text as words and how we put them together - sentences, paragraphs, sections, and so on. So, why not move across such text parts, i.e., "text objects"?

我们已经知道如何在字符和行之间移动了。But we tend to think of our text as words and how we put them together - sentences, paragraphs, sections, and so on. 那么我们为什么不在文本的某些部分直接移动呢?比如 “文本对象”。

Let's take the first few words from our sample text:

从我们的样例文本里拿出一些单词

The polar regions are of special interest, as they might contain ice.

First, let's position the cursor on the first character by pressing ^.

首先,我们按下 ^ 键,然光标移动到第一个字符上。

[T]he polar regions are of special interest, as they might contain ice.

Note that we are using the square brackets to mark the cursor position.

注意,这里我们用方括号表示光标位置

Want to move to the next 'w'ord? Press w. The cursor should now be at the 'p' in 'polar'.

想要移动到下一个单词('w'ord))的时候,按下 w。这时候光标应该就会在 'polar' 的 'p' 上了。

The [p]olar regions are of special interest, as they might contain ice.

How about moving 2 words forward? Just add the prefix count to 'w': 2w.

那么要往前移动到2个单词呢?只要在 'w' 前面加上移动的次数: 2</w>。

The polar regions [a]re of special interest, as they might contain ice.

Similarly, to move to the 'e'nd of the next word, press <tt>e</tt>.

类似的,如果要移动到下一个单词的末尾('e'nd),按下<tt>e

The polar regions ar[e] of special interest, as they might contain ice.

To move one word 'b'ackward, press b. By prefixing a count, 2b will go back by 2 words.

要往回('b'ackward)移动到一个单词,按下b。加上一个次数,2b 就会往回移动到2个单词的位置。

The polar [r]egions are of special interest, as they might contain ice.

See :help word-motions for details.

输入 :help word-motions 查看详情。

We have seen character motions and word motions, let's move on to sentences.

我们已经知道了在字符和单词间移动,来看下如何在语句上移动。

[C]handrayaan-1 is India's first mission to the moon. Launched by India's national space agency the Indian Space Research Organisation (ISRO). The unmanned lunar exploration mission includes a lunar orbiter and an impactor. The spacecraft was launched by a modified version of the PSLV XL on 22 October 2008 from Satish Dhawan Space Centre, Sriharikota, Andhra Pradesh at 06:23 IST (00:52 UTC). The vehicle was successfully inserted into lunar orbit on 8 November 2008. The Moon Impact Probe was successfully impacted at the lunar south pole at 20:31 hours on 14 November 2008.

Position the cursor at the first character (^).

将光标放到第一个字符上 (^)。

To move to the next sentence, press ).

要移动到下一句话的的时候,按 )

Chandrayaan-1 is India's first mission to the moon. [L]aunched by India's national space agency the Indian Space Research Organisation (ISRO). The unmanned lunar exploration mission includes a lunar orbiter and an impactor. The spacecraft was launched by a modified version of the PSLV XL on 22 October 2008 from Satish Dhawan Space Centre, Sriharikota, Andhra Pradesh at 06:23 IST (00:52 UTC). The vehicle was successfully inserted into lunar orbit on 8 November 2008. The Moon Impact Probe was successfully impacted at the lunar south pole at 20:31 hours on 14 November 2008.

Isn't that cool?

是不是很酷?

To move to the previous sentence, press (.

移动到上一句话,按 (

Go ahead, try it out and see how fast you can move. Again, you can prefix a count such as 3) to move forward by 3 sentences.

继续,试试看你能移动的多快。童言,你可以加上次数,类似 3) 来向前移动3句话。

Now, use the whole text and try out moving by paragraphs. Press } to move to the next paragraph and { to move to the previous paragraph.

现在使用整个文本并且尝试在段落间移动。按下 } 移动到下一段,并且 { 移动到上一段。

Notice that the 'bigger' brackets is for the bigger text object. If you had already noticed this, then congratulations, you have already started to think like a winner, err, "think like a Vimmer".

注意,这 用'大'括号的意义是 为大段的文本对象。如果你已经意识到这点,那么恭喜你,你已经开始以一个vim使用者的思维方式思考了。

Again, don't try to remember these keys, try to make it a habit such that your fingers naturally use these keys.

再次提醒,不要死记硬背这些按键,试着让你的手指去习惯性的使用这些按键。

See :help cursor-motions for more details.

输入 :help cursor-motions 查看更多详情。

Make your mark

You are writing some text but you suddenly remember that you have to update a related section in the same document, but you do not want to forget where you are currently so that you can come back to this later. What do you do?

Normally, this would mean scrolling to that section, update it, and then scroll back to where you were. This is a lot of overhead and we may tend to forget where we were last at.

We can do things a bit smarter in Vim. Move the cursor to the 5th line in the following text (the words by John Lennon). Use ma to create a mark named 'a'. Move the cursor to wherever you want, for example 4j.

I am eagerly awaiting my next disappointment. —Ashleigh Brilliant

Every man’s memory is his private literature. —Aldous Huxley

Life is what happens to you while you’re busy making other plans. —John Lennon

Life is really simple, but we insist on making it complicated. —Confucius

Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment. —Buddha

The more decisions that you are forced to make alone, the more you are aware of your freedom to choose. —Thornton Wilder

Press 'a (i.e., single quote followed by the name of the mark) and voila, Vim jumps (back) to the line where that mark was located.

You can use any alphabet (a-zA-Z) to name a mark which means you can have up to 52 named marks for each file.

Jump around

In the various movements that we have learned, we might often want to jump back to the previous location or to the next location after a movement. To do this, simply press ctrl-o to jump to the previous location and ctrl-i to jump forward to the next location again.

Parts of the text

There are various ways you can specify text objects in Vim so that you can pass them to a command. For example, you want to visually select a part of the text and then convert the case (from upper to lower or from lower to upper case) of the text using the ~ key.

Open the dapping.txt file that we created in previous chapters. Use the various keys to move to the first letter of the word 'dapper' in the second paragraph. Hint: Use }, j, w.

Dapping means being determined about being determined and being passionate about being passionate.

Be a dapper.

Press v to start the visual mode, and press ap to select 'a' 'p'aragraph. Press ~ to flip the case of the text. If you want to cancel the selection, simply press <Esc>.

Dapping means being determined about being determined and being passionate about being passionate.

bE A DAPPER.

Other text object mnemonics are aw which means 'a' 'w'ord, a" means a quoted string (like "this is a quoted string"), ab means 'a' 'b'lock which means anything within a pair of parentheses, and so on.

See :help object-motions and :help text-objects for more details.

Summary

We have seen the rich number of methods that Vim gives us to move around the text. It is not important to remember each of these movements, it is more important to make them a habit whenever you can, especially the ones that are most relevant to you, and when they become a habit they reduce the movement of your hands, you become faster, and ultimately spend more time on thinking about your writing rather than on the software you use to write.

See :help various-motions as well as :help motion for more interesting ways of movement.


 A Byte Of Vim 中文版,由geckos团队翻译。
 本章原文地址: http://www.swaroopch.com/notes/Vim_en:Moving_Around
个人工具
名字空间
变换
动作
导航
工具箱