ZURÜCK

vi & regex Kurzreferenz

Kurzreferenz für vi, regex, sh/bash and sed/awk.

Tags: vi, regex, sh, bash, sed, Linux

vi Cheat Sheet

Edit mode
-------------------------------------------------------------------------
i                  insert at cursor position
a                  insert after cursor position
A                  append at end of current line
o                  insert at new line after cursor
O                  insert at newline before cursor
[ESC]              leave edit mode

Command mode
-------------------------------------------------------------------------
:w!                write file to disk
:q!                quit
:wq!               write file to disk and quit
ZZ                 "
u                  undo
dd                 delete current line
C                  clear-edit the current line from cursor
D                  delete the current line from cursor
cw                 change the current word from cursor
dw                 delete the current word from cursor

Text replacement
-------------------------------------------------------------------------
[NUM][COMMAND]     repeat COMMAND NUM times
5dd                delete five lines
7C                 clear the next 7 lines from cursor

Y                  copy current line
P                  paste text after the current line
p                  paste text prepending the current line
v   characer-based visual selection
                   use "y" to copy selected text
V   line-based visual copy
                   use "y" to copy selected text

:%s/\s\+$//        delete trailing spaces at the end of each line
:%s/^\s\+          delete all leading spaces at the beginning of each line
:%s/\s\+/ /g       replace multiple whitespaces (or tabs) with one whitespace

:g/^$/d            delete empty lines
:g/^$\|^#\|^;/d    delete empty lines and lines starting with # or ;
  

regex Cheat Sheet

Character  Legend                    Example                Sample Match
-------------------------------------------------------------------------
\d         One digit                 file_\d\d              file_25
\D         One character that is not a digit
                                     \D\D\D                 ABC
\w         One "word character": letter, underscore or digit
                                     \w-\w\w\w              A-b_1
\W         One character that is not a word character
                                     \W\W\W\W\W             *-+=)
\s         One white space character (e.g.: a space or tab)
                                     ab\s\s\sc              ab   c
\S         One character that is not a space
                                     \S\S\S\S               Yoyo

Quantifier Legend                    Example                Sample Match
-------------------------------------------------------------------------
+          One or more               Version \w-\w+         Version A-b1_1
{3}        Exactly three times       \D{3}                  ABC
{2,4}      Two to four times         \d{2,4}                156
{3,}       Three or more times       \w{3,}                 regex_tutorial
*          Zero or more times        A*B*C*                 AAACC
?          Once or none              plurals?               plural

Character  Legend                    Example                Sample Match
.          Any character except new line
                                     a.c                    abc
.          Any character except new line
                                     .*                     whatever, man.
\.         A period (special character: needs to be escaped by a \)
                                     a\.c                a.c
\          Escapes a special character  \.\*\+\? \$\^\/\\   .*+? $^/\
\          Escapes a special character  \[\{\(\)\}\]        [{()}]

