使vim变正常

by Amit Kulkarni

通过阿米特·库尔卡尼(Amit Kulkarni)

Since I started using Vim in 2016, I’ve discovered several lesser-known features that Vim offers out of the box without any plugins.

自2016年开始使用Vim以来,我发现了Vim提供的一些鲜为人知的功能,没有任何插件。

在开始涉猎这些新事物之前,您能否涵盖一些基础知识? (Can you cover some basics before you start rambling about these new things?)

Oh sure! Before I copy paste a few commands from a cheatsheet, I am going to make a bold assumption: you wouldn’t be reading this if you wanted a cheatsheet and you already knew Vim basics.

行,可以! 在我复制备忘单上的一些命令之前,我将做出一个大胆的假设:如果您想要一份备忘单并且您已经了解Vim的基础知识,那么您将不会阅读本文。

You might have just heard that Linux distributions ship with a default command-line text editor called Vim, and may want to just give it a try.

您可能刚刚听说Linux发行版附带了一个名为Vim的默认命令行文本编辑器, 并可能只想尝试一下。

So, let’s assume you are completely new to this whole game and start from just what we need as basics (without history/boring theory).

因此,假设您是整个游戏的新手,并从我们所需的基础知识开始(没有历史/无聊的理论)。

NOTE: If you know the basics, click here to scroll past them

注意:如果您了解基础知识, 请单击此处滚动它们

与Vim上的大量其他文章相比,您在这里的交易是什么? (What’s your deal here compared to tons of other articles on Vim?)

Most of intro articles on Vim begin with modes of Vim, inserting, saving and exit. If you’re really in the mood of theoretical perfection learning mode, feel free to read whatever helps in wikibooks.

关于Vim的大多数介绍性文章都是从Vim的模式开始的,即插入,保存和退出。 如果您真的有理论上的完善学习模式,请随时阅读Wikibook中的任何帮助。

There are also some great books and articles that tell you that there is a philosophy behind the way Vim works and that the commands in VI/Vim are meant to be combined. Absolutely true and I’m sure you will appreciate it once you get used to the editor and the power it provides.

也有一些很棒的书和文章告诉您,Vim工作方式背后有一种哲学,VI / Vim中的命令是要组合在一起的。 绝对正确,一旦您习惯了编辑器及其提供的功能,我相信您会喜欢的。

我听过有趣的故事,并看到了有关Vim学习曲线的有趣图像。 真的吗? 真的那么糟糕吗? (I have heard funny stories and seen funny images about learning curve of Vim. Is that true? Is it actually that bad?)

Well, haters gonna hate ? However, according to me, the image that somewhat gives a proper representation of Vim is:

好吧,讨厌的人会讨厌吗? 但是,根据我的看法,某种程度上可以正确表示Vim的图像是:

The majority of the articles on Vim refer to the learning *curve* as a learning *wall*, but hey, there’s some positivity: look at the other side of the wall!

Vim上的大多数文章都将学习*曲线*称为学习*墙*,但是嘿,有一些积极性:看看墙的另一面!

For beginners, it’s literally a wall since they have never done anything like this before to use an editor on command line. The thing that appealed most to me when I started as a beginner was the ubiquity of Vim.

对于初学者来说,这实际上是一堵墙,因为他们从未在命令行上使用编辑器之前做过类似的事情。 当我刚开始学习时,最吸引我的是Vim的普及。

Log in to any (non-windows) machine from any terminal and you can literally get an editor by typing *vi* with your eyes closed. The editor will appear in front of you!

从任何终端登录到任何(非Windows)计算机,您可以通过闭上眼睛键入* vi *来获得编辑器。 编辑器将出现在您的面前!

Another thing that appealed to me is the ability to work without a mouse and without wasting any productive time on touchpad or getting a mouse for laptop.

吸引我的另一件事是无需鼠标即可工作,无需在触摸板上浪费大量生产时间或无需为笔记本电脑配备鼠标即可工作的能力。

I know, I know, I can hear some of you yelling “Emacs! Emacs!” I get it. But once I was hooked to Vim, I just never really had any interest in emacs (may be because of the installation required). So, yeah emacs is also great I guess. Feel free to jump ship before you start sailing on this beautiful journey with VI(m).

我知道,我知道,我能听到一些人大喊“ Emacs! Emacs!” 我知道了。 但是一旦迷上了Vim,我对emacs就再也没有真正的兴趣了(可能是由于需要安装)。 所以,我猜emacs也很棒。 在您搭乘VI(m)踏上这一美好旅程之前,请随时跳船。

我刚打开终端,输入vi并按回车键。 我所看到的只是一个欢迎屏幕。 我无法输入,也不知道该如何摆脱。 您确定它是功能强大的编辑器吗? (I just opened my terminal and typed vi and hit return key. All i see is a welcome screen. I can’t type and I don’t know how to get out of it. Are you sure it’s a powerful editor with capabilities?)

100% sure. The behavior you just witnessed is the *wall* we saw earlier. Trust me VI(m) can do a lot of other things. It just has its own ways of using it. You can edit files, open tabs, split screen horizontally or vertically, browse file system, run linux commands without leaving your file, trigger make builds from your source code without exiting the file, bookmark directories, even better: bookmark lines of a file, find and replace words, of course copy-paste and a lot more.

100%肯定。 您刚刚看到的行为是我们之前看到的* wall *。 相信我,VI(m)还能做很多其他事情。 它只是有自己的使用方式。 您可以编辑文件,打开标签页,水平或垂直拆分屏幕,浏览文件系统,在不离开文件的情况下运行linux命令,在不退出文件的情况下触发源代码进行的构建,书签目录,甚至更好:文件的书签行,查找和替换单词,当然还有复制粘贴等等。

是的 这样,对于编辑人员而言,支持这些工作意义重大。 Me! 每个人都这样做。 有什么大不了的? (Yeah! Like that’s a big deal for an editor to support those. Meh! Everyone does that. What’s the big deal?)

There’s no big deal, the only deal I see is the ability to focus on your file/code without leaving keyboard. Really, if you don’t mind using a mouse, then go ahead and open your MS word/GUI Editor and do all the editing you wish to do.

没什么大不了的,我唯一看到的是无需离开键盘即可专注于文件/代码的能力。 确实,如果您不介意使用鼠标,那么请继续并打开MS word / GUI编辑器,然后执行所有您想做的编辑。

很公平。 但是,认真地为什么不为某些工作使用IDE? (Fair enough. But, seriously why not an IDE for some work?)

Okay, so you are a developer and have had some liking/love for an IDE. No, VI(m) is not a replacement for your shiny IDE. VI(m) does not have the out of the box awesome capabilities of your IDE. VI(m) is just small in size (package and installation) compared to the bulky IDEs and is available to use without any configuration and installations. Seriously, VI(m) is no match for some great things your IDE provides.

好的,您是一名开发人员,并且对IDE颇有喜好。 不,VI(m)不能替代您的闪亮IDE。 VI(m)没有您的IDE的出色功能。 与庞大的IDE相比,VI(m)的体积(打包和安装)很小,无需任何配置和安装即可使用。 认真地说,VI(m)无法与您的IDE提供的一些出色功能匹配。

聊够了,告诉我基础知识吗? (Enough talk, show me the basics?)

Sure, before you begin, just keep in mind that any Vim user has to basically deal with command mode and insert mode. There’s no escape (literally, not the Esc key).

当然,在开始之前,请记住,任何Vim用户都必须基本处理命令模式和插入模式。 没有转义符(从字面上讲,不是Esc键)。

Let’s say you are using some editor and you want to delete a long function in C language. The simple steps you do are: Position your cursor at the beginning of the line, then press Shift + Down arrow till end or use mouse. This action that you had to do to select those lines required you to *stop* typing and press keys. Isn’t it? Don’t tell me you were typing something and simultaneously pressed keys to magically select the body of your function.

假设您正在使用某些编辑器,并且想删除C语言中的long函数。 您执行的简单步骤是:将光标置于行的开头,然后按Shift +向下箭头直到结束或使用鼠标。 选择这些行所必须执行的此操作要求您*停止*输入并按键。 是不是 不要告诉我您在键入内容并同时按下键来神奇地选择函数的主体。

Be reasonable. You paused typing and did the selection work to tell your editor that you want to do something with this text (copy/cut/Bold/italics/anything).

合理一点 您暂停了输入并进行了选择工作,以告知您的编辑者您想要对此文本进行操作(复制/剪切/粗体/斜体/任何内容)。

This pause that you took is equivalent to being in command mode in VI(m). This is the time when you tell VI(m) that you want to do some actions on some lines/word/anything and you are not going to type. Now, VI(m) throws you out of insert mode and you are locked out of typing text in your file. Obviously, the other mode in which you can actually type in your file is the insert mode.

您采取的这种暂停等效于在VI(m)中处于命令模式。 这是您告诉VI(m)您想对某些行/单词/任何东西执行某些操作而又不想输入的时候。 现在,VI(m)使您退出插入模式,并且无法在文件中键入文本。 显然,可以在其中实际键入文件的另一种模式是插入模式。

BTW, if you were wondering how are you selecting body of function without selecting text or using mouse, I accomplish that by placing the cursor on the opening braces and using keystrokes: d%

顺便说一句,如果您想知道如何在不选择文本或不使用鼠标的情况下选择功能主体,则可以通过将光标放在左花括号上并使用击键来实现: d%

Yes, that deletes the contents of your function body. No, that’s not some creepy combination of keys to remember! d indicates you want to delete something. % is going to move the cursor to the end of the matching brace.

是的,这将删除功能主体的内容。 不,这不是要记住的令人毛骨悚然的按键组合! d表示您要删除某些内容。 %将光标移动到匹配括号的末尾。

Now, that we have established the basic modes, let’s dive into basic VI(m).

现在,我们已经建立了基本模式,让我们深入了解基本VI(m)。

If you know the name of the file you are writing:

如果您知道要写入的文件名:

$ vi myfile.c

If you are not sure of file name and want to start typing:

如果不确定文件名并要开始输入:

$ vi

As soon as you open vi, you will be in the command mode. To enter insert mode, press i . Type whatever you wish. Press Esc to return to command mode. Now you have a few options to exit depending on how you opened vi.

打开vi后,您将立即进入命令模式。 要进入 nsert模式,按i 。 键入任何您想要的。 按Esc返回命令模式。 现在,根据打开vi的方式,您有一些退出的选择。

If you gave a file name: :w will write those changes safely to disk. :q will quit the editor. You can combine these actions with : :wq and Return key

如果你给一个文件名: :w意志W¯¯仪式这些更改安全硬盘。 :qq UIT编辑器。 您可以将这些动作与:wqReturn键结合使用

If you did not give a filename: :wq filename.c will write the contents to the file filename.c and quit the editor. If you are not interested in the text you wrote and wish to exit without saving anything: :q! and you are out! The ! is required at the end to say : “Yes, I am sure I don’t want to save the contents and I want to get out urgently”

如果您没有给出一个文件名: :wq filename.c意志W¯¯仪式的内容文件filename.cQ UIT编辑器。 如果您对编写的文本不感兴趣,并希望不保存任何内容就退出:q! 你出去了! ! 最后要求说:“是的,我确定我不想保存内容,我想紧急离开”

[DEMO] Basic vim usageTo start the vim editor: Use vim command on shell To start editing a file using vim, use : vim filenameasciinema.org

[DEMO]基本的vim用法 要启动vim编辑器:在shell上使用vim命令要开始使用vim编辑文件,请使用:vim filename asciinema.org

There you go! You just created, edited and saved(or may be not) your first vi file. Congratulations ?

你去! 您刚刚创建,编辑和保存(可能不是)您的第一个vi文件。 恭喜你

As I mentioned earlier, this is not a beginner’s introduction to VI(m). There are plenty of other articles (I will provide reference at the end of article) to get started. I just inserted that intro so that you’re not disappointed after landing on this page and finding nothing to learn?

如前所述,这不是对VI(m)的初学者介绍。 还有很多其他文章(我将在文章结尾提供参考)开始。 我只是插入了该简介,以使您在进入该页面后没有失望的地方而不会感到失望?

This is the line where beginners say goodbye to intermediate users and head to the reference section for more brilliant intro articles.

这是新手向中级用户说再见的行,然后转到参考部分,获得更出色的介绍文章。

Welcome to the intermediate users. These are some cool capabilities of VI(m) that I wasn’t aware of. But now I use them daily to be more productive.

欢迎中级用户。 这些是我不知道的VI(m)的一些很酷的功能。 但是现在我每天都使用它们来提高生产力。

For those of you who prefer TL;DR:

对于那些更喜欢TL; DR的人:

  • tab-pages标签页
  • sessions会议
  • line numbers (+ marks) and copy/paste行号(+号)和复制/粘贴
  • folds褶皱
  • indention with =

    缩进=

  • insert-completion插入完成
  • netrw网络
  • splits/windows拆分/窗口
  • :! and a little bit about :make

    :! 还有一点关于:make

Vim标签页 (Vim tab-pages)

您是否提到了Vim中的标签? 我不知道那个存在! (Did you mention tabs in Vim? I didn’t know that existed!)

I know, right! A tab page is a page with one or more windows with a label (aka tab) at the top.

我知道,对吧! 标签页是在一个或多个窗口顶部带有标签(又称标签)的页面。

If you are interested in knowing more about windows, buffers, tab pages: technical details

如果您有兴趣了解有关窗口,缓冲区,标签页的更多信息 : 技术细节

Have a look:

看一看:

Steps:

脚步:

  • Open Vim with any file or just Vim: $ vim file1

    用任何文件或仅用Vim打开Vim: $ vim file1

  • Type the contents of file and get into command mode (Press Esc )

    输入文件内容并进入命令模式(按Esc )

  • :tabedit file2 , will open a new tab and take you to edit file2

    :tabedit file2 ,将打开一个新选项卡,并带您编辑file2

  • :tabedit file3 , will open a new tab and take you to edit file3

    :tabedit file3 ,将打开一个新选项卡,并带您编辑file3

  • To navigate between these tabs, you can be in normal mode and type : gt or gT to go to next tab or previous tab respectively. You can also navigate to a particular index tab (indexed from 1) using {i}gt where, i is the index of your tab. Example: 2gt takes you to 2nd tab

    要在这些选项卡之间导航,您可以进入普通模式并键入: gtgT分别转到下一个选项卡或上一个选项卡。 您还可以使用{i}gt导航到特定的索引标签(从1开始索引),其中i是标签的索引。 示例: 2gt带您进入第二个选项卡

  • To directly move to first tab or last tab, you can enter the following in command mode: :tabfirst or :tablast for first or last tab respectively. To move back and forth : :tabn for next tab and :tabp for previous tab

    要直接移至第一个选项卡或最后一个选项卡,可以在命令模式下输入以下内容:tabfirst:tablast分别用于第一个或最后一个选项卡。 来回移动:tabn用于下一个选项卡, :tabp用于上一个选项卡

  • You can list all the open tabs using : :tabs

    您可以使用:tabs列出所有打开的:tabs

  • To open multiple files in tabs: $ vim -p source.c source.h

    要在选项卡中打开多个文件: $ vim -p source.c source.h

  • To close a single tab: :tabclose and to close all other tabs except the current one: :tabonly . Use the suffix! to override changes of unsaved files

    要关闭一个标签:: :tabclose并关闭除当前一个标签之外的所有其他标签:tabonly 。 使用后缀! 覆盖未保存文件的更改

[DEMO] Tabs in VIMVIM supports tabs to open multiple files and work with themasciinema.org

VIM中的[DEMO]选项卡 VIM支持选项卡以打开多个文件并使用它们 asciinema.org

I think this feature enables us to effectively save time by sharing the buffer between tabs and enabling us to copy paste between tabs and keep multiple sessions of different tab set for category of work. Example: You can have a terminal tab with all Vim tabs of source code C files only and you can have another terminal tab with all Vim tabs of header files (.h).

我认为此功能使我们能够通过在选项卡之间共享缓冲区并在选项卡之间复制粘贴并为工作类别保留多个不同选项卡会话来有效地节省时间。 示例:您可以拥有一个仅包含源代码C文件的所有Vim选项卡的终端选项卡,并且可以拥有另一个具有头文件(.h)的所有Vim选项卡的终端选项卡。

标签提供了很多便利,可以使我的所有文件保持打开状态并在需要时访问它们。 但是,每次重新启动或关闭并打开终端时打开所有选项卡是否很痛苦? (Tabs provide so much convenience to keep all my files open and access them when I want. However, isn’t it a pain to open all tabs every time I reboot or close and open the terminal?)

Right! We all like to have our own sessions of work in which we work with a set of files and would like Vim to restore that session of tabs the way we left it.Vim allows us to save and restore those tab sessions! ✋

对! 我们每个人都希望拥有自己的工作阶段,在其中处理一组文件,并希望Vim以离开时的方式恢复该选项卡阶段。Vim允许我们保存和恢复这些选项卡会话! ✋

Steps:

脚步:

  • Open any number of tabs you wish to work with打开您希望使用的任意数量的标签页
  • From any tab, press Esc and enter the command mode

    在任何选项卡中,按Esc并进入命令模式

  • Type :mksession header-files-work.vim and hit enter

    输入:mksession header-files-work.vim并按回车

  • Your current session of open tabs will be stored in a file header-files-work.vim

    当前打开的选项卡会话将存储在文件header-files-work.vim

  • To see restore in action, close all tabs and Vim要查看实际还原,请关闭所有标签和Vim
  • Either start vim with your session using : $ vim -S header-files-work.vim or open vim with any other file and enter command mode to type: :source header-files-work.vim and BOOM! All your tabs are opened for you just the way you saved it!

    使用以下命令在您的会话中启动vim: $ vim -S header-files-work.vim或使用任何其他文件打开vim并进入命令模式键入:source header-files-work.vim和BOOM! 保存方式将为您打开所有标签!

  • If you change any session tabs (close/open new), you can save that back using : :mks! while you are in the session

    如果更改任何会话选项卡(关闭/打开新的),则可以使用:mks!将其保存回来:mks! 在会议中

[DEMO] Sessions in VIMVIM allows users to store their work sessions separately based on the projects they are working on. Users can easily…asciinema.org

VIM中的[DEMO]会话 VIM允许用户根据他们正在从事的项目分别存储其工作会话。 用户可以轻松地… asciinema.org

