Home  >  Article  >  System Tutorial  >  Vim Text Editor Getting Started Guide

Vim Text Editor Getting Started Guide

PHPz
PHPzforward
2024-01-02 16:58:084812browse

Vim 文本编辑器 入门指南

For a programmer, choosing a text editor is a very important thing. Because there are many differences between different editors: graphical interface or non-graphical interface, different shortcut keys, different programming language support, different plug-ins and custom settings, etc. My advice is not to search for the best editor, but to choose the one that best suits your habits and best suits your tasks. If you plan to work in a group, it's best to choose the same editor as your colleagues. This way, if you encounter problems during use, you can go to them for help.

This is exactly why I started using Vim a few years ago. Generally speaking, Vim is placed in opposition to the legendary Emacs. I admit I know very little about Emacs, but what you need to know about both is that they are both deeply customizable and can be very confusing at first. This tutorial won't cover everything there is to know about Vim, but it will cover the basics so you can get it right from the start, and then show you some tips that will (hopefully) give you the ability to explore on your own. study.

The word Vim comes from "Vi IMproved". Vi is a non-graphical text editor widely installed on Unix systems, and it is also installed by default on Linux systems. Vim is an enhanced version of this original editor, but unlike Vi, not every distribution has it installed by default.

Install

You can use the following command to install Vim in Ubuntu:

sudo apt-get install vim

If you are already interested in certain plug-ins, use the following command:

sudo apt-cache search vim

This command will output you a long list of packages related to Vim. Among these, there are tools for different programming languages, plug-in managers, and so on.

In this series of tutorials, I will use the latest version of Vim (7.3.154, LCTT Annotation: the latest version is now 8.0) on Ubuntu. Of course you can use any other version.

warm up

Enter the vim command in the terminal, and you will see a great welcome interface.

Vim 文本编辑器 入门指南

(LCTT translation annotation: Did you see the line "Help poor children in Uganda!" in the welcome interface?)

If you've never used Vi or Vim before, chances are you don't even know how to exit it... Yes, that's true. Any shortcut keys you commonly use in Vim will lose their original effects. (LCTT translation annotation: There is a joke circulating on the Internet - "How to create garbled code" and "Let novices exit vi")

First, use any imperative function like Save () or Exit ( ), you must first enter a colon (:). Saving is :w and exiting is :q. If you want to exit without saving the file, use the force exit command :q!. The great thing about Vim is that you don't need to enter each command separately. In other words, if you want to save and exit, you can just use :wq.

Now, we exit Vim and open a text file. To do this, just add the name of the file you want to edit after the command:

vim [文本文件名]

Vim 文本编辑器 入门指南

Generally speaking, when you open a text file, you will be in view mode. This makes Vim unique and initially confusing. Vim mainly consists of two modes: viewing mode and editing mode. View mode is used to view content and use some commands. To enter edit mode, just press the i key to insert () or the a key to Add to(). To return to view mode or perform command function operations, press the Escape key. The difference between inserting () and adding () is just that you want Enter edit mode and enter text before or after the cursor position. To fully understand, you should try it yourself. My suggestion: use to add () only at the end of the line, and to insert (## at other times #).

(LCTT Translation: The original text of "view mode" in this paragraph is "visual mode", which is suspected to be "view mode". In this mode, text can be viewed but cannot be edited; while "visual mode" is editing mode. One, you can press the

v key to enter, and then you can use the direction keys to select starting from the current cursor position, and it will be displayed with a reversed visual effect. Normally, you can press y after selection. Copy, press d to cut, etc. In addition, the author's terminology is not standard. According to Vim's own terminology, the so-called "view mode" here should be called "normal mode" ", "Edit mode" should be called "Insert mode", but the meaning is the same.)

To move the cursor within text, you can usually use the arrow keys on the keyboard, which work in both view mode and edit mode. However, a true purist will tell you to use the keys

h for left, j for down, k for up, and i for Come right (in view mode) to move.

Now that you understand how to control Vim simply, let's go a little deeper.

Some simple commands Now that you are familiar with switching between normal mode and insert mode, here are some commands you can use in normal mode:

  • x: Delete a character
  • u: Undo an operation (equivalent to Ctrl z)
  • dd: Delete a line of content
  • dw: Delete a word
  • yy: Copy a line of content
  • yw: Copy a word
  • p: Paste a previously deleted or copied line or word
  • e: Jump to the next word (LCTT translation annotation: end of word) (faster than simply using the arrow keys)
  • r: Replace a letter (press r, release, then press new letter)

Of course there is more than this, but these are enough for now. If you master all of the above, you will be able to use Vim smoothly.

For those who want to know more, I will mention a few more. You can add a value before any of these commands and the command will be repeated the corresponding number of times. For example, 5x will delete 5 consecutive letters on the current line, while 3p will paste 3 times.

Advanced commands

Finally, as an encouragement and example for you to continue exploring, here are several advanced and commonly used commands:

  • /Searched content: Search for specific content in the text
  • :sp text file name: Split the screen into upper and lower halves horizontally, and the new file is displayed in the other half. If you want to switch focus on both sides, you can use the Ctrl w ​​shortcut key.
    Vim 文本编辑器 入门指南
  • :vsp text file name: Same as above, but split the screen vertically
  • Ctrl Shift C and Ctrl Shift V: Copy and paste text in Terminal
  • :! Command name: Run terminal commands outside Vim in Vim and send them directly to the shell. For example, :! ls will display the files in your current directory without exiting the editor.
    Vim 文本编辑器 入门指南
in conclusion

I think you should be ready enough to start using Vim now. You can also use interactive tutorials to learn more by installing various plugins, editing the ~.vimrc file, or entering the vimtutor command in the shell.

If you have any other Vim commands you’d like to share, please let us know in the comments.


The above is the detailed content of Vim Text Editor Getting Started Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete