You do not need to be a command line wizard to be able to use Vim. Using Vim consists in three simple steps: opening Vim, making the necessary text edits, then saving and exiting.
Using the vim command followed by a file name will result in opening that file in the vim editor. If that file does not already exist in your current directory, it will be created.
In this case, we see [New File], because I did not have a helloworld.txt already present in my directory, so I am creating it.
In order to go any further, we need to get a grasp on how Vim generally works. These are the key concepts you need to remember:
- Vim is a text editor (just like VSCode) that runs in your terminal.
- Once you open Vim, you get into an interactive mode.
- IMPORTANT: Vim has multiple modes.
- IMPORTANT: Vim operates on commands.
- Vim is extremely configurable.
Theory behind modes & commands:
When you first open Vim, you are in NORMAL mode (otherwise knows as MASTER mode) . This is like your home base. You always want to make sure you are in NORMAL mode before you execute any commands. Press i to jump to INSERT mode. Note that once you are in INSERT mode, all your keystrokes will result in text. In other words, you will not be able to enter any commands. In order to do so, you will need to go back to NORMAL mode, which is easily achievable by pressing the <ESC> key.
Feel free to reference the diagram below. Keep in mind that VISUAL mode is simply another mode, just like INSERT mode, accessible by pressing v.
Every Vim command starts with a colon : and is followed by a special keyword. Remember that you can only execute commands in NORMAL mode.
Now that you understand the concept of modes & commands, go back to your terminal, and add some text to your file while in INSERT mode:
To wrap this up, let’s make sure to have stackoverflow’s most popular question answered. Numerous people (including me) end up opening Vim by mistake, and not knowing how to exit it (in my case, it was after a git commit command without a -m message).
With the knowledge you have now, this is simply a matter of executing the right command. make sure you are in the NORMAL mode by pressing <ESC> and use the adequate :q command from the list below to quit Vim.
:q - no changes were made :q! - trash all changes :wq - save the changes
- Open file using Vim: vim
- INSERT mode: press i
- NORMAL mode: <ESC>
- Save Changes & Exit: :wq
- Exit Without Saving Changes: :q!
If you think you got a handle on the 5 steps from the recap above, then congratulations 🎊 you are now a Vim user!
Although this article might help you get your feet wet with Vim by accomplishing basic text editing operations quickly via the terminal, it only encompasses about 1% of Vim’s potential. If you want to learn more, vimtutor is a fantastic built-in command that will pull up important Vim lessons on your terminal. You can also access it on the web via vimtutor.
Still trying to grow your Vim knowledge? Checkout these resources:
🌐 Official Website: https://www.vim.org/
🎥 Youtube Playlist: https://www.youtube.com/watch?v=_NUO4JEtkDw&list=PLMHNm2pfuJiuJUOSpIyGJHpk3B7PCvpBw
🌐 Wikipedia: https://en.wikipedia.org/wiki/Vim_(text_editor)
📜 My personal favorite Vim Cheat Sheet: https://devhints.io/vim
🌐 Tutorial: https://www.tutorialspoint.com/vim/vim_quick_guide.htm
🎨 Personalize Vim by editing .vimrc: https://dougblack.io/words/a-good-vimrc.html
Original Source: https://medium.com/swlh/getting-started-with-vim-3f11fc4f62c4