Logic      Legend                    Example                Sample Match
-------------------------------------------------------------------------
|          OR operand                22|33                  33
()         Capturing group           A(nt|pple)             Apple
\1         Contents of Group 1       r(\w)g\1x              regex
\2         Contents of Group 2       (\d\d)\+(\d\d)=\2\+\1  12+65=65+12
(?:        Non-capturing group       A(?:nt|pple)           Apple

Character  Legend                    Example                Sample Match
-------------------------------------------------------------------------
\t         Tab                       T\t\w{2}               T     ab
\r         Return character          see below
\n         New line character        see below
\r\n       New line in Windows       AB\r\nCD               AB
                                                            CD

Quantifier Legend                    Example                Sample Match
-------------------------------------------------------------------------
+          The + (one or more) is "greedy"
                                     \d+                    12345
?          Makes quantifiers "lazy"  \d+?                   1 in 12345
*          The * (zero or more) is "greedy"
                                     A*                     AAA
?          Makes quantifiers "lazy"  A*?                    empty in AAA
{2,4}      Two to four times, "greedy"
                                     \w{2,4}                abcd
?          Makes quantifiers "lazy"  \w{2,4}?               ab in abcd

Character  Legend                    Example                Sample Match
-------------------------------------------------------------------------
[stuff]    One of the characters in the brackets
                                     [AEIOU]                One uppercase vowel
-          Range indicator           [a-z]                  One lowercase letter
[stuff]    One of the characters in the brackets
                                     [AB1-5w-z]             One of either:
                                                            A,B,1,2,3,4,5,w,x,y,z
[stuff]    One of the characters in the brackets
                                     [A-Z]+                 GREAT
[^x]       One character that is not x
                                     [^a-z]{3}              A1!
[\d\D]     One character that is a digit or a non-digit
                                     [\d\D]+                Any characters,
                                                            including new line

Anchor     Legend                    Example                Sample Match
-------------------------------------------------------------------------
^          Beginning of line (but means "not" inside [^brackets])
                                     ^abc .*                abc (line start)
$          End of line               .*? the end$           this is the end
\A         Beginning of string       \Aabc[\d\D]*           abc (string...
                                                            ...start)
\Z         End of string             [\d\D]*the end\Z       this is...
                                                            ...the end
\b         Word boundary             Bob.*\bcat\b           Bob ate the cat
\B         Not a word boundary       Bob.*\Bcat\B.*         Bobcats

Character  Legend                    Example                Sample Match
-------------------------------------------------------------------------
[:alpha:]  Letters                   [8[:alpha:]]+          WellDone88
[:alnum:]  Letters and numbers       [[:alnum:]]{10}        ABCDE12345
[:punct:]  Punctuation marks         [[:punct:]]+           ?!.,:;

Anchor     Legend                    Example                Sample Match
-------------------------------------------------------------------------
(?i)       Case-insensitive           (?i)Monday            monDAY
(?s)       The dot (.) matches new line characters
                                     (\r\n) (?s)ABC.*to Z   ABC
                                                            to Z
(?m)       Treats the string as multiple lines, so that ^ and $ can match
           in several places          (?m)1\r\n^2$\r\n^3$   1
                                                            2
                                                            3
(?x)       Comment mode (aka whitespace mode)
                                     (?x) # this is a       abc d
                                     # comment
                                     abc # on multiple
                                     # lines
                                     [ ]d # spaces must be
                                     # in brackets

Lookaround Legend                    Example                Sample Match
-------------------------------------------------------------------------
(?=        Positive lookahead        (?=\d{10})\d{5}        01234 in 0123456789
(?<=       Positive lookbehind       (?<=\d)cat             cat in 1cat
(?!        Negative lookahead        (?!theatre)the\w+      theme
(?<!       Negative lookbehind       \w{3}(?<!mon)ster      Munster
  

sh/bash Cheat Sheet

Substring extraction and removal
STRING='12345abcdefg.nm.xyz'
Expression         Legend                                   Result
-------------------------------------------------------------------------
${STRING:4}        Extracts from position 4                 5abcdefg.nm.xyz
${STRING:3:5}      Extracts 5 characters from position 3    45abc
${STRING:(-5)}     Extracts 5 characters from the end       m.xyz
${STRING#*.}       Delete shortest match of '*.'            nm.xyz
${STRING##*.}      Delete longest match of '*.'             xyz
${STRING%.*}       Delete shortest match of '.*' from back  12345abcdefg.nm
${STRING%%.*}      Delete longest match of '.*' from back   12345abcdefg
${STRING/./-}      Replace first '.' with '-'               12345abcdefg-nm.xyz
${STRING//./-}     Replace all '.' with '-'                 12345abcdefg-nm-xyz
  

sed Cheat Sheet

Substring extraction and removal
Expression         Legend
-------------------------------------------------------------------------
s/\./-/g           replace any '.' with '-'
/^$/d              delete empty lines
/^$\|^#\|^;/d      delete empty lines and lines starting with # or ;
s/\s\+/ /g         replace multiple whitespaces (or tabs) with one whitespace
/^$/d;s/$/;/g      delete empty lines and append ';' to every line (2 commands)