简体   繁体   English

如何在终端中使用 vim?

[英]How to use vim in the terminal?

How does one setup and start using vim in the terminal on OS X?如何在 OS X 的终端中设置并开始使用 vim?

I want to start writing my C code using vim in the terminal rather than a separate text editor.我想在终端而不是单独的文本编辑器中使用 vim 开始编写我的 C 代码。 How does one get started on this?如何开始?

The basics like: opening, creating, saving files via terminal using vim and writing code using vim.基本知识如下:使用 vim 通过终端打开、创建、保存文件,使用 vim 编写代码。 Also, does one compile directly using vim in the terminal?另外,是否在终端中直接使用 vim 进行编译?

Get started quickly 快速上手

You simply type vim into the terminal to open it and start a new file. 您只需在终端中输入vim即可打开它并启动一个新文件。

You can pass a filename as an option and it will open that file, eg vim main.c . 您可以传递文件名作为选项,它将打开该文件,例如vim main.c You can open multiple files by passing multiple file arguments. 您可以通过传递多个文件参数来打开多个文件。

Vim has different modes, unlike most editors you have probably used. Vim具有不同的模式,与您可能使用过的大多数编辑器不同。 You begin in NORMAL mode, which is where you will spend most of your time once you become familiar with vim. 您从NORMAL模式开始,这是您熟悉vim后将花费大部分时间的地方。

To return to NORMAL mode after changing to a different mode, press Esc . 要返回NORMAL切换到不同的模式,按Esc键后模式。 It's a good idea to map your Caps Lock key to Esc, as it's closer and nobody really uses the Caps Lock key. 将您的Caps Lock键映射到Esc是个好主意,因为距离更近并且没有人真正使用Caps Lock键。

The first mode to try is INSERT mode, which is entered with a for append after cursor , or i for insert before cursor . 第一种模式是尝试INSERT模式,进入可以进行追加光标后 ,或对 光标前插入

To enter VISUAL mode, where you can select text, use v . 要进入可以选择文本的VISUAL模式,请使用v There are many other variants of this mode, which you will discover as you learn more about vim. 该模式还有许多其他变体,当您进一步了解vim时,您会发现它们。

To save your file, ensure you're in NORMAL mode and then enter the command :w . 要保存文件,请确保您处于NORMAL模式,然后输入命令:w When you press : , you will see your command appear in the bottom status bar. 当您按: ,您将看到命令显示在底部状态栏中。 To save and exit, use :x . 要保存并退出,请使用:x To quit without saving, use :q . 要退出而不保存,请使用:q If you had made a change you wanted to discard, use :q! 如果您要放弃更改,请使用:q! .

Configure vim to your liking 根据您的喜好配置vim

You can edit your ~/.vimrc file to configure vim to your liking. 您可以编辑~/.vimrc文件以根据自己的喜好配置vim。 It's best to look at a few first ( here's mine ) and then decide which options suits your style. 最好先看一些( 这是我的 ),然后再决定哪些选项适合您的风格。

This is how mine looks: 这是我的样子:

vim屏幕截图

To get the file explorer on the left, use NERDTree . 要使文件浏览器位于左侧,请使用NERDTree For the status bar, use vim-airline . 对于状态栏,请使用vim-airline Finally, the color scheme is solarized . 最后,将配色方案晒干

Further learning 进一步学习

You can use man vim for some help inside the terminal. 您可以使用man vim在终端内部寻求帮助。 Alternatively, run vimtutor which is a good hands-on starting point. 或者,运行vimtutor ,这是一个很好的动手vimtutor

It's a good idea to print out a Vim Cheatsheet and keep it in front of you while you're learning vim. 在学习vim时,打印出Vim备忘单并放在您的面前是个好主意。

Good luck! 祝好运!

Run vim from the terminal. 从终端运行vim For the basics, you're advised to run the command vimtutor . 对于基础知识,建议您运行命令vimtutor

# On your terminal command line:
$ vim

If you have a specific file to edit, pass it as an argument. 如果您有要编辑的特定文件,请将其作为参数传递。

$ vim yourfile.cpp

Likewise, launch the tutorial 同样,启动教程

$ vimtutor

You can definetely build your code from Vim, that's what the :make command does. 您可以 Vim定义代码,这就是:make命令的作用。

However, you need to go through the basics first : type vimtutor in your terminal and follow the instructions to the end. 但是,您需要首先了解基础知识:在终端中输入vimtutor并按照说明进行操作。

After you have completed it a few times, open an existing (non-important) text file and try out all the things you learned from vimtutor : entering/leaving insert mode, undoing changes, quitting/saving, yanking/putting, moving and so on. 几次完成后,打开一个现有的(非重要的)文本文件,并尝试从vimtutor中学到的所有知识:进入/离开插入模式,撤消更改,退出/保存,拉动/放置,移动等上。

For a while you won't be productive at all with Vim and will probably be tempted to go back to your previous IDE/editor. 一段时间后,您将根本无法使用Vim进行工作,并且可能会想回到以前的IDE /编辑器。 Do that, but keep up with Vim a little bit every day. 这样做,但是每天都要跟上Vim的时间。 You'll probably be stopped by very weird and unexpected things but it will happen less and less. 您可能会被非常奇怪和出乎意料的事情所阻止,但是这种情况会越来越少。

In a few months you'll find yourself hitting o , v and i all the time in every textfield everywhere. 再过几个月,您会发现自己在任何地方的每个文本字段中都一直打ovi

Have fun! 玩得开心!

if you want to open all your .cpp files with one command, and have the window split in as many tiles as opened files, you can use: 如果要使用一个命令打开所有.cpp文件,并且将窗口分成与打开的文件一样多的磁贴,则可以使用:

vim -o $(find name ".cpp")

if you want to include a template in the place you are, you can use: 如果要在您所在的位置包含模板,则可以使用:

:r ~/myHeaderTemplate 

will import the file "myHeaderTemplate in the place the cursor was before starting the command. 将在启动命令之前将文件“ myHeaderTemplate”导入光标所在的位置。

you can conversely select visually some code and save it to a file 相反,您可以直观地选择一些代码并将其保存到文件中

  1. select visually, 视觉选择
  2. add w ~/myPartialfile.txt 添加w〜/ myPartialfile.txt

when you select visualy, after type ":" in order to enter a command, you'll see "'<,'>" appear after the ":" 当您选择视觉效果时,在键入“:”以输入命令后,您会看到“:”后出现“'<,'>”

'<,'>w ~/myfile $

^ if you add "~/myfile" to the command, the selected part of the file will be saved to myfile. ^如果在命令中添加“〜/ myfile”,则文件的选定部分将保存到myfile中。

if you're editing a file an want to copy it : 如果要编辑文件,则要复制它:

:saveas newFileWithNewName 

If you want to learn by reading yourself:如果你想通过阅读自己来学习:

  1. Open MacOS terminal app.打开 MacOS 终端应用程序。
  2. Write this and press enter -> vimtutor写下这个然后按回车-> vimtutor
  3. For quit write this and click -> :q退出写这个然后点击 -> :q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM