In my mind, if notepad is an old Nokia phone, and Sublime text is the iPhone, Vim would be the PDA that Spy-Kids had on their wrists. The point I am trying to make is, that Vim takes text editing to a whole new level, by making a "text editing language". So just as you have programming languages for programs, markup languages for markup, you can basically start using a text editing language, while using Vim.
This is going to be a multi-part series, going from the absolute basics to an intermediate level.
Let us get started with the very basics.
1. Modes of operation :
To edit or create a file with Vim, you need to just give the filename as an argument with Vim. ( vim mytext.txt ). Once you do that, you will realise that you can't directly modify the file, This is because, unlike many other editors Vim has specific modes of operation, and each mode is designed for a specific purpose. The modes are :
- Command mode ( default )
This mode is designed to allow you to view your text file ( or files ), perform search, and modify it by using specific commands. This mode prevents, editing of the file erroneously, because you need to enter a specific command, before Vim allows you to modify the file
- Editing mode
This mode is what almost all other text editors, open the text files in. It allows you to add/ delete text, just as you would in a Notepad application ( or any other simple text editor ). You can enter this mode by typing the "i" key when in command mode, and to exit you need to press escape key which will take you back to the command mode
- Visual selection mode
This is a special mode designed to allow you to visually select text, just as you can do with a mouse on any simple text editor. The difference, here is that you need to use your keyboard, instead of the mouse.
Once a text is selected, you can choose to run any text modification command on the selected section of the text.
2. Basics of movement
Since you will need to use the keyboard for moving around in the text, it helps to know the basics of movement.
- Ups and Downs ( h,j,k,l or Arrow keys )
You can either use h ( left ) , j ( up ), k (down ) and l (right ) for moving between lines of text when in command mode, or you can use the standard arrows keys.
- Finding text ( /, ? , n and p )
You can use the / key to search any piece of text in the entire document. Just type / in the command mode, followed by the pattern ( regular expressions are supported ), and press enter. To go the next instance of the pattern press n, and to go back to the previous occurrence press : Shift + n. To perform a search in the opposite direction, i.e. back from the current cursor position to the top of document, you can use ? instead of /, and the rest of it remains the same
3. Cut, Copy, Paste ( or Delete, Yank, and Paste )
In Vim world, delete = cut, yank = copy and paste is just paste. The keys used are d ( delete ), y (yank ) and p ( paste ). This works best along with the visual selection mode. So typically to copy some section of a sentence and paste it somewhere else, you would :
a ) Move the cursor to that position by using arrow keys ( or h,j,k, l )
b) Start visual selection by pressing v
c) Select the whole section
d) Press y to yank ( copy ) this section
e) Move the cursor to the desired destination
f) Press p to paste it after the cursor, or ( P : Shift + p ) to paste before the cursor
The same steps would apply to a delete and paste scenario, where instead of yanking with a y key, you would delete with a d key.
4. Save, Undo and Redo
Now all the changes done above would mean nothing, if we can't save them. A save operation, is literally a disk write operation ( in most cases ), hence it is called a write in Vim world. The command key to write the changes made to the file is w. To undo a particular change, you can use the u key any number of times, and to redo those changes you can use ( Ctrl + r ) command.
With the above information you can start exploring the vast world of Vim. Let me know if you found this helpful. If you have any suggestions, feedback or any other comments, write them in the comment section below. Also, if you found this helpful, do share it with others who can benefit from this. I'll see you in the next section.
Comments
Post a Comment