Skip to main content

Vim

Move cursor vim

Arrow key replacements

  • h - Left
  • j - Down
  • k - Up
  • l - Right

Move one Word

  • w - move the cursor to the RIGHT one word
  • b - move the cursor to the LEFT one word

Move to Start or End of LINE

  • ^ or 0 - move the cursor to the START of the line
  • $ - move the cursor to the END of the line

Move to Start or End of FILE

  • gg - move the cursor to the BEGINNING of the File
  • G - move the cursor to the END of the File

Move to Start or End of SCREEN

  • H - (High) move the cursor to the BEGINNING of the Screen
  • M - (Middle) move the cursor to the MIDDLE of the Screen
  • L - (Low) move the cursor to the END of the Screen

Scroll Screen vim

scroll down

  • Ctrl-f scroll forward one screen (f for forward or down )
  • Ctrl-d scroll forward one half of a screen (d for down)

scroll up

  • Ctrl-b scroll backward one screen (b for backward or up)
  • Ctrl-u scroll backward one half of screen (u for up)

Delete vim

delete word

  • dw - to delete char from cursor to the beginning of the next word

    Place the cursor at the beginning to delete the whole word.
    if the cursor is placed somewhere in the word, the command will delete from the cursor position to the end of the word.

  • diw - ignore the cursor position in the word and delete the whole word
  • dt<char> - delete the char's from the current cursor position till the first encounter of the char <char>

delete till end of line

  • d$ - Delete the char's from the current cursor position till the end of the line.

delete line

  • dd - Delete the whole line.

delete number of line UP

  • 2d- - Delete 3 lines UP from the current cursor position including the line of the cursor.

delete number of lines DOWN

  • 3dd - Delete 3 lines DOWN from the current cursor position including the line of the cursor.

delete till End of file

  • dG - Delete from the cursor position to the end of the file.

Advanced Vim operations

process the txt file with shell commands like grep, cut, jq etc without leaving vim

:%!grep succeeded
note

% - current file
! - execute the following command and input back into the file

Search and replace in vim

Replace /ope with ** in the file in vi mode
:%s/\/ope/**/g
Replace ** with /ope in the file in vi mode
:%s/\*\*/\/ope/g

delete in vim

Delete all the spaces

:%s/\s*//g

Delete the line with word

:g/searchword/d

Delete all the lines excluding

Delete all the line excluding the lines containing /bash

:g!/\/bash/d

replace in vim

replace all new line char with ,

:%s/\n/,/g ( Replace new line char with , )
note

if \n not working try \r

Replace all comma with new line

:%s/,/\r/g

Append in vim

Append string at end of each line

:%norm Asomething"

% apply on each line
norm execute the commands followed after norm. i.e command A
A Append command in Vim
something string to append

Insert in vim

Insert at the BEGINNING of each line

Insert hello at the beginning of each line

:%s/^/hello : /

Insert new line at the END of each line

Insert NEW LINE at the beginning of each line

:%s/$/\r/g

copy in vim

Copy all the lines to clipboard

gg"*yG

undo or redo in vim

  • u - Undo
  • ctrl + r - Redo

set line numbers

Numbers each line down the left side.

:set number

json format the file in vim

format json file in vim

:%!jq .

Append at the end of file

Go to bottom of the file and in append text mode. In other words, jump to last line and start writing code/text.

Esc + G + A 

Record macro with @q

  1. In command mode hit qa - this starts recording for macros with a as command
  2. Do the changes needed to repeat with the command a
  3. exit insert mode and then hit q to stop the recording
  4. in command mode enter @a - This will repeat the macro recorded for a

Ref

vim command

oracle vim ref