vim block vim_如何不再害怕Vim

淳于新
2023-12-01

vim block vim

If you’ve ever used Vim, you know how difficult it can get to reach the same speed as in a “normal” GUI editor. But once you do, the payoff is exponential — you get much more efficient at writing code. Of course, this isn’t the main reason for this post.

如果您曾经使用过Vim,那么您将知道达到与“常规” GUI编辑器相同的速度是多么困难。 但是,一旦这样做,收益是成倍的—您可以更高效地编写代码。 当然,这不是这篇文章的主要原因。

The majority of the time goes — or should go — into designing a solution to your problem, not actually coding it; so optimizing the how-fast-you-code bit seems like the last thing to focus on. It’s not a leverage point.

大多数时间(或应该去)用于设计问题的解决方案,而不是实际编码。 因此优化您的编码速度似乎是最后要关注的事情。 这不是一个杠杆点。

This post took shape because of another reason: the non-existence of GUIs on SSH’ed machines. What are you going to do now? Any machine where you have access to the terminal and you want to edit a file — You have 2 options:

这篇文章之所以成形,是由于另一个原因:SSH计算机上不存在GUI。 你现在要做什么? 您可以访问终端并要编辑文件的任何计算机-您有2个选项:

  1. Give up, try to close Vim

    放弃,尝试关闭Vim
  2. Master Vim

    Vim大师

Just making a copy of vimtutor wouldn’t help anyone. So here I’m using a different approach: I aggregated the best bits of vim that I use in everyday life as a software developer, along with mnemonics to remember the stuff by. This contains almost everything you need for regular editing and writing.

仅仅复制一份vimtutor并不会帮助任何人。 因此,在这里我使用了另一种方法:我汇总了作为软件开发人员在日常生活中使用的vim的最佳片段,以及用于记忆这些东西的助记符。 它包含了常规编辑和编写所需的几乎所有内容。

基础 (The basics)

Stick with me, this isn’t about the commands but the ideology behind them!

坚持我,这与命令无关,而是其背后的意识形态!

模式 (Modes)

Vim has 2 modes:

Vim有2种模式:

  • Normal ( Command mode)

    普通(命令模式)
  • Insert ( Edit mode )

    插入(编辑模式)

When you open Vim, you start in normal mode. To enter back into normal mode at anytime, press the ESC key. Normal mode is where you can issue commands: the list of commands is endless!

打开Vim时,将以普通模式启动。 要随时返回正常模式,请按ESC键。 在普通模式下,您可以发出命令:命令列表无穷无尽!

There are a lot of ways to enter insert mode. The most intuitive one is to use the i command. i for insert. In normal mode, press i and you’ll enter insert mode. Now anything you type will show up in the editor. One of the biggest hurdles, sorted.

有很多方法可以进入插入模式。 最直观的一种是使用i命令。 i要插入。 在普通模式下,按i ,您将进入插入模式。 现在,您键入的任何内容都会显示在编辑器中。 最大的障碍之一,排序。

The mental model — to understand the idea: Since there is no GUI, there’s no concept of mouse clicks. There’s no menu to choose options from, there’s no right click. Hence, you need a way to get all this on the keyboard — your only input source.

思维模型-理解这个想法:由于没有GUI,因此没有鼠标单击的概念。 没有菜单可供选择,没有右键单击。 因此,您需要一种将所有这些内容输入键盘的方法-您唯一的输入源。

Having two modes achieves this! You can think of the Normal ( Command) mode as the Menu bar and mouse on steroids, while the Insert mode is like the normal mode in GUI editors ( where what you type shows up on the screen ).

有两种模式可以实现这一目标! 您可以将“普通”(命令)模式视为类固醇上的菜单栏和鼠标,而“插入”模式则类似于GUI编辑器中的普通模式(您键入的内容将显示在屏幕上)。

(Words)

To Vim, words mean almost the same as what they mean to us — A set of characters separated by whitespace or special characters. The command is w.

对Vim而言,单词的含义几乎与我们的含义相同-一组由空格或特殊字符分隔的字符。 命令是w

命令解剖 (Command Anatomy)

Commands in Vim follow a set pattern. Knowing this would help put each command into a certain bucket of commands, thus building a better mental model for the same.

Vim中的命令遵循设定的模式。 知道这一点将有助于将每个命令放入特定的命令桶中,从而为相同的命令建立更好的思维模型。

Commands look like this:

命令如下所示:

[action] <number> [motion]

[动作] <数字> [动作]

The action is what you want to do,

动作就是你想做的,

The number is how many times you want to do that action,

该数字是您要执行该操作的次数,

The motion is the range of that action.

运动是该动作的范围。

The motion is the scope. An example would make this clearer. Let us say, we want to delete the next 3 words, starting from the cursor. Here, the action is delete, the number is 3 and the motion is a word. The command action for delete is d.

议案是范围。 一个例子可以使这一点更加清楚。 让我们说,我们要从光标开始删除接下来的3个单词。 此处,动作为删除,数字为3,动作为单词。 删除的命令操作是d

Hence, we get the final command as: d3w — delete the next 3 words.

因此,我们得到的最终命令为: d3w —删除接下来的3个单词。

Omitting the number defaults to once.

省略数字默认为一次。

Motions can be used without an action, which defaults to navigation. Hence, typing w in command mode would move the cursor forward one word.

无需动作即可使用动作,动作默认为导航。 因此,在命令模式下键入w将使光标向前移动一个单词。

We are well equipped now to start learning about the commands themselves (and a range of motions to use with them)

我们现在已经准备好开始学习命令本身(以及与它们一起使用的一系列动作)的知识。

有用的命令 (Useful commands)

如何关闭Vim (How to close Vim)

First things first, we don’t want to get stuck in Vim land without having an exit plan. Always have an exit strategy.

首先,我们不想在没有退出计划的情况下陷入Vim领域。 始终有退出策略。

:q to quit

:q退出

:q! to force quit

:q! 强迫戒烟

:wq to save and quit

:wq保存并退出

命令动作 (Command Actions)

d : delete

d :删除

i : insert

i :插入

p : put / paste

p :放置/粘贴

y : yank / copy

y :拉动/复制

x : cut

x :切

u : undo

u撤消

di: delete inside*, yi : yank inside*

di :删除内部*, yi :删除内部*

v : visual / selection

v :视觉/选择

/ : search

/ :搜索

% : parentheses matching, developers rejoice!

% :括号匹配,开发人员很高兴!

:s : substitute! In other words, find-replace on steroids

:s :替代! 换句话说,找到替代类固醇

Since actions on the entire line are very frequent, the developers of vim created a new shorthand for them — omitting the need to add a motion. Repeat the action to apply on the entire line. For example:

由于整个生产线上的动作非常频繁,因此vim的开发人员为他们创建了新的速记-无需添加动作。 重复该操作以应用于整行。 例如:

To delete the current line: dd

删除当前行: dd

To copy the current line: yy

复制当前行: yy

Neat, ain’t it?

整洁,不是吗?

指令动作 (Command motions)

Motions go with actions as we have seen, and the available motions changes with the action. However, some motions are pretty uniform.

如我们所见,动作与动作并存,可用动作随动作而变化。 但是,某些动作非常统一。

w : beginning of next word (we’ve seen this before!)

w :下一个单词的开头(我们之前已经看过!)

e : ending of current word

e :当前单词的结尾

b : beginning of previous word

b :上一个词的开头

Arrow keys / <h,j,k,l> : respective motions. h,j,k,l are a substitute to the arrow keys, and the source of speed in Vim: You don’t have to move your hands away from the typing part of the keyboard.

箭头键/ <h,j,k,l>:各自的动作。 h,j,k,l可以替代箭头键,也可以代替Vim中的速度来源:您不必将手从键盘的打字部分移开。

$ : end of line

$ :行尾

0 : beginning of line

0 :行首

G : end of file

G :文件结尾

nG : jump to line number n

nG :跳转到第n

) : jump forward a sentence

) :向前跳一个句子

} : jump forward a paragraph

} :向前跳一个段落

This helps visualise better:

这有助于更好地可视化:

ge      b          w                             e
           <-     <-         --->                          --->
    This is-a line, with special/separated/words (and some more). ~
       <----- <-----         -------------------->         ----->
         gE      B                   W                       E

Armed with these actions and motions, we can create most of the basic commands in Vim. Here’s a list of 8 every day functions. Figure out the command to make them happen!

有了这些动作和动作,我们就可以在Vim中创建大多数基本命令。 这是每天8个功能的列表。 找出使它们发生的命令!

  1. Delete the next 3 lines ( including current line)

    删除接下来的3行(包括当前行)
  2. Copy current word — cursor is at beginning of word

    复制当前单词-光标在单词的开头
  3. Copy current word — it has special characters — cursor is in the middle of the word

    复制当前单词-它具有特殊字符-光标位于单词中间
  4. Navigate down 10 lines

    向下浏览10行
  5. Delete everything inside the curly brackets

    删除大括号内的所有内容
  6. Go up 2 paragraphs

    上2段
  7. Paste the previously selected text 5 times.

    将先前选择的文本粘贴5次。
  8. Edit where the cursor is present: “I can Vim now!”

    编辑光标所在的位置:“我现在可以Vim!”

