Skip to content

Instantly share code, notes, and snippets.

@rrhinox
Last active June 15, 2023 16:53
Show Gist options
  • Save rrhinox/0ffeea98bc3920151f7cb14d2dd69c88 to your computer and use it in GitHub Desktop.
Save rrhinox/0ffeea98bc3920151f7cb14d2dd69c88 to your computer and use it in GitHub Desktop.
My favorite and useful functions of vi / vim editor

With a big thank you to the internet for saving my days - my favourite Vim shortcuts

Good and Fun Wiki

All of the following must be run outside of the -- INSERT -- mode

Copy, paste, cut or delete shortcuts

# copy the line
    yy

# paste the copied or cut line
    p

# change word 
    cw

# cut the line
    dd 

#  cuts a user-defined number of rows
    ${number of rows} + dd

# cut all rows from where you are to the end of the file
    d + Shift + G

# will delete all rows from where you are to the beginning of the file
    d + g

# will delete from the current line to the end of file 
    d + G

Sort Command

Examples:

	:sort 

Toggle line number

Examples:

  	:set nu 

Substitute a pattern on the entire file

  • :[%s] substitute /pattern_to_find/pattern_to_substitute/

Examples:

	:%s/http:/https:/
	:%s/https:\/\/www/https:\/\/test-liferay/gc
	:%s/10\.1\.1\.67/10\.1\.102\.43

Execute a command on lines which match a regex.

Examples:

	:g/^$/d

Convert ^M (Windows) line breaks to normal line breaks

Example:

  ### Note - :e ++ff=dos followed by :set ff=unix will convert the endings to a sane format.

  :e ++ff=dos

Insert text at beginning of a multi line selection

https://stackoverflow.com/questions/253380/how-to-insert-text-at-beginning-of-a-multi-line-selection-in-vi-vim

Example:

	%s!^!<Value><\![CDATA[

Add text at the end of each line

Example:

	:%s/$/]]><\/Value>/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment