# 使vim变正常_我为什么爱Vim：鲜为人知的功能使其变得如此惊人

by 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.

#### 在开始涉猎这些新事物之前，您能否涵盖一些基础知识？ (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.

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.

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

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.

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学习曲线的有趣图像。 真的吗？ 真的那么糟糕吗？ (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:

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.

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!

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).

#### 我刚打开终端，输入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.

#### 很公平。 但是，认真地为什么不为某些工作使用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.

#### 聊够了，告诉我基础知识吗？ (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).

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.

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.

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%

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.

Now, that we have established the basic modes, let’s dive into basic 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.

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

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”

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

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?

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.

For those of you who prefer 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中的标签？ 我不知道那个存在！ (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 。 使用后缀! 覆盖未保存文件的更改

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).

#### 标签提供了很多便利，可以使我的所有文件保持打开状态并在需要时访问它们。 但是，每次重新启动或关闭并打开终端时打开所有选项卡是否很痛苦？ (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! ✋

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! 在会议中

#### 是否可以在不知道行号的情况下复制/剪切粘贴？ (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).

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 ?

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!

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 ?

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:

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)

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.

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)

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

#### 我可能在做梦，但是(*颤抖的声音*)，我的意思是我只想尝试一下，嗯，我可能会为此而努力，但是(* 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.

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

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!

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.

#### 我了解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:

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

#### 有时我必须在某些行上重复相同的步骤才能编辑某些内容。 我很确定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 :

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.

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.

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

#### 我知道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)

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 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:

$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-machine Hostname 10.0.18.12 User dev-john IdentityFile ~/.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 :

• 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:

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 :

To solve your problem, all you’ve to do is create 3 local bookmarks and quickly jump to them by looking at :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 ! ?

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)

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

:marks
mark line  col file/text
P     53    4 ~/project/src/large-file.c
A     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)

#### 我听说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)

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!

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.

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:

Similarly you can build other targets in your Makefile!

Example: Build directory clean up

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

Feel free to comment, criticize or applaud ?

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

05-17 1045
07-15 7129

06-08 58
05-23 2237
04-15 125
05-24 472
06-10 1849
10-12 47
10-14 706
10-17 49
10-19 30