Laboratorium Komputerowe Progmar
Marcin Załęczny

We are using cookies in the page. If you use the page you agree for the cookies.      Close

Vim editor - commands and shortcuts

Opens a file main.c for edition. If the file didn't exist it will be created after first save: vim main.c

Vim works either in command mode or in insert mode. If in the insert mode then at the bottom of the screen there is a text INSERTING and any keypress inserts character in the opened file. To switch from the insert mode to command mode just press ESC key. To enter insert mode from command mode just press any combination of following: a or SHIFT+A or i or SHIFT+I

Following command closes vim editor. If file wasn't saved before then any changes will be discarded: :q!

Turning on line numeration (the ordinary number will be displayed to the left of each line): :set number

Turning off line numeration: :set nonumber

Removes a character at the cursor (in command mode): x

Removes a character to the left of the cursor (in command mode): SHIFT+X

Removes characters from current position of the cursor to the end of current word (in command mode): dw

Removes current line (in command mode): dd

Reverting last change (in command mode): u

Restoring current line to the state before its edition (in command mode): SHIFT+U

Redoing reverted change (in command mode): CTRL+R or :redo

Inserts (in command mode) new empty line below the cursor, moves the cursor to the line and switches to the insert mode: o

Inserts (in command mode) new empty line above the cursor, moves the cursor to the line and switches to the insert mode: SHIFT+O

Removes (in command mode) characters from current cursor position to the end current word and switches to insert mode: cw

Inserts in insert mode End of page character: CTRL+L

Saves changes to the disk and exits editor (in command mode): ZZ or :wq

Saves changes (in command mode): :w

Saves changes to the specified file (in command mode): :w file_name

Saves changes to the specified file (in command mode). If file already exists, then it will be overwritten (if permissions allows): :w! file_name

Displays extra status line at the bottom of screen. The line contains current file name and cursor coordinates (in command mode): :set laststatus=2

Opens new window for the current file (in command mode): CTRL+W S

Switches to window of specified number (in command mode): CTRL+W number, np. CTRL+W 2

Switches to window above (in command mode): CTRL+W ARROW_UP

Switches to window below (in command mode): CTRL+W ARROW_DOWN

Closes current window (in command mode): CTRL+W Q

Opens an empty document in new window (in command mode): CTRL+W N

Displays list of vim swap files located in current directory (in shell): vim -r

Opens file main.c using existing swap file main.c.swp (in shell): vim -r main.c

Moving cursor one character to the right (in command mode): ARROW_RIGHT or l or SPACE

Moving cursor one character to the left (in command mode): ARROW_LEFT or h

Moving cursor ten characters to the right (in command mode): 10ARROW_RIGHT or 10l or 10SPACE

Moving cursor ten characters to the left (in command mode): 10ARROW_LEFT or 10h

Moving cursor to the next occurence of character i in current line (in command mode): fi

Moving cursor to the previous occurence of character i in current line (in command mode): Fi

Repeats the last finding operation (f or F) (in command mode): ;

Moving cursor to the next word (punctuation characters are treated as a beginning of a next word) (in command mode): w

Moving cursor to the next word separated by a whitespace character (in command mode): SHIFT+W

Moving cursor to the previous word (punctuation characters are treated as a beginning of a previous word) (in command mode): b

Moving cursor to the previous word separated by a whitespace character (in command mode): SHIFT+B

Moving cursor to the end of current/next word (punctuation characters are treated as a separated word) (in command mode): e

Moving cursor to the end of current/next word separated by a whitespace character (in command mode): SHIFT+E

Moving cursor to the beginning of next line (in command mode): ENTER

Moving cursor to the next line (in command mode): j or ARROW_DOWN

Moving cursor to the previous line (in command mode): k or ARROW_UP

Moving cursor to the beginning of previous line (in command mode): -

Moving cursor to the beginning of next sentence (in command mode): )

Moving cursor to the beginning of next paragraph (in command mode): }

