macos 如何在终端中使用vim?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5764071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:59:06  来源:igfitidea点击:

How to use vim in the terminal?

macosvimterminal

提问by system

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. How does one get started on this?

我想在终端中使用 vim 而不是单独的文本编辑器开始编写我的 C 代码。如何开始呢?

The basics like: opening, creating, saving files via terminal using vim and writing code using vim. Also, does one compile directly using vim in the terminal?

基础知识如:使用 vim 通过终端打开、创建、保存文件以及使用 vim 编写代码。另外,是否直接在终端中使用 vim 进行编译?

回答by alex

Get started quickly

快速上手

You simply type viminto 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, e.g. vim main.c. You can open multiple files by passing multiple file arguments.

您可以将文件名作为选项传递,它会打开该文件,例如vim main.c. 您可以通过传递多个文件参数来打开多个文件。

Vim has different modes, unlike most editors you have probably used. You begin in NORMALmode, which is where you will spend most of your time once you become familiar with vim.

Vim 有不同的模式,这与您可能使用过的大多数编辑器不同。您从NORMALmode开始,一旦您熟悉 vim,您将在此度过大部分时间。

To return to NORMALmode after changing to a different mode, press 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.

NORMAL在更改为不同模式后返回模式,请按Esc。将 Caps Lock 键映射到 Esc 是个好主意,因为它更接近并且没有人真正使用 Caps Lock 键。

The first mode to try is INSERTmode, which is entered with afor append after cursor, or ifor insert before cursor.

第一种模式是尝试INSERT模式,将其输入与a用于追加光标后,或i光标前插入

To enter VISUALmode, where you can select text, use v. There are many other variants of this mode, which you will discover as you learn more about vim.

要进入VISUAL模式,您可以在其中选择文本,请使用v。这种模式还有许多其他变体,随着您对 vim 的了解更多,您会发现这些变体。

To save your file, ensure you're in NORMALmode and then enter the command :w. When you press :, you will see your command appear in the bottom status bar. To save and exit, use :x. To quit without saving, use :q. If you had made a change you wanted to discard, use :q!.

要保存文件,请确保您处于NORMAL模式,然后输入命令:w。当您按下 时:,您将看到您的命令出现在底部状态栏中。要保存并退出,请使用:x. 要退出而不保存,请使用:q. 如果您进行了要放弃的更改,请使用:q!

Configure vim to your liking

根据自己的喜好配置 vim

You can edit your ~/.vimrcfile to configure vim to your liking. It's best to look at a few first (here's mine) and then decide which options suits your style.

您可以编辑~/.vimrc文件以根据自己的喜好配置 vim。最好先看几个(这是我的),然后决定哪些选项适合您的风格。

This is how mine looks:

这是我的样子:

vim screenshot

vim 截图

To get the file explorer on the left, use NERDTree. For the status bar, use vim-airline. Finally, the color scheme is solarized.

要获取左侧的文件资源管理器,请使用NERDTree。对于状态栏,请使用vim-airline。最后,配色方案是solarized

Further learning

进一步学习

You can use man vimfor some help inside the terminal. Alternatively, run vimtutorwhich is a good hands-on starting point.

您可以使用man vim终端内部的一些帮助。或者,跑步vimtutor是一个很好的动手起点。

It's a good idea to print out a Vim Cheatsheetand keep it in front of you while you're learning vim.

在您学习 vim 时,打印出Vim Cheatsheet并将其放在您面前是一个好主意。

Good luck!

祝你好运!

回答by Michael Berkowski

Run vimfrom the terminal. For the basics, you're advised to run the command vimtutor.

vim从终端运行。对于基础知识,建议您运行命令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

回答by romainl

You can definetely build your code fromVim, that's what the :makecommand does.

您可以完全Vim构建您的代码,这就是该:make命令的作用。

However, you needto go through the basics first : type vimtutorin 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. Do that, but keep up with Vim a little bit every day. You'll probably be stopped by very weird and unexpected things but it will happen less and less.

在一段时间内,您使用 Vim 根本无法提高效率,并且可能会想回到您以前的 IDE/编辑器。这样做,但每天跟上 Vim 一点点。你可能会被非常奇怪和意想不到的事情阻止,但它会越来越少发生。

In a few months you'll find yourself hitting o, vand iall the time in every textfield everywhere.

在短短的几个月,你会发现自己击中ovi所有在每一个文本框无处不在的时间。

Have fun!

玩得开心!

回答by toshi

if you want to open all your .cppfiles 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
  1. 视觉上选择,
  2. 添加 w ~/myPartialfile.txt

when you select visualy, after type ":" in order to enter a command, you'll see "'<,'>" appear after the ":"

当您选择visualy时,在键入“:”以输入命令后,您将看到“'<,'>”出现在“:”之后

'<,'>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