Saturday, October 13, 2012

Useful commands in Vim


Vim is text editor based on Vi. It is very powerful program At first look it don't look invitingly but when you start deeper understanding of Vim you can see how siginficantly is speedup of writing. Vim have countless amount of settings which can wriete on ~/.vimrc file and which will be set during the opening as a default. Vim have a dozen work mode. A few the most principal are:
NORMAL - it is default mode to change for it press a <Esc>
INSERT - insert new tekst mode to strat press <i> to start writing in front of cursor and press <a> at the back of cursor. Also you can press <o><O> to start writing in the next line.
COMMAND LINE/Ex - command line mode. To start press a <:> key
VISUAL - visual mode where you can mark tekst. Press <v> to start this mode.
REPLACE - replace mode wehre some chars are replace by other chars. To start this mode press a <R>

Undermentioned commands are merely small piece that what Vim can. They serve of lern the usually using and the most useful. This list will be extend with common service of Vim. Tekst between [] means alternative nameof command. Tekst between <> means key from keyboard.

In NORMAL mode also in VISUAL mode:
<?> or </> searching a string in file. Start it at cusor position and search backwards or ahead.
<$> or <0> go to end or beginning of line.
<gg> or <G> go to begining or end of file.
<w> or <e> go to beginning or end of next word in file.
<b> or <ge> back to beginnig or end of previous word in file.
<number><G> go to linie given as a <nubmer>.
<x> delete char under the cursor.
<d$> delete chars to the ond of line.
<dd> delete entire line.
<de> or <dw> delete entire word without or with space.
<r><char> change char under cursor to given in <char>.
<J> merge to lines which will be separate by space.
<p> put the text from cache memory.
<ga> show ASCII value form char under the cursor.
<u> or <ctr+r> undo and redo changes.(default even last 1000 changes)
<.> redo last command.

In COMMAND LINE/EX mode:

Movement in Ex mode:
To search available commands we can write part of command and press <ctrl+d>
:set i<Ctrl+d>
icon         ignorecase   imsearch     includeexpr  indentexpr   infercase    isfname      iskeyword
iconstring   iminsert     include      incsearch    indentkeys   insertmode   isident      isprint
We can complete our command if is only one posible command and when we press <tab> and  it fill out autonaticly. But it is more fiting commands.Then when we press <tab> scoundly our command will be change.
:se[set] i<tab>
:set icon <tab>
:set ignorecase

To split screen on equal parts we can use:
:split name_of_file
We can also use word vertical to split teh screen vertically:
:vertical split
<ctrl+w><ctrl+w> serve to change a using window.
Except searching commands <tab> we can also searching file names from recent directory:
:split n<tab>
We have to files in recent directory:
name_of_file
name_of_file2
To complete the longest common string we can use:
:split n<ctrl+l>
To establish new screen with determined size :
:15split name_file determine a new screen with open name_file file as a contens with size on 15 lines.
We can also change size of screens already opened.
<number><ctrl+w><_> determine screen on size given in <number>
<ctrl+w><=> align all screens.
To open screen  with contens of recent directory:
:Sex[Sexplore]

It also posible to add tabs using command:
:tabnew path/file_name
To moving between tabs we can use:
:tabn, :tabp, :tabfir, :tablast, :tabn2
We can also open new tans from shell grade:
vim -p fiename1 filename2 it open Vim with two tabs for filename1 and filename2 files.

To simplify searching before using commands we can use:
:history this command show all our history of using commands.
<q:> and in this way you can open menu wehre you can chose command from history. YOu can move tehre like in NORMAL mode.

To make easier to  searching strings charakters we can set:
:set ic set ignore a case-sensitive during the searching.
:set hls[hlsearch] is[incsearch] hls command illumine finding strings and is command show up to date strings during it wiriting.
:set noic it end ignore a case-sensitive.
:nohlsearch it remove illumine finding strings.

To execute commands from shell we can use <:!>. To write it on open file:
:w[write] !{shell command} or !!{shell command} it execute command from shell and write on recent open file.
!! date it put our recent date.

Command responsible for sessions in Vim:
:mksession mysession.vim it write current session with all settings.
:source mysession.vim it read session from file
We can straightaway Vim with setting the session:
vim -S mysesion.vim

To set default settings we have to overwrite ~/.vimrc file:
:edit ~/.vimrc
We can also read example file:
:read $VIMRUNTIME/vimrc_example.vim

To replace one string with another string we use:
:s[substitute]/cow/pig/ it replace string "cow" with string "pig" in recent line and only one strings will be changed.
:s/cow/pig/g it will change all strings in line
:s/cow/pig/gc it will change all strins in line and every time it ask us to confirm removing.
:%s/cow/pig/g it will change all strings in entire file.
We can enforce it to change all strings only in particular place:
21s/cow/pig/g it wiil change all strings in 21 line.
5,21s/cow/pig/g it will change all strings between 5 and 21 line.

Friday, October 5, 2012

Linux and processes


Process is a instance of a computer program that is been executed. Each porcess have assign resources through operating system such as: processor, memory, access to intput-output devices. This resources are destined only for this process and it isn't share with other processes This is featue which distinguish between processes and threads. To manage the processes is responding a kernel. To create a new process is required to execute a system command fork or his derivative. Each processes have in linux:
- process identifier PID
- parent process identifier PPID
- process name CMD
- user name USER
- process piority queue PRI and NI
To display processes working on computer is command ps with:
-a display processes from all users
-x display list of demons porcess
-l display a additional information about porcess

user:~$ ps -ax
  PID          TTY     STAT      TIME COMMAND
    1 ?       Ss       0:01          /sbin/init
    2 ?       S        0:00          [kthreadd]
    3 ?       S        0:00          [ksoftirqd/0]
                   ...
 2459 ?       S        0:00          gnome-pty-helper
 2461 pts/0   Ss       0:00          bash
 2516 pts/0   R+       0:00          ps -ax

To display dinamic real-time view of runing processes is top command. To display ruining procesess as a tree is pstree command.
First  process who is open on linux is init process with PID=1. It is parent for all other procesess which we call them a child process. Scripts are open through init process and are locate in /etc/init.d and they are usually use to booting computer. Process init may by on varying levels of working wehre:
0 - halt the system
1 - single user mode
2-5- multi user modes
6 - reboot the sytem
To check levels of working we execute command who -r.

user:~$ who -r
run-level 2  2012-10-06 18:44

Init process is also demon process because it don't need interaction with human and run as a backgound peocess. Tradicionally demon names end with letter 'd' for example: ftpd, httpd and so on. To comunication with running process is command kill which send signals. To check what kinds of signals we can send we use kill -l. More ofen than not are ues:
-1  IGHUP read in again configuration file without terninate the process
-2  SIGTERM is default signal and it's sending requst to a process to request its termination and let the process to perform  appropriate termination.
-9  SIGKILL cause terninate immediately
-19 SIGSTOP stop a process for later resumption
-18 SIGCONT restart a process previously paused
To send signals to group of processes with the same name is killall command.

user:~$ sleep 100 &
[1] 2546
user:~$ sleep 200 &
[2] 2547
user :~$ killall sleep
[1]-  Zakończony             sleep 100
[2]+  Zakończony             sleep 200

Zombie proces is that which has compled execution but still has entry in the process table because its parent don't execute its child's exit status.
To change the  process priority we use renice command. To open the program with determining priority process we use nice comand. This commands change NI value which can impact on PRI value. When  NI have less value then have bigger priority.

nice -10 k-meleon

To open any process in the background we can add & on the end of command. Owing to & during working  process in the background we back immediately to the command line.To display process  in the background we use jobs command.

user@K52Je:~$ jobs
[1]-  Running                 sleep 200 &
[2]+  Running                 sleep 100 &

To bring the process back in the foreground we use fg command. To suspend a process we klick Ctrl+Z. We can move back in the bacground using bg command or in the  foregroud using fg command.

user@K52Je:~$ fg 2
sleep 100