That stack overflow post about VIM
What is vim?
`vim
- Vim is the most pure text and code editor. It’s useful for two things:
- Looking like a badass,
- Mouse independence (you can do everything with commands, your hands never need to leave the keyboard)
- It comes by default on most UNIX systems.
- Its full name is “Vi IMproved.”
- Vim operates in several modes:
- normal
- insert
- visual
- etc.
Why use vim in the command line when Visual Studio and other IDE’s exist?
Using vim in the command line is advantageous for several reasons.
- Firstly, it is lightweight and fast, making it suitable for remote editing over SSH or on systems with limited resources.
- Secondly, Vim is highly customizable, allowing users to tailor their editing environment precisely.
- Thirdly, Vim’s keyboard-centric approach reduces the need for mouse usage, potentially enhancing editing speed and efficiency.
- Lastly, Vim is ubiquitous, being available on almost all Unix-like systems, and it’s valuable for users who work across different environments to have a consistent editing experience.
How do you exit vim?
Success
:qquit vim.:q!to force quit with unsaved changes:wqwrites and quits:wq!forces this action:xor:exitsaves and quits (safest)
☞ If you got this, congratulations! One in every 20,000 stack overflow questions is ‘how do I exit vim’
How do you enter Normal mode in vim?
Esc or Ctrl-[
- In Vim, Normal mode is the default mode in which you can execute commands. To return to Normal mode from any other mode (like Insert or Visual mode), press
EscorCtrl-[. This will stop text editing or visual selection and allow you to execute Vim commands.
How do you enter Insert mode in vim?
i, a, o, and other commands
- To enter Insert mode in Vim (where you can insert text), press
iwhile in Normal mode to start inserting text at the cursor position. Alternatively, pressato start inserting after the cursor,oto open a new line below the current line, orOto open a new line above. There are other variations likeIfor inserting at the beginning of the line andAfor appending at the end of the line.
How do you enter Visual mode in vim?
v, V, Ctrl-v
- To enter Visual mode in Vim (where you can select text), press
vin Normal mode. This allows you to start selecting text character by character. To select whole lines, pressV. For block (column) selection, useCtrl-v. You can then extend the selection using movement keys.
How do you undo in vim?
u
- The way to undo in VIM is ‘u’, in normal mode.
How do you save a file in vim?
:w
- To save changes to your file in Vim, press
Escto ensure you’re in normal mode, then type:wand pressEnter. This writes the changes to the current file. If you want to save the file with a different name, type:w filenameand pressEnter.
How do you exit vim?
:q
- To exit Vim, press
Escto go to normal mode, then type:qand pressEnter. If you have unsaved changes, Vim will warn you. To exit without saving changes, use:q!.
How do you select lines in vim?
V
- To select an entire line in Vim, press
Vin normal mode. This puts Vim in visual line mode, selecting the current line. You can then use the arrow keys orjandkto extend the selection to additional lines. To select a block of text, usevin normal mode and move the cursor to cover the desired text.
How do you copy and paste in vim?
y and p
- To copy (or “yank” in Vim terminology) the selected text, press
yin visual mode. To paste the yanked text, navigate to where you want to paste and presspin normal mode to paste after the cursor, orPto paste before the cursor.
How do you find and replace text in vim?
:%s/old/new/g
- To find and replace text in Vim, use the command
:%s/old/new/gin normal mode. This replaces all occurrences of ‘old’ with ‘new’ in the file. To confirm each replacement, addcat the end, like:%s/old/new/gc.
How do you move efficiently in vim?
Various navigation commands
- Vim offers numerous navigation commands. For instance,
h,j,k,lfor left, down, up, right;w,bto move by words;0,$to move to the start/end of a line;gg,Gto move to the start/end of the file. Vim’s navigation is designed to minimize keystrokes for efficiency.