Moving cursor to the beginning of current/previous sentence (in command mode): (

Moving cursor to the beginning of current/previous paragraph (in command mode): {

Moving cursor to the beginning of the first line on the screen (in command mode): SHIFT+H

Moving cursor to the beginning of the middle line on the screen (in command mode): SHIFT+M

Moving cursor to the beginning of the last line on the screen (in command mode): SHIFT+L

Scrolling down a half of screen (in command mode): CTRL+D

Scrolling up a half of screen (in command mode): CTRL+U

Scrolling down one screen (entire screen without one line) (in command mode): CTRL+F or PAGE_DOWN

Scrolling up one screen (entire screen without one line) (in command mode): CTRL+B or PAGE_UP

Moving cursor to the last line in the document (in command mode): SHIFT+G

Moving cursor to the 10th line in the document (in command mode): 10SHIFT+G

Replacing current character (in command mode): r new_character

Replacing text at current cursor position with new characters until ESC was pressed (in command and insert mode): SHIFT+R new_text

Removes current line and the next one too (in command mode): d Enter

Swapping curent character and the next one (in command mode): xp

Swapping curent line and the next one (in command mode): ddp

Removes current character (in command mode): dl

Removes all characters from the beginning of current line to the current character (in command mode): d0

Removes all characters from the first non blank space character in current line to the current character (in command mode): d^

Removes characters from the current position to the beginning of the next word (in command mode): dw

Removes characters from the current position to the beginning of the next third word (in command mode): d2w

Removes characters from the current position to the beginning of the next word (separated by white space character) (in command mode): dSHIFT+W

Removes characters from the current position to the beginning of the current word (in command mode): db

Removes characters from the current position to the beginning of the current word (separated by white space character) (in command mode): dSHIFT+B

Removes characters from the current position to the beginning of the second previous word (separated by white space character) (in command mode): d2SHIFT+B

Removes characters from the current position to the end of the current sentence (in command mode): d)

Removes characters from the current position to the beginning of the current sentence (in command mode): d(

Removes characters from the current position to the end of the current paragraph (in command mode): d}

Removes characters from the current position to the beginning of the current paragraph (in command mode): d{

Removes characters from the current position to the next occurence of the specified text (in command mode): d/text

Removes characters from the current position to the next occurence of the 'i' character including it (in command mode): dfi

Removes characters from the current position to the next occurence of the 'i' character excluding it (in command mode): dti

Removes characters from the current position to the end of current line (in command mode): SHIFT+D

Removes characters from the current position to the end of current line (in command mode): d$

Removes current line (in command mode): dd

Removes lines from the current one to the last line visible on the screen (in command mode): dSHIFT+L

Removes lines from the first line visible on the screen the current one (including it) (in command mode): dSHIFT+H

Removes lines from the current one to the end of the file (in command mode): dSHIFT+G

Removes lines from the current one to the beginning of the file (in command mode): d1SHIFT+G

Removes characters from the current one to the fourth next occurence of the 'i' character in current line (in command mode): d4fi

Removes current character and switches to the insert mode in place of removed character (in command mode): s

Removes all characters in current line and switches to the insert mode at the beginning of the line (in command mode): SHIFT+S

Removes four characters starting from the current one and switches to the insert mode in its place (in command mode): 4s

If current character is a letter then changes its size to the opposite size (ex. a <=> A) and moves cursor to the next character. If current character is not a letter then just moves cursor to the next character. (in command mode): ~

Changes next four letters to the letters of the opposite size and moves cursor to the next character after processed block. Does not change characters that are not letters. (in command mode): 4~

Searches forward for the specified text and places cursor at its beginning (text is a regular expression) (in command mode): /text

Searches backward for the specified text and places cursor at its beginning (text is a regular expression) (in command mode): ?text

Repeating the last search (in command mode): n

Repeating the last search in the opposite direction (in command mode): SHIFT+N

Turns on the incremental search (in command mode) (incremental search means that vim highligts first found - forward '/' or backwrd '?' - text according to the typed in characters): :set incsearch

Turns off the incremental search (in command mode): :set noincsearch

:[g][where]s/text-pattern/new-text[/option] - replaces found text-pattern with the new-text in all lines ([g]) that matches [where] condition, ex. (in command mode): :5s/text-pattern/new-text - replaces in 5th line :5,10s/text-pattern/new-text - replaces in lines starting at 5th to 10th including :5,.s/text-pattern/new-text - replaces in lines starting at 5th to the current including :5,$s/text-pattern/new-text - replaces in lines starting at 5th to the last one including :1,$s/text-pattern/new-text - replaces in the whole file :%s/text-pattern/new-text - replaces in the whole file :/string/s/text-pattern/new-text - replaces in the next line that contains specified string :.,+4s/text-pattern/new-text - replaces in the next 4 lines starting at current one including (summary in 5 lines) :g/string/s/text-pattern/new-text - replaces in all lines that contain specified string (only first occurence in each line) :g/string/s/text-pattern/new-text/g - replaces in all lines that contain specified string (all occurences in each line) :g/string/s/text-pattern/new-text/c - replaces in all lines that contain specified string (only first occurence in each line). Before each replacement asks for confirmation, you can answer one of following:

  • y - applies replacement
  • q - quits replacement process
  • l - applies replacement and quits replacement process
  • a - applies all further replacements
  • n - skips current replacement and searches for the next one

Joins current line with the next one by removing current line's new line character. Additionally adds a space character between the lines and moves cursor to that space character. If current line ends with the period ('.') then two spacebars are inserted as a glue. (in command mode): SHIFT+J

Displays in status bar informations about file being edited: file name, information about if it is modified or it is read-only, number of rows and percentage position in file. (in command mode): CTRL+G or :f

Repeats last operation that has modified the file (in command mode): .

Copies characters starting at current cursor position to the end of the current word and places it in the general purpose buffer. Next you can insert the buffer's content in another location by pressing p character (in command mode). Command y has the same syntax as command d described above. (in command mode): yw

Copies current line to the general purpose buffer (in command mode): yy or SHIFT+Y

Inserts after current cursor position the general purpose buffer's content (in command mode): p

Inserts before current cursor position the general purpose buffer's content (in command mode): SHIFT+P

The named buffers has identifiers that consists of one letter: a-z. Copying current line to the named buffer a and to the general purpose buffer (in command mode) can be done by entering command (in command mode): "ayy

Pastes text from a named buffer (in command mode): "ap Any further operations of copying/cutting text to the general purpose buffer cause changing only this one buffer. a named buffer and any other named buffer stays untouched.

In vim there are also buffers named from 1 to 9. These buffers are readonly and they contain most recently deleted fragments of text of length at least one row. Most recently deleted text occupies 1 buffer, last but one occupies 2 buffer and so on. Command below inserts into document content of 1 buffer (in command mode): "1p

Load text from specified file into the current document in line below the current one (existing content of current document stays untouched) (in command mode): :r file_name.txt

Load text from specified file into the current document in line below the specified one (existing content of current document stays untouched) (in command mode): :[line_number]r file_name.txt

Appends current document to the specified file (text is appended at the end of the file) (in command mode): :w>> file_name.txt

Sets mark named a in current line (in command mode): ma

Go to the beginning of the line that conains mark named a (in command mode): 'a

Removes text from current cursor position to the line containing mark named a (in command mode): d'a

Starts a new session of editing current file. All changes applied since last file write are discarded. The general purpose buffer, all named and ordered buffers stay untouched. (in command mode): :e!

Starts a new session of editing specified file. All changes applied to current file since last file write are discarded. The general purpose buffer, all named and ordered buffers stay untouched. (in command mode): :e! file_name.txt

Closes current file and opens last edited one (in command mode): :e#

Creates a text abbreviation named HW (in command mode): :abbrev HW Hello world! Since now every typed in HW text in insert mode will be replaced by text Hello world! with one space character followed.

Runs shell command/program and pastes its output to the current line (it is replaced by a new text) (in command mode): !! shell_command

How to sort list of names: Marcin
Antek
Paweł
Bartek
Janusz
Go to the beginning of line with Marcin name and set i mark (in command mode): mi Next go to the beginning of the line with Janusz name and issue following command (in command mode): !'isort