The text editor one of the most important programs on your computer. This begs the question of what is the best text editor out there. I think the answer is obvious vim of course. So why vim why bother learning vim. Vim is this strange beast. It’s got this weird mode system to it. It uses strange key bindings nothing makes sense as far as the keystrokes involved. It’s not like any other plain text editor you’ve ever used. So this is A Beginner Guide to Use vim editor which is the best text editor. Those of us that run Unix like operating systems. vi or vim is installed on every Unix like operating system out there. So you already have it on the system on minimal installs and server installs often. VM is the only text editor installed on the system on minimal Linux distros.
Vim is for all Linux Distro
I installed many Linux distros where vim is the default text editor on the system. Even if there were other text editors installed. So you go to edit a config file or something at the command line and you end up in vim wouldn’t it be nice to be familiar with vim to be able to do some basic editing of a file.
Just to make your life easier. If you’re a Linux enthusiast a Linux guru or you’re doing any kind sysadmin work. You’re a programmer or what have you it makes a lot of sense to at least have some familiarity with vim. Now, why would you go beyond them? Why would you know really dive into vim become a vim master? It’s because it’s such a powerful tool. If you live in the text editor. It makes a lot of sense to really dive into vim and learn the ins and outs.
Installing vim on linux
So you can use the sudo apt get install vim command to install on Debian based Linux. That’ll centrally installed for you it’s a couple of megabytes. So you don’t have to worry about anything that all right now. When it comes down to using the vim editor opening files is extremely simple creating files is extremely simple you simply need to type in the syntax vim and the name of the file that you want to edit.
Creating a New file with vim.
Now if you’re creating a new file. It is very simple just type vim and type file name with your file extension. I can essentially give the file a name
for instance, I create file myfile.txt with the command of vim myfile.txt
So I found it great a file called and of course the extension can be whatever you want to be whatever you’re trying to create it can be a C file a python file etcetera. Whatever it is really you’re looking to do you can use that file extension.
vim insert mode.
By default when we open file with vim. It is open in command mode. So now we’re talking about actually writing changes to the old guys are writing to the file to do that we use the I. Which is stands for insert and after pressing I. Now you are in editing mode now you can write in your file. You can see little insert dialog left bottom side of your file. This is very important because it lets you know what mode you’re currently. And so when I am in insert mode I can start typing whatever I want.
Close file without saving.
If I want to close the file without saving any changes. I do that by pressing esc key then followed by colon q and the exclamation mark. Then hit enter will essentially close the file without saving any of the new changes made to the file.
There are a few keyboard commands. That you can use without using the colon. That is at the capital Z. I said so you want to type capital Z two times. So doubles it and that will essentially save and quit. So you don’t have to use the colon. On that make sure you have your caps lock enabled or you press z with the shift key.
Save file with vim
After creating a file for us and of course with vim or any other editor you need to save the file all right. So when you talk about saving a file. First press the ESC key and followed by Saving a file can be done by using the colon(:) sign and interacting with the editor in regards to saving files always thought off with the colon. That is something very important that you need to know with vim. Many people actually open up a file with vim.
This is where they get really really confused. So if you want to exit out of the editing mode and you want to start interacting with the editor in regards to saving the file or exiting. You start off with the colon all right.
If I want to save a file I hit ESC key and followed press colon and the lower case w. and hit enter. All right and it’s going to tell you that your file is written okay. so that’s how you save the file in vim
Exit from vim editor.
Now the file has been saved now when you want to edit and save your file. When you want to exit out of the vim editor. it’s really very simple all you need to do is hit the colon again And you can hit the lower case q and a which essentially close all files. If you want to exit out of your editor you simply use the colon and the lower case q and hit enter. That’ll get you out of the file that you editing.
so if you want to now save and exit the file. I use the colon and hit w and q. and that will save and close the file and exit from vim editor.
for that and that’s pretty much it in regards to saving files exiting from the vim editor. All right so now that we’ve talked about. This and should be creating a file closing the file, saving and closing without saving. We can now move on to navigation and editing the code itself.
Navigation in vim is So simple.
So again you can use your arrow keys right of yet to navigate around the file. I know that there are other navigational keys and that adding those are denoted by the H. J. K. and L. keys. I’ll be talking about that in the next article the reason I’d want to do it right now is that that’ll confuse a lot of things. All right so let’s focus on the basics. If I want to insert code on this particular line press I for insert mode and then I can comment that line and once I’m done I hit escape to exit out of the insert mode.
Moving around lines in vim
All right if I want to move on to the next line I can hit o that is a lower case and. That will take me to the next line and will take me into the into insert mode right over here which means I can enter a new line.
Deleting Single character in vim
Now when you talk about deleting characters for a particular character for example if I admit misspelled at this line would defy say Lino of course first you have to exit from insert mode. I want to delete the character o. All I need to do is go right to where it is and hit a lower case s. That will get rid of the last character and I can correct it and he just hit escape and that we all right. So that is very very simple.
Delete the entire line in vim.
I want to delete an entire line to do that I hit the capital S. The upper case S and that will delete a particular line right. So we can delete the full line with the Capital S in the command mode of vim. That will delete the entire line for me.
Undo and Redo in vim editor.
Now to talk about undo and redo. That those are denoted by the lower case u. which is for undoing the changes. And a capital C or R to redo the changes. So if I want to undo the changes that you have made just hit lower case (u) and it’s going to undo the change. If I want to redo the change I use the capital C that redoes or will essentially redo all the changes that I’ve made. So to undo use u. the lower case u and redo the changes use the capital C or R.
So that to undo you press lower case u. To redo we can press capital C or R to redo the stuff. But all of that you do in command mode.
Moving around lines in vim
When it comes down to navigation. Navigation is very very simple of course moving on to different lines so for example. If I wanted essentially moved to the start of a line I can use the Zero (0) key. That’ll take me to the start of the line. If i want to move to the end of the line. I use the dollar sign and they’ll take me to the end of the line excellent stuff right there. so if I wanted to edit to the start of the line I use these 0 command and if i want to edit last line i press dollor sign.
copying and pasting in vim editor
I haven’t covered so far is your clipboard. Now you can copy and paste. That’s it for now when you talk about coping with vim. If you are using a GUI editor. You can use your mouse. I’ll talk about coping well if you want to copy we essentially you can use the double yy command
For paste the copy line where ever you want by pressing the lower case p. That is how simple is that coping and pasting in vim.
Some Tips to Use vim editor.
I’m gonna give you guys a few tips that will really help you now with vim I should have actually mentioned that when talking about code lines all right code lines are very very important. You’re talking about enabling code lines in the vim editor. Which is something a lot of you guys actually want.
So if you want to enable code lines all you need to do is make sure you are in Command mode on vim. Type a command set number and hit enter. It’s going to give us all very code lines with the number.
Navigating to a particular line
Now when you’re talking about navigating to a particular line in your code for example I have a file that has 100 lines in it. To move to line 40 to do this I usually call on once more command mode of vim to press ESC hit colon. I type the line number I want to go in this case I’m going to go to line 40. so I type 40 and hit enter. It takes me directly to line 40 which is excellent. I do recommend when working with code that you do enable you’ll you and you to enable your code lines nice
Disable line numbers in vim.
You can disabled if you’re not a fan of them you simply type set nonumber and that’ll get rid of them for you.
All right so that pretty much is going to be it for this A Beginner Guide to Use vim editor reference from. I know I haven’t covered a lot but I hopefully have covered all the basics and the fundamentals of the vim editor.
I’ll be covering the different tips and tricks that you can use that will actually improve your productivity when using the tool in the next article. So that’s pretty much going to be for this article. Thank you so much for here. if you have any questions or suggestions let me know in the comments section on my website and I’ll be seeing you in the next article. This is your penguin from linuxcan signing off.