是否可以在不知道行号的情况下复制/剪切粘贴? (Can I copy/cut paste without having to know line numbers?)

Oh Yes! Earlier I used to see the line numbers ( :set nu ) of the functions I wanted to copy/cut. Let’s say I want to copy/cut lines 34 to 65. I used :34,65y (Copy/Yank) or :34,65d (Cut/Delete).

哦,是的! 之前我曾经看过要复制/剪切的函数的行号( :set nu )。 比方说,我要复制/剪切线34到65我用:34,65y (复制/ Y ANK)或:34,65d (剪切/ d elete)。

Of course counting the lines and using {n}yy or {n}dd (where n is number of lines) is not an option for hundreds of lines ?

当然,对行数进行计数并使用{n}yy{n}dd (其中n是行数)不是数百行的选择吗?

There can be some functions that span multiple pages and you don’t want to go down only to forget what was the first line number. There’s a simple way to achieve this without worrying anything about line numbers!

可能有一些功能跨越多个页面,您不想只想忘记第一行的编号是什么。 有一种简单的方法可以实现这一目标,而不必担心行号!

Steps:

脚步:

  • Enter normal mode, go to the start line进入普通模式,转到开始行
  • Type mk (Mark point with alphabet ‘k’ or use any other alphabet)

    键入mk (用字母“ k”标记点或使用其他任何字母)

  • Move down (page down or whatever) and move to the end line向下移动(向下翻页或其他操作)并移至结束行
  • y'k will yank/copy all the lines from start to end

    y'kÿANK /复制所有从开始到结束的线条

  • d'k will cut/delete all the lines from start to end

    d'k将剪切/ d从头到尾删除所有行

我的文件顶部有一些烦人的长函数,我不想浪费时间滚动或跳转到行。 这可能要问很多,因为这不是IDE,但是,我们有机会折叠代码块吗? (I have some annoying long functions at the top of my file and I don’t want to waste my time scrolling or jumping to lines. This may be a lot to ask because this is not an IDE but, by any chance can we fold the code blocks?)

Absolutely! Let’s say you want to skip remembering those line numbers and walk around with your new found love *the markers*. Go to the beginning of the function body and type mb . Now, just go to the end of the function body using % (brace matching) or any other convenient technique and press zf'b and you’re done!

绝对! 假设您想跳过记住这些行号,然后带着新发现的爱*标记*走动。 转到函数正文的开头,然后键入mb 。 现在,只需使用% (括号匹配)或任何其他方便的方法转到函数主体的末尾,然后按zf'b

Before and after:

之前和之后:

If you are comfortable using the line numbers, the command is even easier to remember: :5,16fo (fo stands for code fold). Once you have folded your code, it’s easy to toggle between open and closed views using zo (Open the code fold) and zc (Close the code fold). Don’t stress it so much. Just use za to toggle between open and closed folds ?

如果您习惯使用行号,则该命令甚至更容易记住:5,16fo (fo代表代码fo ld)。 折叠代码后,使用zo (打开代码折叠)和zc (关闭代码折叠)很容易在打开的视图和关闭的视图之间切换。 不要那么强调。 只需使用za在打开和关闭折叠之间切换?

Let’s say you spent considerable time folding your functions in a large file, you would obviously want to retain those folds every time you open that file right? (If not, why did you waste your energy folding them!?), so there’s a solution right in your ~/.vimrc . Insert the following lines in ~/.vimrcand your code folds are saved and restored:

假设您花了相当多的时间将功能折叠到一个大文件中,显然您希望每次打开该文件时都保留这些折叠,对吗? (否则,为什么要浪费精力折叠它们!?),所以~/.vimrc就有一个解决方案。 在~/.vimrc插入以下行,即可保存和还原代码折叠:

autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview

我通常会缩进,但有时我不得不编辑其他白痴的源代码,这使我无法编辑没有缩进的他/她的代码。 是否有任何神奇的按键操作可以做到这一点? (I’m usually careful with my indentation but sometimes, I have to edit some other idiot’s source code and it bugs me to edit his/her code without indentation. Are there any magical keystrokes to make that happen?)

