There are four types of vim mode
- Command Mode
- Command line mode
- Insert Mode
- Visual Mode
- This is default mode of Vim, You can switch to other modes using this mode
- The commands that you run without any prefix indicate that you are running the command mode
- you can access this mode using
icommand mode - to exit the command mode use
esc
- you can use this mode to play around with some commands
- Command in this mode is prefix to this column
- visually select some text and run commands over that section of code. You can switch to this mode by pressing
vfrom the command mode. - prefacing a movement command with a number will execute that movement multiple times. So, if you want to move up six lines, enter 6k and Vim will move the cursor up six lines. If you want to move over five words, enter 5w. To move 10 words back, use 10b.
:edit sample.txtuse to create or edit file in vim:wto save the file in command line mode:qto close the file:wqto save and quit:q!to quit the vim without saving the changesvim -R sample.txtopen file in read only mode0orentermove the cursor to beginning of file$moves cursor to end of linewmoves cursor forward by one wordbmoves backwardGmoves to end of file andggto the top of file- `. move to the last edit.
Iinsert mode in start of lineato append text after cursorAswitch to insert mode and move cursor to end of lineoswitch to insert mode and move cursor to new lineOswitch to insert mode and move cursor above and create new linesswitch to insert mode and delete the char (Substitute text)Sswitch to insert mode and delete entire line (Substitute text)ccswitch to insert mode and delete entire line (Substitute text)Cswitch to insert mode and delete entire front of cursor (Substitute text)pto paste after cursorPto paste before cursorRto replace single char:njump to nth linewjump to start of next wordNfor next occurrence andnfor previous occurrenceejump to end of current wordbjump to beginning of previous word:swapnameSwap area is a file created by Vim to store buffer contents periodically. While editing file our changes may be lost because of any reasons and Vim provides swap files to provide data recoveryxdelete char from cursor posXdelete previous char from cursor posdwdelete word from cursor position aDdelete entire line from cursor posdddelete entire lineycopy entire lineuto undo changes/to search expression in forward directionnfor next occurrence andNfor previous occurrence//repeat previous forward search
?to search expression n backward directionNfor next occurrence andnfor previous occurrence??repeat previous backward search
*when on word shift cursor to next occurrence#to previous word
Finding text and replacing it with some other text is simple and straightforward in Vim. There's a one-line command that simplifies this entire process.
Here's the syntax:
:[range]s/{pattern}/{string}/[flags]
Let's dismantle each part and understand how it all works.
- [range] indicates that you can pass the range of lines. Pass % to find and replace in all lines. The range is separated by a comma. To find and replace between lines 5 to 10, pass 5,10. Use . to represent the current line and $ the last line of the file.
- {pattern} indicates the pattern to find the text. You can pass regex patterns here.
- {string} is the string to replace in the found text.
- [flags] indicates if you wish to pass any additional flags (for example, the flag c is passed to confirm before replacing the text). By default, this does a case-sensitive search. You can change it to do a case-insensitive search by passing a i flag.