Книга: Linux Command Line: An Admin Beginners Guide
Назад: The Command Line - Basic Commands
Дальше: The Command Line - More Advanced Commands
The Powerful Built-In vi Editor
Most Linux distributions include some built-in editors such as vi, nano or emacs. The vi editor is very light-weight and is commonly built-in even on space-constrained Linux distributions such as the Raspberry Pi.
vi & vim
Vi is a text editor for Unix and Unix-like systems, such as Linux. Enter vi by typing vi into the command line and pressing Enter . You may be taken to vim, which is okay because vim is a lot like vi with a few extra features.
vim
Typing vim will also allow you to use a text editor in the terminal. The abbreviation vim stands for "vi improved" in Linux, and it offers a few features not found in previous versions of the vi text editor.
vimtutor
This section will provide an overview of the vi and vim editors but a vim tutorial is built into most Linux distributions and can be viewed by typing vimtutor. The tutorial will take less than an hour and has been helping people learn to use vi and vim for many years.
vi/vim Basics
The vi/vim editor has 3 modes, command mode, insert mode, and visual mode.
Quitting vi or vim
To quit, type colon and then the letter q ":q" into the terminal and press Enter . Like every other program run in the terminal, vi and vim require that you type a different command into the terminal to quit. Typing Escape or Delete will not yield any results.
Exercise:
Open vim by typing vim and the name of the file you want to edit. For this exercise, we will edit that file we were working on in the previous example. You should be able to use the arrow keys to move the cursor to where you want to begin editing. Press i and then the Enter or Return key to enable editing. Add a sentence such as "I'm using vim" to this document. Next, save the document. Press Escape to go back into "Command" mode, then type “:q” to quit. It may take a few tries before you feel comfortable using the editor and entering the key combinations.
More vim
Other things you can do with vim include viewing and editing two documents or files at once, copying, and pasting.
Vertical Split
To view two documents or files at once, type in vsplit and the name of the file. In some cases, you may have to type in the absolute path. Once you do this, you can now view and edit both documents. This may come in handy if you are editing a file's code and you need certain lines of the two to match.
Copying
If you want to copy text in vim, enter Visual mode and select the text and type y (short for yank). Make sure you are in Visual mode, or this may not work.
Cutting
If you want to cut text in vim, enter Visual mode and select the text and type "d." Make sure you are in Visual mode or this may not work.
Pasting
To paste, type p. Make sure you are in Visual mode or this may not work. The program should let you know what mode it's in by displaying it at the bottom.
Exercise:
Practice opening up two files at once and edit them to say the same thing. Then try cutting, copying, and pasting information into your document.
Play around with what you've learned and try to master the vim text editor. Remember, it might take a little practice to get the commands right when you first start using this editor. 
Chapter Summary
Назад: The Command Line - Basic Commands
Дальше: The Command Line - More Advanced Commands