How to Write in Vim on Linux or other OSes (Tutorial)
You can find many charts online about writing in the Vim editor on Linux or any other OS, but none of them are straightforward. (Many just have charts, which are still useful, just not for beginning users. I encourage you to read this later if you want to know more: https://bsd.org/viref.html) This tutorial will show you only what you need to know to get started writing in Vim. There are plenty more features and commands, and you can look those up later, but here are the very basics.
How to write in vim: The very basic commands
So, you open up the vim text editor. The first thing you notice is that you can’t just start typing as you can in nano. To write in the Vim editor as you would in nano, what you need is insert mode. To enter insert mode, type “i” and then you will be able to type normally. Once you are done typing, press ESCAPE, and it will return you to the mode you started in.
Once you are done writing, you probably want to save and exit. Saving and exiting both involve the colon key. To save, type “:w” (for write). To exit, type “:q” (for quit). If you want to do both at the same time, you can combine commands. Type “:wq” to save and exit. To exit without saving, type “q!”.
A few more complex commands you will probably want to know
Say you want to do more complex stuff other than simply writing in vim. For example, searching the file for a specific term. The “/” key is used to search. For example, if you want to search for “foo” across the file, type “/foo”; it will show you all instances. You can then press “n” to go through the instances.
Another good example is deleting a whole line. If you want to delete a line, type “dd”. If you want to delete one character, type “x”. Suppose you want to copy a line, press “v”. Then use the arrow keys to select an area of text. Then, type “yy” (for yank). Finally, find the location in the file you want to put the text, and type “p”.
How to write in vim on Linux or other OS: A table for your convenience
Command | Description |
i | Insert mode (allows you to type normally) |
ESC | Exit current mode |
:w | Write (Save) file |
:q | Quit vim |
:wq | Save and Quit |
:q! | Quit without saving |
/<term> | Search |
n | Go to the next instance found with the search (/) |
dd | Delete line |
x | Delete character |
v | Visual mode (lets you highlight) |
yy | Yank (Copy) text |
p | Put (paste) text |