In this segment we will take a look at a tool to put content into files and edit that content also. Vi (Vim) is a very incredible tool. The aim of this article is not to cover everything that Vi can do however to get you ready for the basics.
Vi is a command line text editor, the command line is a different environment. It’s a single window with text input and output only. Let’s jump into it and try to learn the basics stuff about Vim.
A. Normally vim is run with a single argument which is the file you would like to edit. When we specify the file it can be with either file name or file path. Vi and Vim, both can be used. You can do it this way. The file will be empty.
B. Vim generally starts in EDIT mode so the first thing we will do is switch to INSERT mode by typing “i”.
You can type in a couple of lines of text and press Esc which will return to EDIT mode.
Let’s try some more advanced steps. For these, ensure you are in EDIT mode.
If you are not sure that you are in EDIT mode or not, you can take a look at the bottom left corner. If it does not say INSERT, you are fine or you can simply press Esc to exit INSERT mode. If you are in Edit mode, pressing Esc does nothing.
C. Suppose you want to quit and discard all the changes to the file. The option (:q!) can be used. First press colon (:) and then type “q!”. Make sure you are not in INSERT mode.
D. Suppose you want to save a file but do not want to exit. The option (:w) can be used. You can do it this way.
E. Suppose you want to save a file but also want to exit. The option (:wq) can be used. You can do it in this way.
F. Suppose you want to move around the cursor at a specific point in the file, arrow keys can be used and you can do your things according to EDIT or INSERT MODE.
G. Suppose you want to give numbers to the lines, “set number” command can be used.
H. Suppose you want to delete the content of the file, there are commands for this but “backspace” and “delete” are easy to use, those options can be used in INSERT mode.
I. Suppose you want to search for a particular word in a file, there are different ways to do this but in vim it can be done using “/yourword” syntax, vim will highlight the line in which the word is present.
J. Suppose you want to replace every occurrence of a word with a replacement word in a file, it can be done using “%s/pattern/replace/g”.
Few more important commands are mentioned below.
For more options, See the Manual Page.
More on Related Topics :