Sure! It’s as simple as: =i{ . Really that’s all ! ( i is inner object)

当然! 就这么简单: =i{ 。 真的就这些! ( 是内在对象 )

[DEMO] Indentation in VIMVIM allows blocks of code to be indented with a few keystrokes. All you have to do is place the cursor in a block of…asciinema.org

[DEMO] VIM中的缩进 VIM允许通过几次按键来缩进代码块。 所有您需要做的就是将光标放在一个块上…… asciinema.org

Before-after:

前后:

All you have to do is place the cursor anywhere within a block you want to indent, press Esc to enter normal mode and then: =i{ . Boom! Your entire function body (including inner blocks) is indented.

您所要做的就是将光标放在要缩进的块中的任何位置,按Esc进入正常模式,然后: =i{ 。 繁荣! 整个函数体(包括内部块)都缩进了。

NOTE: Don’t expect indentation of your python files ?. It only works when Vim can identify the start and end using opening and closing parenthesis)

注意:不要期望python文件缩进吗? 仅在Vim可以使用左括号和右括号来标识开始和结束时才有效)

You can also increase/decrease the indentation within a block using : >;i{ to increase and <i{ to decrease in normal mode.

您还可以使用: > ; i {增加nd减少<<在正常模式下减小,在块内增加/减少缩进。

我可能在做梦,但是(*颤抖的声音*),我的意思是我只想尝试一下,嗯,我可能会为此而努力,但是(* 5秒暂停*)..没关系,让步关于我的下一个问题 (I may be dreaming but (*quavering voice*), I mean I just want to give it a try, Uhmm, I may be pushing it really far with this one but (*5 second pause*)..never mind, lets move on to my next question)

Vim is quite open-minded to take criticism or face the fact that it’s not an IDE, go ahead, let’s see what you’ve got.

Vim态度开放,可以接受批评或面对它不是IDE的事实,请继续,让我们看看您所拥有的。

嗯,对不起,但是无论如何(*喘气*)任何插件或东西,vim会像IDE一样自动完成吗? (Uhmmm, sorry but by any chance (*panting*) with any plugin or something, does vim have autocomplete like an IDE?)

? You may be surprised but yes it does! ? and guess what…* drum rolls ** drum rolls ** drum rolls ** drum rolls *

? 您可能会感到惊讶,但是可以! ? 猜猜是什么... *鼓卷**鼓卷**鼓卷**鼓卷*

Without a plugin!

没有插件!

You heard me right! The only condition for Vim to show you options is “Vim should know what you’re talking about.” It could be through an included source file or defined functions or variables.

你没听错我的话! Vim向您显示选项的唯一条件是“ Vim应该知道您在说什么。” 它可以通过包含的源文件或定义的函数或变量来实现。

All you have to do is start typing and then press Ctrl+n in insert mode.

您所需要做的就是开始输入,然后在插入模式下按Ctrl+n

Just imagine the uses! Specially if you’re writing C code and you cannot recollect the exact OpenSSL library call, all you have to do is include the header!

试想一下用途! 特别是如果您正在编写C代码并且无法重新收集确切的OpenSSL库调用,那么您所要做的就是包含标头!

[DEMO] Autocomplete feature in VIMVIM has autocomplete suggestions for keywords, function names if the appropriate header files are included or if the…asciinema.org

[DEMO] VIM中的自动完成功能如果包含适当的头文件或... asciinema.org ,VIM会为关键字,函数名称提供自动完成建议。

Let me remind again: No plugins required ?

让我再次提醒:不需要插件吗?

NOTE: The header files may be at some other locations on Mac and Vim may not be able to find them. I just use a Mac to login to a linux machine. So, if you’re using Mac, sorry about that.

注意:头文件可能在Mac上的其他位置,而Vim可能找不到它们。 我只是使用Mac登录到Linux计算机。 因此,如果您使用的是Mac,请对此表示抱歉。

我了解Vim只是一个文本编辑器,但是如果您希望我在不失去焦点的情况下工作并且不时不退出Vim,那么如果我忘记了所有文件名,该怎么办? (I understand Vim is just a text editor but if you want me to work without losing focus and without exiting Vim every now and then, what options do I have if I can’t remember all the file names?)

Simple, use the file explorer provided by VIM ? Yes, Vim provides a simple file explorer (*without any plugins*). Just type : :Explore from any Vim window and you will see an easy to navigate file explorer which can be navigated using ⬆️ and ⬇️ arrow keys. Press Enter/Return key to open a file/directory. Use :q to exit the explorer and vim. If you do not wish to quit vim and continue working with an open file, you have 3 options:

简单,使用VIM提供的文件浏览器? 是的,Vim提供了一个简单的文件浏览器(*没有任何插件*)。 只需键入::在任何Vim窗口中Explore ,您将看到一个易于浏览的文件浏览器,可以使用⬆️和⬇️箭头键进行导航。 按ENTER nter/Return键打开文件/目录。 使用: q退出浏览器和视频 。 如果您不想退出vim并继续使用打开的文件,则有3个选择:

  1. Open the explorer in a horizontal ( :Sexplore ) or vertical ( :Vexplore ) split and exit the explorer using :q

    以水平( :Sexplore )或垂直( :Vexplore )分割打开资源管理器,并使用:q退出资源管理器

  2. Open the explorer in another tabpage using :Texplore and exit using :q

    使用:Texplore在另一个标签页中打开资源管理器,然后使用:q退出

  3. Open file explorer in your current window and then unload the current buffer and delete it from the buffer list using :bdel (buffer delete).

    在当前窗口中打开文件资源管理器,然后卸载当前缓冲区,并使用:bdel (缓冲区删除)将其从缓冲区列表中删除。

NOTE: You can also use the short command :Ex to open the file explorer

注意:您还可以使用简短的命令:Ex打开文件资源管理器

有时我必须在某些行上重复相同的步骤才能编辑某些内容。 我很确定Vim将具有使我能够执行此操作的功能。 我对吗? (Sometimes I have to repeat same steps on some lines to edit something. I am pretty sure Vim will have some feature that enables me to do this. Am I right?)

100% Right! You are talking about macros and Vim supports macros. Repeating the last executed command is simple and can accomplish simple repetitive tasks. However, if the text processing is made up of several steps to achieve a result, macros come in handy.

100%对! 您在谈论宏,Vim支持宏。 重复最后执行的命令很简单,并且可以完成简单的重复任务。 但是,如果文本处理由几个步骤组成,以获得一个结果,则宏会派上用场。

Consider an example C header file :

考虑一个示例C头文件:

void encrypt_text(char *text, int bytes)
void decrypt_text(char *text, int bytes)
void process_text(char *text, int bytes)
void another_important_function(int bytes, double precision)

Oops! You forgot to put a semicolon at the end of each line and also you just realized that all these functions return an integer error code instead of void.

糟糕! 您忘记在每行的末尾添加分号,并且您刚刚意识到所有这些函数都返回整数错误代码而不是void。

The steps you need to perform for making change in one line are:

在一行中进行更改所需执行的步骤是:

  • Place the cursor at the beginning of the word void

    将光标放在单词void的开头

  • Press cw in normal mode to delete the word void and type int

    在正常模式下按cw删除单词void并输入int

  • Press Esc , move to the end of line using Shift+a to insert ;

    Esc ,使用Shift+a插入到行尾;

  • Press Esc and press ^ to return to the beginning of the edited line

    Esc ,然后按^键返回到已编辑行的开头

Resulting in:

导致:

int encrypt_text(char *text, int bytes);
void decrypt_text(char *text, int bytes)
void process_text(char *text, int bytes)
void another_important_function(int bytes, double precision)

You can just record this sequence of steps and replay it on all 4 lines.

您可以只记录此步骤序列,然后在所有4行中重播。

All you have to do is, before you start the sequence, start recording the macro in any alphabet (let’s say a) by pressing qa in normal mode. Now your steps are being recorded in a . Once you are done with all your steps, just press q in normal mode. This will end the recording. To replay these steps, just keep the cursor at the same place where it was placed during macro. Press @a and we’re done! BOOM! Vim will repeat the same steps for you on that line! To repeat it on multiple lines, you can also use @@ after using @a command once

您要做的就是在开始序列之前,在正常模式下按qa开始以任何字母(例如a )记录宏。 现在,你的脚步被记录在a 。 完成所有步骤后,只需在普通模式下按q 。 这样将结束录制。 要重播这些步骤,只需将光标放在宏过程中的同一位置即可。 按@a ,我们完成了! 繁荣! Vim将在那条线上为您重复相同的步骤! 要多行重复,也可以在使用一次@a命令后再使用@@

我知道Vim离IDE不远,我可能抱有一些不合理的希望,只是一个简单的问题:Vim是否可以远程编辑文件? (I know Vim is nowhere close to an IDE and I may be having some unreasonable hopes but just a quick question: Remote editing of files possible with Vim?)

If you think of it considering the available resources:[1] Vim[2] openssh-client (Comes installed with most Linux flavors)

如果考虑到可用资源,可以考虑以下因素:[1] Vim [2] openssh-client(大多数Linux版本都安装了该软件包)

You are in luck my friend! Yes, Vim supports remote editing of files ?Vim just utilizes the secure connection established by scp (secure copy) provided by openssh-client. There are times when you are working with files on multiple remote machines and it’s a waste of time to log into a machine just to edit one single file! You can relax in your current machine if you just know your remote machine credentials and path.

你好运,我的朋友! 是的,Vim支持文件的远程编辑?Vim仅利用openssh-client提供的scp(安全副本)建立的安全连接。 有时候,您要在多台远程计算机上使用文件,而只是为了编辑一个文件而浪费时间登录计算机! 如果您只知道远程计算机的凭据和路径,则可以在当前计算机中放松。

vim scp://remoteuser@remote_IP_or_hostname/relative/path/of/file

For example: I need to edit a file on 10.0.18.12 stored in /home/dev-john/project/src/main.c and I have login credentials for dev-john, I can access the main.c using:

例如:我需要编辑存储在/home/dev-john/project/src/main.c中的/home/dev-john/project/src/main.c上的文件,并且具有dev-john登录凭据,我可以使用以下命令访问main.c

$ vim scp://dev-john@10.0.18.12/project/src/main.c

I can use the relative path because, I can start looking for the file from the home directory of dev-john

我可以使用相对路径,因为我可以从dev-john的主目录开始查找文件

TIP: If you access a remote machine frequently, you can create an ssh config file to create a shortcut for the connection. Create a file ~/.ssh/config with

提示:如果您经常访问远程计算机,则可以创建一个ssh配置文件来创建连接的快捷方式。 使用以下命令创建文件~/.ssh/config

Host remote-dev-machineHostname 10.0.18.12User dev-johnIdentityFile ~/.ssh/id_rsa

Now you can access your file using:

现在,您可以使用以下方式访问文件:

$ vim scp://remote-dev-machine/project/src/main.c

If it’s confusing to remember the relative path and not intuitive, you can also specify it with an alternative:

如果难以记住相对路径并且不直观,则还可以使用其他方式指定它:

$ vim scp://remote-dev-machine/~dev-john/project/src/main.c

太棒了! 我已经为Vim的开箱即用功能感到兴奋。 看起来您已经解决了许多常见的编辑问题。 让我们来看看。 我有一个超过2000行的文件,并且我感兴趣的功能位于第9行,第768行和第1898行。我知道我可以使用行号跳到一行,但是我不太会记住这些数字。 有什么给我的吗? (Awesome! I’m already thrilled at the out-of-the-box capabilities of Vim. Looks like you’ve a solution to a lot of common editing problems. Let’s see. I have a file with over 2000 lines and the functions of my interest are located at line 9, line 768 and line 1898. I know I can jump to a line using line number but I’m not so good at remembering those numbers. Got anything for me?)

Hell yeah! What you’re looking for is a local bookmark solution in Vim using letters. All you have to do is :

真是的! 您正在寻找的是Vim中使用字母的本地书签解决方案。 您所要做的就是:

  • Place your cursor on any line at any position将光标放在任何位置的任何行上
  • Press Esc to make sure you’re in normal mode

    Esc以确保您处于正常模式

  • Press m{lowercaseletter} where {lowercaseletter} is any letter from a-z

    m{lowercaseletter} ,其中{lowercaseletter}是来自az任何字母

  • You just created a local bookmark to navigate in your file您刚刚创建了本地书签以浏览文件

To view all your bookmarks: Press Esc and enter command mode, type :marks and hit Enter/Return . You’ll see a list of your bookmarks. To visit any bookmark at any time, just press Esc and type `{lowercaseletter} . Kaboom! You’ll arrive at the exact same location with cursor where you bookmarked. Example:

要查看所有书签:按Esc并进入命令模式,键入:marks并按Enter/Return 。 您会看到一个书签列表。 要随时访问任何书签,只需按Esc并输入`{lowercaseletter} 。 b! 使用光标将您添加到书签的位置完全相同。 例:

I have created a local bookmark to line 21, column 18 using a . If I’m editing something on line 1783, I would just press Esc and type `a :

我一直在使用创建本地书签到第21行,列18 a 。 如果要在1783行上编辑内容,只需按Esc并输入`a

To solve your problem, all you’ve to do is create 3 local bookmarks and quickly jump to them by looking at :marks .

要解决您的问题,您要做的就是创建3个本地书签,并通过:marks快速跳转到它们。

Problem solved ?

问题解决了 ?

What if I told you that you can create global bookmarks too?! ? Yes, it is possible to create global bookmarks too! These are equivalent to your windows or GUI shortcuts (or linux soft/hardlinks) except you don’t need to create an actual link. You heard me right! You can literally jump from editing a file in /dir1 to another file and line in /project/src/ from your Vim without exiting ! ?

如果我告诉过您您也可以创建全局书签怎么办? ? 是的,也可以创建全局书签! 这些与您的Windows或GUI快捷方式(或Linux软/硬链接)等效,除了您不需要创建实际的链接。 你没听错我的话! 您可以从Vim从编辑/ dir1的文件直接跳到另一个文件,并在/ project/src/插入而无需退出! ?

Fret not, it’s not a big new thing to remember. All you have to do is:Use an uppercase letter instead of lower case letter to create a global bookmark. That’s all! Really! You navigate to the global bookmark using the same process. Example: If you’ve created a bookmark using mP , all you’ve to do is press Esc and type `P and BAM! You jump to your global bookmark (Vim remembers the path, so you don’t have to type anything about the path)

不用担心,这不是一件值得记住的新事物。 您所需要做的是:使用大写字母而不是小写字母来创建全局书签。 就这样! 真! 您使用相同的过程导航到全局书签。 示例:如果您使用mP创建了书签,则只需按Esc并输入`P and BAM! 您跳到全局书签(Vim会记住路径,因此您无需键入任何有关路径的信息)

You can access the global bookmarks in the same way as local : :marks

您可以以与local :marks相同的方式访问全局书签。

:marks
mark line  col file/textP     53    4 ~/project/src/large-file.cA     11    0 ~/project/README.md

NOTE: If you are not interested in the cursor position and just want to be there at the beginning of you bookmarked line, use 'P instead of `P (Use a single quote instead of back tick to be positioned at the beginning of the line)

注意:如果您对光标的位置不感兴趣,只想放在书签行的开头,请使用'P代替`P (使用单引号而不是反引号放置在行的开头) )

我听说Vim支持窗口拆分和选项卡! 我知道选项卡很棒,您可以一次使用多个打开的文件。 但是,分裂呢? 我为什么要那个? (I’ve heard that Vim supports window splitting along with tabs! I understand tabs are great and you get to work with multiple open files at once. But, what about splitting? Why would I want that?)

Scenarios:

场景:

  • You may want to edit a file by looking at another file simultaneously (May be you are defining a C function by looking at it’s declaration in a header file)您可能希望通过同时查看另一个文件来编辑文件(可能是通过查看头文件中的声明来定义C函数)
  • You may want to edit some portion of a file by looking at the top/bottom portion of the same file simultaneously您可能希望通过同时查看同一文件的顶部/底部来编辑文件的某些部分
  • Your work may require you to edit a file by looking at different portions of different files simultaneously您的工作可能需要您通过同时查看不同文件的不同部分来编辑文件

Vim supports splitting of screen both horizontally and vertically. Even better, you can even browse file system to open a file when you split your screen.

Vim支持水平和垂直屏幕分割。 更好的是,您甚至可以在拆分屏幕时浏览文件系统以打开文件。

Here are the available options:

以下是可用的选项:

:split filename  - split window horizontally and load filename
:vsplit file     - vertical split and open file
ctrl-w up arrow  - move cursor up a window
ctrl-w ctrl-w    - move cursor to another window (cycle)
ctrl-w _         - maximize current window vertically
ctrl-w |         - maximize current window horizontally
ctrl-w =         - make all equal size
:sview file      - same as split, but readonly
:close           - close current window

Maximizing a window for work:

最大化工作窗口:

Resizing:

调整大小:

CTRL-W [N] - Decrease current window height by N (default 1)
CTRL-W [N] +   Increase current window height by N (default 1)
CTRL-W [N] < Decrease current window width by N (default 1)
CTRL-W [N} > Increase current window width by N (default 1)

Is there a way to use file explorer while I split panes? (I can’t remember and type the file names always!)

拆分窗格时,是否可以使用文件浏览器? (我不记得总是输入文件名了!)

Of course, all you have to do is type : :Sexplore for horizontal file explorer and :Vexplore for vertical file explorer. You can also use :Vexplore! to open the file explorer on right side (instead of default left)

当然,所有你需要做的是类型: :Sexplore卧式文件浏览器和:Vexplore垂直文件浏览器。 您也可以使用:Vexplore! 打开右侧的文件浏览器(而不是默认的左侧)

Again, all this works *without any extra plugins* ?

再次说明,所有这些都有效*没有任何额外的插件*?

我正在编辑一些代码,因此我很快需要运行一个shell命令。 我应该保存工作,退出Vim并运行命令吗? 我敢打赌Vim有更好的出路 (I am in the middle of editing some code and I quickly need to run a shell command. Should I save my work, exit Vim and run my commands? I bet there is a better way out with Vim)

You bet! Vim just doesn’t want you to leave Vim and wants you to continue focussing on your work. Hence the option to execute shell commands from within your Vim. Don’t worry, all your unsaved work is not discarded, you just execute your command and BAM! you are back in your unsaved/saved file safely!

你打赌! Vim只是不想让您离开Vim,而是希望您继续专注于自己的工作。 因此,可以从Vim内部执行shell命令。 不用担心,所有未保存的工作不会被丢弃,您只需执行命令和BAM! 您已安全地返回到未保存/保存的文件中!

Let’s say you are in the middle of a coding session and you quickly need to head out to take a look at man page of file operations because you forgot the signature! You don’t have to save your work, exit Vim and then check man pages or you don’t have to open another tab just for the man page. You can issue the command from right within the Vim editor.

假设您正处于编码会话的中间,由于忘记了签名,您需要快速前往文件操作手册页看看! 您不必保存工作,退出Vim然后检查手册页,或者不必仅为手册页打开另一个选项卡。 您可以从Vim编辑器中直接发出命令。

[DEMO] Unix commands from VIMVIM allows users to execute shell commands from within VIM without exiting. All you have to do is enter the command…asciinema.org

[DEMO]来自VIM的Unix命令 VIM允许用户 从VIM 内部执行shell命令而无需退出。 您所要做的就是输入命令…… asciinema.org

Guess what! Prepare to be amazed. Vim also supports make command from within your file! All you have to do is navigate to a directory with Makefile . Open any file (Could be your source code) and make all the changes and save it. Wait, there’s no need to exit to see the compilation result. You can trigger your make build from right within Vim:

你猜怎么了! 准备惊讶。 Vim还支持从文件中执行make命令! 您所要做的就是使用Makefile导航到目录。 打开任何文件(可能是您的源代码)并进行所有更改并保存。 请稍等,无需退出即可查看编译结果。 您可以直接在Vim中触发make build:

[DEMO] Trigger make builds from vimVIM allows users to trigger make builds without exiting VIM. All we have to do is enter the command mode and type :makeasciinema.org

[DEMO]从vim触发make build VIM允许用户在不退出VIM的情况下触发make build。 我们要做的就是进入命令模式并输入:make asciinema.org

Similarly you can build other targets in your Makefile!

同样,您可以在Makefile中构建其他目标!

Example: Build directory clean up

示例:构建目录清理

I hope these cool features will help you to use Vim more productively.

我希望这些很酷的功能可以帮助您更有效地使用Vim。

Your feedback is always welcome.

随时欢迎您提供反馈。

Feel free to comment, criticize or applaud ?

随意发表评论,批评或鼓掌?

参考文献: (References:)

  • http://www.openvim.com/tutorial.html

    http://www.openvim.com/tutorial.html

  • https://linuxconfig.org/vim-tutorial

    https://linuxconfig.org/vim-tutorial

  • ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

    ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

  • http://vim.wikia.com/wiki/Tutorial

    http://vim.wikia.com/wiki/教程

  • http://www.viemu.com/a-why-vi-vim.html

    http://www.viemu.com/a-why-vi-vim.html

  • http://robertames.com/files/vim-editing.html

    http://robertames.com/files/vim-editing.html

  • https://www.youtube.com/watch?v=wlR5gYd6um0

    https://www.youtube.com/watch?v=wlR5gYd6um0

翻译自: https://www.freecodecamp.org/news/learn-linux-vim-basic-features-19134461ab85/

使vim变正常

使vim变正常_我为什么爱Vim:鲜为人知的功能使其变得如此惊人相关推荐

  1. vim block vim_我如何学会爱Vim

    vim block vim by Sanchit Gera 通过Sanchit Gera 我如何学会爱Vim (How I learned to love Vim) I've had a bitter ...

  2. vim windows版本_大概是篇Vim入门教程(1): 基本的一些东西

    虽然其实已经有了很多很好的Vim教程了... 但是... senioria也来试着写写吧... 首先, 放一些senioria当年入门的时候看的教程: vimtutor, 这个是随Vim安装的一篇小教 ...

  3. chrome正受到自动软件的控制_谷歌优化Chrome Omnibox自动完成功能 使其更加智能

    为了让 Chrome 用户更方便地导航到自己想要的网址,谷歌正计划对浏览器 Omnibox 中的自动完成功能进行优化,使其变得更加智能.谷歌将全新的自动完成体验称之为"Rich autoco ...

  4. xshell vim 不能粘贴_编辑器之神——vim的入门指南

    编辑器之神--Vim的操作说明 ©作者:geekctwd Vim命令模式: Vim虽然被称为编辑器之神,但是很多接触过的人都说入门较难,原因是什么呢,除了vim繁杂的按键之外就是因为它的模式了,很多初 ...

  5. word图片填充之后图片颜色变灰了_一文读懂如何快速实现网页页面变灰?

    灰色显示百度首页 今天早上醒来一看各大网站,为表示新冠肺炎疫情死难者的哀悼,各大网站均将页面变为灰色显示,显示的极为庄重肃穆.本文主要介绍常用网页页面快速变灰色的方法. CSS样式Filter属性 对 ...

  6. vim 全局替换_高效操作:vim 快速删除空行和注释

    说起 Linux 下的高效率编辑器,绝对少不了 vi / vim 这样灭霸级别的效率产品,无论是编辑文档.写 shell 脚本,还是 coding ,熟练使用 vi / vim 可以让你在 Linux ...

  7. linux vim 添加注释_服务器维护Linux Vim批量注释和自定义注释快捷键

    如何做好服务器维护?北京艾锑无限科技与你谈谈IT人员必须知道的服务器维护信息 服务器维护小知识使用 Vim 编辑shell脚本,在进行调试时,需要进行多行的注释,每次都要先切换到输入模式,在行首输入注 ...

  8. 不锈钢怎么变白_我家的不锈钢蒸锅的锅底为什么会变白

    地漏铜的好还是不锈钢的好,为什么呢什么原因 地漏应该是铜的比较好,为什么呢?我在这里分析给你听,你看仔细了哦~首先地漏埋在地面以下,且要求密封好,所以不能经常更换:全铜地漏镀层厚,即使时间长了生了铜锈 ...

  9. vim 编辑演示_改变我生活的7个Vim技巧(含演示)

    vim 编辑演示 Hi, fellow coders! You may have heard of the Vim code editor before, or even used it a bit. ...

最新文章

  1. Py之Beautiful Soup 4.2.0:Beautiful Soup 4.2.0的简介、安装、使用方法详细攻略
  2. 【拥抱大厂系列】百度面试官问过的 “JVM内存分配与回收策略原理”,我用这篇文章搞定了
  3. elk 搜索 语法_ELK学习笔记7| Lucence 的搜索语法
  4. 十个问题弄清JVMGC(二)
  5. Angular jasmine spied Method toHaveBeenCalled的执行原理
  6. 微软宣布公开预览Dev Spaces for AKS
  7. 一个人的战斗---走出软件作坊:三五个人十来条枪 如何成为开发正规军(十九)[转]...
  8. Shell.ItemTemplate
  9. Bailian4072 判断多个点是否在同一直线【数学】
  10. MITRE Implementation of ISO 12233 Spatial Frequency Response (SFR)
  11. 金融领域的数字化转型和科技创新有哪些应用?
  12. Mac OS X 平台有哪些优秀应用可以将视频转成 GIF?Mac视频转gif软件推荐
  13. python mysql插入数据报错:TypeError: %d format: a number is required, not str
  14. 《决战Linux到精通》,你的Linux水平将从入门到入魔「建义收藏」
  15. 学习编程到底用不用记笔记?
  16. 处理Whitelabel Error Page
  17. 接口测试平台代码实现48: 自动异常测试-1
  18. python毒酒检测_Turkey HSD检验法/W法
  19. AWS 中文入门开发教学 25- 高可用性设计 - 建立 ALB 负载均衡
  20. 关于售前、售后、产品、市场、销售几个部门职能的理解

热门文章

  1. ORA-00942: 表或视图不存在解决方法
  2. 00942 ora 表存在_ORA-00942: 表或视图不存在 的原因和解决方法
  3. 如何在COCOS2D中绘制3d椭圆柱?
  4. Cocos2dx-Lua 利用网格Grid实现3D的景深效果显示
  5. 网络综合布线及网络硬件补充知识
  6. 携职教育:拿到一级造价工程师证书后,如何利用好它“赚大钱”
  7. 彻底关闭数据执行保护(DEP)
  8. Android Audio代码分析(4): audiohalservice 启动
  9. 【2023年13万字】JavaScript面试题看这一篇就够了
  10. Centos使用parted分区