Wait for it…

等等...

Here are the solutions:

以下是解决方案:

  1. d3j

    d3j

  2. yw

    yw

  3. yiW : the yank inside is to yank inside, and W is inside what to yank ( current word). This is amazingly useful, you can use all sorts of combinations with inside !

    yiW :内部的yiW是内部的yiWW在内部的yiW是什么(当前单词)。 这非常有用,您可以将各种组合与inside

  4. 10j

    10j

  5. di} : just like #3.

    di} :就像#3。

  6. 2{ : this was slightly more intuitive. { is to go up a para, } is to go down a para

    2{ :这有点直观。 {是上一段, }是下一段

  7. 5p : remember the optional numbers from the command anatomy? They can be used almost anywhere

    5p :还记得命令解剖中的可选数字吗? 它们几乎可以在任何地方使用

  8. i I can Vim now! : i is to go into insert mode, then you can work like in a “normal” editor

    i I can Vim now!i将进入插入模式,然后您可以像在“常规”编辑器中一样工作

Alright, you made it! Congratulations, this is enough Vim to go about exploring on your own, adventurer. The tutorial is over. Good luck.

好吧,您做到了! 恭喜,这足以让Vim独自冒险。 本教程已结束。 祝好运。

If someone asks you about Vim, you can do better than using this meme. Explain it to them, or point them here ;)

如果有人问您有关Vim的问题,您可以比使用此模因做得更好。 向他们解释,或将其指向此处;)

*Note: This is my model of Vim. It isn’t exactly how things work internally. If you look at the documentation ( :help user-manual ), you’ll see that d is the command, and the motion is iw, or “inside word”. There’s a deviation.

*注意:这是我的Vim模型。 这并不是内部如何运作。 如果查看文档(:help user-manual),您会看到d是命令,并且运动是iw或“内部单词”。 有偏差。

奖金 (Bonus)

Here are some extra commands that come in handy:

以下是一些有用的额外命令:

分屏 (Split screen)

:vsplit <filename>

:vsplit <filena >

Creates a vertical splitting. Enables you to copy-paste from one screen to another.

创建垂直分割。 使您可以从一个屏幕复制粘贴到另一个屏幕。

To cycle between screen-splits: <ctrl-w> &lt;ctrl-w>

要在屏幕分割之间循环: <ctrl-w> &l t; ctrl-w>

Alternatively, you could use the arrow keys / hjkl like so:

另外,您可以像这样使用箭头键/ hjkl

<ctrl-w> h to go to previous screen.

<ctrl-w > h转到上一个屏幕。

You can close windows as you normally do (:q), or —

您可以像平常一样关闭窗口( :q ),或者-

:only — to close all other windows

:only —关闭所有其他窗口

多个标签 (Multiple Tabs)

:tabnew <filename> opens a new file in a new tab on Vim

:tabnew <filena me>在Vim的新标签页中打开一个新文件

:tabn to go to next tab ( or :gt )

:tabn转到下一个标签(或:gt )

:tabp to go to previous tab ( or :gT )

:tabp转到上一个标签(或:gT )

Again, you can copy-paste from one tab into another.

同样,您可以从一个选项卡复制粘贴到另一个选项卡。

You can map this combination to a key of your choice too!

您也可以将此组合映射到您选择的键!

I tried <Ctrl-Tab>, but that’s already reserved. What a bummer. Instead, we have:

我尝试了<Ctrl-T ab>,但是已经保留了。 真可惜 相反,我们有:

map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>

Which means <Ctrl-t> followed by the left or right arrow key would let you switch between tabs.

这意味着<CTRL-T>接着b y th ë勒ft or右箭头键将让你切换标签。

How exactly did I come up with this mapping? Check out this tutorial.

我是如何精确想到这个映射的? 查看本教程

Other stories in this series:

本系列中的其他故事:

Enjoyed this? Don’t miss a post again — subscribe to my mailing list!

喜欢这个吗? 不要再错过任何帖子-订阅我的邮件列表!

翻译自: https://www.freecodecamp.org/news/how-not-to-be-afraid-of-vim-anymore-ec0b7264b0ae/

vim block vim

 类似资料: