Linux Working with files Saleh Khazaei

36
Linux Working with files Saleh Khazaei http://ceit.aut.ac.ir/~Khazaei/linux

description

VIM

Transcript of Linux Working with files Saleh Khazaei

Page 1: Linux Working with files Saleh Khazaei

LinuxWorking with files

Saleh Khazaeihttp://ceit.aut.ac.ir/~Khazaei/linux.pptx

Page 2: Linux Working with files Saleh Khazaei

Commands Cat – prints context of a filemore / less head / tail ( e.g. -n10 )filegrep ( -i , -n , -c )wc ( -c , -m , -l , -w )nanogeditgeanyQt

Page 3: Linux Working with files Saleh Khazaei

VIM

Page 4: Linux Working with files Saleh Khazaei

What is VIMVim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems

Page 5: Linux Working with files Saleh Khazaei

The Programmers’ Editor !

Vim is often called a "programmer's editor," and so useful for programming that many consider it an entire IDE. It's not just for programmers, though. Vim is perfect for all kinds of text editing,from composing email to editing configuration files.

Page 6: Linux Working with files Saleh Khazaei

It could be distributed freely!

Vim is under the license of GPL which means it could be distributed freely.

Page 7: Linux Working with files Saleh Khazaei

Vim & Vi The Differences

- Vim has been ported to a much wider range of OS's than vi.

- Vim includes support (syntax highlighting, code folding, etc) for several popular programming languages (C/C++, Python, Perl, shell, etc).

- Vim integrates with cscope.- Vim can be used to edit files using

network protocols like SSH and HTTP.

- Vim includes multilevel undo/redo.- Vim allows the screen to be split for

editing multiple files.- Vim can edit files inside a

compressed archive (gzip, zip, tar, etc).

Page 8: Linux Working with files Saleh Khazaei

Vim & Vi The Differences

- Vim includes a built in diff for comparing files (vimdiff).

- Vim includes support for plugins, and finer control over config and startup files.

- Vim can be scripted with vimscript, or with an external scripting language (e.g. python, perl, shell).

Page 9: Linux Working with files Saleh Khazaei

Let’s Start!

Page 10: Linux Working with files Saleh Khazaei

Modes

Page 11: Linux Working with files Saleh Khazaei

Movement ( Only Command Mode )h – move leftl – move rightk – move upj – move down

Or you can also use the cursor keys in all three modes.

Page 12: Linux Working with files Saleh Khazaei

Movement ( Only Command Mode )w – start of the next word.e – end of the word.b – beginning of the word.

H – moves to the top of the page.M – moves to the middle of the page.L – moves to the end of the page.

Ctrl-f – scroll forward one screen , Ctrl-b – scroll backward one screen

Page 13: Linux Working with files Saleh Khazaei

Important Commands !•0 first column of the line•^  first non-blank character of the line•w  jump to next word•W  jump to next word, ignore punctuation•e  jump to word-end•E  jump to word-end, ignore punctuation•b  jump to word-beginning•B  jump to word-beginning, ignore punctuation•ge  jump to previous word-ending•gE  jump to previous word-ending, ignore punctuation•g_  jump to last non-blank character of the line•$  jump to the last character of the line

Page 14: Linux Working with files Saleh Khazaei

There are a lot of movement commands !

Page 15: Linux Working with files Saleh Khazaei

Text inserting is pretty simple !Just type “i” and start typing !

Page 16: Linux Working with files Saleh Khazaei

Vim offers these commands !• d  delete the characters from the cursor position up the position given by

the next command (for example d$ deletes all character from the current cursor position up to the last column of the line).

• c  change the character from the cursor position up to the position indicated by the next command.

• x  delete the character under the cursor.• X  delete the character before the cursor (Backspace).• y  copy the characters from the current cursor position up to the position

indicated by the next command.• p  paste previous deleted or yanked (copied) text after the current cursor

position.• P  paste previous deleted or yanked (copied) text before the current cursor

position.• r  replace the current character with the newly typed one.• s  substitute the text from the current cursor position up to the position

given by the next command with the newly typed one.• .  repeat the last insertion or editing command (x,d,p…).

Page 17: Linux Working with files Saleh Khazaei

Copy … Paste“ayy” copies the current line into register “a”.Pasting the contents of register “a” is done by “ap”.

“:registers” shows contents of the registers.

Page 18: Linux Working with files Saleh Khazaei

Visual Block !

1- Select a rectangle using Ctrl-V 2- Insert text by I or A3- Type! It’s pretty cool, isn’t it?!

To substitute the whole block with a new text select a block and type s !

To append some text at the end of some lines, use Ctrl-v$ and select the lines.

Page 19: Linux Working with files Saleh Khazaei

MOST IMPORTANT COMMANDS

Don’t be afraid to try the various commands, you can undo almost anything using “u” in the command mode

Even undo is undoable !!! :DUsing Ctrl-r

UNDO AND REDO

Page 20: Linux Working with files Saleh Khazaei

Search? / - Search in forward direction? – Search in backward directionn – repeat search in forward directionN – repeat search in backward direction

f – next occurrence of a characterF – previous occurrence of a character

( e.g. fo finds next o )

* - next occurrence of the word under cursor# - previous occurrence of the word under cursor

Page 21: Linux Working with files Saleh Khazaei

Replace:range s[ubstitute]/pattern/string/cgiI

c Confirm each substitution

g Replace all occurrences in the line (without g - only first).

i Ignore case for the pattern.I Don't ignore case for the pattern.

Page 22: Linux Working with files Saleh Khazaei

Replace:range s[ubstitute]/pattern/string/cgiI

Specifiers

Specifier Description

number an absolute line number

. the current line$ the last line in the file

% the whole file. The same as 1,$

't position of mark "t"

/pattern[/]the next line where text "pattern" matches.

?pattern[?]the previous line where text "pattern" matches

\/the next line where the previously used search pattern matches

\?the previous line where the previously used search pattern matches

\&the next line where the previously used substitute pattern matches

Page 23: Linux Working with files Saleh Khazaei

Regular Exp.

^ - beginning of the line $ - end of the line \<word\> […] – class of characters

Anchors"Escaped" characters or Metacharacters

# Matching # Matching

. any character except new line    

\s whitespace character \S non-whitespace

character\d digit \D non-digit\x hex digit \X non-hex digit\o octal digit \O non-octal digit

\hhead of word character (a,b,c...z,A,B,C...Z and _)

\H non-head of word character

\p printable character \P like \p, but excluding digits

\w word character \W non-word character

\a alphabetic character \A non-alphabetic

character

\l lowercase character \L non-lowercase character

\u uppercase character \U non-uppercase

character

Page 24: Linux Working with files Saleh Khazaei

Regular Exp.

Quantifier Description

*

matches 0 or more of the preceding characters, ranges or metacharacters .* matches everything including empty line

\+ matches 1 or more of the preceding characters...

\= matches 0 or 1 more of the preceding characters...

\{n,m} matches from n to m of the preceding characters...

\{n} matches exactly n times of the preceding characters...

\{,m}matches at most m (from 0 to m) of the preceding characters...

\{n,} matches at least n of of the preceding characters...

where n and m are positive integers (>0)

Quantifiers, Greedy and Non-Greedy

Page 25: Linux Working with files Saleh Khazaei

Regular Exp. Character ranges

[a-zA-z][^A-Z][01234567][-0-9]^ have it’s special meaning only if it’s the first character in the range

Page 26: Linux Working with files Saleh Khazaei

Regular Exp. Grouping and Backreferences

\( … \)

e.g. : :s/\(\w\+\)\(\s\+\)\(\w\+\)/hello

:s/(w+)(s+)(w+)/hello

Page 27: Linux Working with files Saleh Khazaei

Regular Exp. Replacement Part of :substitute

# Meaning # Meaning

& the whole matched pattern \L the following characters are made lowercase

\0 the whole matched pattern \U the following characters are made uppercase

\1 the matched pattern in the first pair of \(\) \E end of \U and \L

\2 the matched pattern in the second pair of \(\) \e end of \U and \L

... ... \r split line in two at this point

\9 the matched pattern in the ninth pair of \(\) \l next character made

lowercase

~ the previous substitute string \u next character made

uppercase

Page 28: Linux Working with files Saleh Khazaei

Regular Exp. Alternations

Using "\|" you can combine several expressions into one which matches any of its components. The first one matched will be used.

\(Date:\|Subject:\|From:\)\(\s.*\)

Page 29: Linux Working with files Saleh Khazaei

A Lovely Feature

Using Ctrl-p Vim searches the currently typed text backwards for a word starting with the same characters as already typed. Ctrl-x Ctrl-l completes the whole line.

Completion

Page 30: Linux Working with files Saleh Khazaei

Commands you shouldn’t forget!

:w ( save ):q ( quit ):q! ( quit without saving )ZZ – exiting VIM immediately !

u – UndoCtrl-r – Redo

Page 31: Linux Working with files Saleh Khazaei

If any problem …

Page 32: Linux Working with files Saleh Khazaei

First:help

Page 33: Linux Working with files Saleh Khazaei

Secondman vim

Page 34: Linux Working with files Saleh Khazaei

ThirdGoogle is your best friend

Page 35: Linux Working with files Saleh Khazaei

References[1] A. Nateghi, M. Behbooei, “An Introduction to VIM”, 2014.[2] M. Jakl, “Vim Introduction and Tutorial”, 2007 [Online], Available HTTPS://blog.interlinked.org/tutorials/vim_tutorial.html [Accessed: 3 Feb. 2015][3] “Vim Regular Expression”, 2002 [Online], Available HTTP://vimregex.com/ [Accessed: 3 Feb. 2015]

Page 36: Linux Working with files Saleh Khazaei

Thanks for your attention!If you have any questions, you could ask now