Some simple Vi commands
i | go to the insert mode |
:w | save the contents |
:w filename2 | save the file you are working as another filename |
:q | exit vi |
:q!. | exit vi without saving |
x | delete character under the cursor |
d^ | deletes from current cursor position to the beginning of the line |
d$ | deletes from current cursor position to the end of the line |
3dd | deletes three lines from current cursor position downwards |
u | undo the last change to the file. Typing u again will re-do the change |
y | copy select characters to the system buffer |
p | paste the system buffer to the current cursor position |
v | visual mode |
/ | command searches forwards |
? | command searches backwards |
n | repeat the previous search command in the same direction |
N | repeat the previous search command in the opposition direction |
Introduction
The vi editor is a command-based editor used by many Linux users. The vi editor has powerful features to aid programmers, but many beginning users avoid using vi because the plentiful features overwhelm them. Although there are quite a lot of graphical based text editor provided by efferent distributions of Linux, learning vi is still important because in many situations, such as for server configuration, graphical user interface is not available. Text editor has to be down with command-based editor like vi.
The first thing most users learn about the vi editor is that it has two modes: command and insert. The command modes allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes. The insert mode puts anything typed on the keyborad into the current file.
vi starts out in command mode. These are several commnads that put the vi editor into insert mode. The most commonly used commands to get into inert mode are a and i.
starting the Vi Editor
Usage: vi
Example: vi test.txt
copy-paste
In the visual mode, you can move your cursor to select the text. When you selected the text area, the front and background color will been changed inversely. Select the second line and then, type "y" key. Move the cursor to the position of the document that you want. Type "p"
Search commands
The vi editor has two kinds of searches: string and character. For string search, the / and ? commands are used. When you use these commands in the command mode, the command just typed will be shown on the bottom line. You should then type the string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards(downwards) in the files while the ? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. Eg. "/the*"