What is Vim?
Vim is one of the most powerful and fast text editors available for Linux mac and UNIX OS. The vim text editor is packed with features, these features are smiler to the features that we see in modern-day IDLE. Vim is considered so special because it is designed in such a way that, you can work on it for hours without even touching your mouse once. you can open files, edit, copy, paste, code, and even run them by using vim. Legends still say vim is better than any text editor including VScode. If you have not used Vim before, then you will be so surprised by how it works.
Well, vim is not just and text editor, it is a cult and there are people who love vim so much that they never use any other text editor because they think, all editors are not good enough when compared to vim. I personally think vim is dope and it's a skill that every hacker and programmer should learn because once you learn it, you will be able to use it forever. Yes, it's going to take a couple of hours for you to wrap your head around it, but trust me, it's worth it. Vim is just a short form of Vi Improved because before vim, there was one text editor named vi and vim is just an improved version of it.
How to Install Vim in Termux?
Vim is just a simple text editor like nano and it comes in a package, so no matter which Linux console you are, you can install it with a package command. since we are on termux, we can use simple pkg command to install it. The vim package is really small and you can install it in a few seconds.
pkg install vim
If you see any prompt, just press enter and vim will be installed in no time. and yes this is all you have to do to install the best text editor of termux.
How to Use Vim in Termux?
Start Vim In Termux:
vim
Now you will see the vim intro page, and if you will try to type anything, you won't be able to do it.
Go into Insert Mode using Vim:
So to start typing anything in the vim text editor you have to press I to start typing, press I on your keyboard and you will be able to use it like a normal text editor.
i
Try typing a few things and get comfortable with the user interface.
Exit Insert Mode in Vim:
If you are done typing, you can press the escape key on your termux extra keys to exit the insert mode. after pressing scape you won't be able to type anything.
ESC
After you escape from insert mode, you need to save the file. because that's what we normally do.
Save files in Vim:
To save a file using vim in termux. you have to write it, writing is the term that is used in computer language for saving files. now make sure you are not in insert mode and type the below command
:w filenameafter you escape from insert mode, you need to save the file. because that's what we normally do.
Exit from Vim:
Well exiting from vim is considered one of the most difficult things among developers😂, there are so many memes, and here Is one for you.
:wq
If your file is not saved then you have to enter the file name as well, but if it's already saved you can just type :wq and you will be out of vim.
Exit Vim without saving anything:
If you are just messing around and you don't want to save anything and just want to exit vim you can use the below command. type the below command after pressing the ESC key.
:q!
After typing this command whatever changes you have done to the file will be gone and you will be out of vim.
Exit Vim without saving quickly:
If you want to save and quickly exit, you can use capital zz two times to save and exit. make sure you are in command mode and not in insert mode.
ZZ
After typing this command whatever changes you have done to the file will be saved and you will be out of vim.
Open folders in Vim:
As I told you, vim is the best text editor and all the best text editors have file management inside them. if you are working on a project, and all of your files are inside a folder, you can import that entire folder inside vim and you will be able to access and manipulate the files without constantly opening and closing the folder. type the below command to import any folder in termux vim.
vim foldername
You can use your navigation keys or hjkl keys to move between these files.
Navigate between folders in Vim:
If you are inside a file and want to switch to the other files, you can just use the :e command, and then you have to Press Tab in termux and you will see all the files, you can select them by pressing enter key.
:e
To go forward you can select the folder and to go backward you have to select the ../ option in the file structure.
Cursor control keys in Vim / how to navigate using vim in Termux:
H --> move Left
J --> move Bottom
K --> move Top
L --> move Right
But, since termux is used in touchscreen smartphones, as long as you are using vim on termux, you can use a touchscreen to quickly navigate and move your cursor to the exact location.
Go to the previous word in Vim:
If you just want to move backward and edit the previous word, you can press be and it will go one word back and it will keep going if you will continuously press b.
b
it will come in handy when you make spelling mistakes or just want to update something quickly.
Go to the end of the Next word in Vim:
When you move backward and do some edits, you need to come back to where you were, you can use e to move forward.
e
These keys will also come in handy while editing a long script.
Move to the start of the line in Vim:
If you are writing something or let's say a code, and you want to change something that is at the start of the line, you can just press 0 on your keyboard and you will be at the start of the line.
0
keep in mind that it will only take you to the start of the same line that you are currently in.
Move to the End of the line in Vim:
This is the same as the above shortcut, if you want to move to the end of the link you can just press the dollar sign and you will be at the end of the line.
$
since $ sign is not visible on our keyboard, we can always use the touch screen to navigate(i mean do whichever makes your workflow faster).
Move to the Top of the page file:
If you are working on let's say a python file and you realize that you forgot to import some libraries. in that case, you can easily press escape and press gg and you will be on the first line of the page.
gg
of course, you can use the touch screen too.
Move to the Bottom of the file:
If you are editing and you want to go to the last line of the file then you can press capital G and you will be in the bottom line.
G
keep in mind that, you just have to press the ⬆ button to make all the letters on your keyboard capital and then press G to move to the bottom.
Go to a specific line in a file:
Well, we all encounter a case where we see an error on a particular line, using this simple command of vim you can directly go to that particular line and edit it. it makes the work follow so much more convenient and professional.
:7
above you can 7 and it will take you to like 7, you can put 11 instead of 7 and it will take you to like 11.
Enter replace mode:
If you are in visual mode and you want to just replace a few lines, you can just press capital R and you will be in replace mode, you can type over the line and it will get replaced in real-time.
R
Replacing makes the job really easy so make sure you try it a few times.
Undo :
The most important thing in the computer is to undo. so in vim you can press u to undo anything that you have previously. it also has multiple undo options so you can roll back many times.
u
yep, you have to press ESC before doing undo, or else you will type that in insert mode.
Redo :
If you have undone something and you want it back, you can just redo it will a simple ctrl and r button. just like undo you can also redo multiple times.
:ctrl r
this can look a little hectic but it will make your workflow much smoother once you learn it.
copy entire line:
This is my favorite command/key. if you want to copy an entire line, you can just use yy to copy it. this works so perfectly.
yy
you can use this to copy any line and you can paste it multiple times with the below commands.
Cut the entire line :
If you think a particular line is unnecessary and it should not be there then you don't have to press the backspace 2 times to delete a file, instead you can press dd and the entire line will be cut.
dd
The lines that you have copied right now, you can also paste it.
Paste the copied and cut lines :
so now that you know, how to cut an entire line and how to copy an entire line, you should also know how to paste it. use p in visual mode to paste the cut line, you can press it multiple times and it will paste the copied and cutting data.
p
This won't paste the data that you have copied on your clipboard.
Paste from your phone's clipboard:
If you want to paste the data that you have copied from github or anywhere other than termux then you need to long press and paste like we normally do.
Just long press on the screen and then paste.
This is simple and most convenient in my opinion when it comes to termux.
Delete a single character in visual mode :
In visual mode x works as backspace so if you are just looking around and find a few letters that are extra or you wanna delete anything in general, use the x key.
x
if you will press x one time, it will only delete one character.
Repeat the last thing you did :
This is the most awesome thing about vim. You can literally loop your actions using. key on your keyboard. Like if you have pressed 1 and pressed enters then press the escape key and start pressing. key, it will loop your actions and you will have a list of 1 like 20 times. it will also repeat the same command if your last interaction with vim was a command.
.
Yes, it is a little hard to explain but try it yourself and you will love it.
Select text line by line :
To select in vim, it's like pressing shift and selecting in computers. So to select some text make sure you press capital V, not small v, and move your cursor forward and it will select the lines.
V
you can use dd to delete or yy to copy the selected data.
Select everything in the file:
When I get frustrated with my code, I usually just delete everything and start over (in the initial stages, I know it's not a good habit 😛). If you want to select everything, just use the below shortcut. It's a combination of multiple keys and here you can see the beauty of vim, just press ggVG and everything is selected. just press ggVGdd and boom everything is gone😂, the easiest way to delete everything.
ggVG
you can use dd to delete or yy to copy everything that you have selected.
Sort anything using vim:
:sort
Enter date in vim:
we sometimes use a text editor to write stuff, we can use the below command to add dates where ever we want. no matter what are you doing, it's always a good practice to put dates as a comment as you update the file. type this command, press enter and you will see the exact date printed there in your file.
:r!date
it works in the command section.
Make the entire line uppercase:
well, there are a few helpful file-editing shortcuts. you can use capital V and capital U together to make any selected text uppercase. you can select an entire line and make it uppercase.
VU
This is actually helpful when you do formatting in your files.
Make the entire line Lowercase:
If you want to a line entire lowercase then in that case you can the below key combination.
Vu
Just select the line and press the above keys and it will turn the selected to lowercase.
Open windows in vim:
Split windows horizontally:
If you want to work on multiple files at a time, you can split your vim text editor into multiple instances of vim by splitting the windows. if you will press the below key combo then the window will get split horizontally.
Ctrl + w + s
You can split windows as many times as you want.
Split windows Vertically:
If you want to split the windows in a vertical manner then you can do that with the below combo.
Ctrl + w + v
you can also split vertically multiple times if your phone screen is big enough.
Switch windows :
If you want to switch to the other opened windows then you can press the below key combinations and your cursor will switch between windows.
Ctrl + w + w
constantly use the combo until you are in your desired window.
Quit the current window:
If you are down and want to quit a window then you can press the below combo and the window will be closed.
Ctrl + w + q
if you will do this command multiple times then all the windows will be closed one by one.
Search and replace:
Search a word in vim:
If you want to just find a word in a text file then you can just use the /and type any word you want after that and vim will highlight it.
/theword
Make sure to type this in visual mode and in the command section.
Search and replace a word in vim:
If you want to search and replace a particular word in vim, then you can easily do that using the following below command. it can look complicated but trust me it's going to save you so much time when executed correctly.
:%s/oldword/newword/g
Do not add any spaces. just follow the above syntax.
Display line numbers in vim:
Every programmer loves those number lines on the left side of the editor, and to enable them in vim you have to type the below command.
:set number
This will show the number line in vim termux.
Enable syntax highlighting in vim:
If you are working in a particular language and you want to see the syntax highlighting just type the below command because syntax highlighting is the best part about using IDEs.
:syntax on
if you don't like then you can turn it off too.
Learn vim through vim help:
If you want to learn more than this and you are okay with reading cultured documentation then it's highly recommended that you execute this command at least once and try to read.
vimtutor
it's optional because I have listed almost everything good in the post.
2 Comments
Helpful info, Termux is really good app for beginners & pro level users.
ReplyDeleteAt first it was really difficult to us Vim when i was studying Software Engineeeing, but after a while I was happy that i can manipulate vim so easily. Its like a power of being able to control. Very helpful article for me to revise on Vim. Thanks Maqsood
ReplyDelete