ult/session4.rst
changeset 99 799f1c2a0689
parent 64 fb96a1e1c38c
equal deleted inserted replaced
98:678c7c01b5d5 99:799f1c2a0689
   224 Now, it would also be nice to have the list ordered in the decreasing order of the frequency of the appearance of the words. We sort the output of the ``uniq`` command with ``-n`` and ``-r`` options, to get the desired output. 
   224 Now, it would also be nice to have the list ordered in the decreasing order of the frequency of the appearance of the words. We sort the output of the ``uniq`` command with ``-n`` and ``-r`` options, to get the desired output. 
   225 ::
   225 ::
   226 
   226 
   227   $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
   227   $ grep  "[A-Za-z]*" -o alice-in-wonderland.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr
   228 
   228 
   229 Basic editing and editors
       
   230 =========================
       
   231 
       
   232 vim
       
   233 ---
       
   234 Vim is a very powerful editor. It has a lot of commands, and all of them cannot be explained here. We shall try and look at a few, so that you can find your way around in vim. 
       
   235 
       
   236 To open a file in vim, we pass the filename as a parameter to the ``vim`` command. If a file with that filename does not exist, a new file is created. 
       
   237 ::
       
   238 
       
   239   $ vim first.txt
       
   240 
       
   241 To start inserting text into the new file that we have opened, we need to press the ``i`` key. This will take us into the *insert* mode from the *command* mode. Hitting the ``esc`` key, will bring us back to the *command* mode. There is also another mode of vim, called the *visual* mode which will be discussed later in the course. 
       
   242 
       
   243 In general, it is good to spend as little time as possible in the insert mode and extensively use the command mode to achieve various tasks. 
       
   244 
       
   245 To save the file, use ``:w`` in the command mode. From here on, it is understood that we are in the command mode, whenever we are issuing any command to vim. 
       
   246 
       
   247 To save a file and continue editing, use ``:w FILENAME``
       
   248 The file name is optional. If you do not specify a filename, it is saved in the same file that you opened. If a file name different from the one you opened is specified, the text is saved with the new name, but you continue editing the file that you opened. The next time you save it without specifying a name, it gets saved with the name of the file that you initially opened. 
       
   249 
       
   250 To save file with a new name and continue editing the new file, use ``:saveas FILENAME``
       
   251 
       
   252 To save and quit, use ``:wq``
       
   253 
       
   254 To quit, use ``:q``
       
   255 
       
   256 To quit without saving, use ``:q!``
       
   257 
       
   258 Moving around
       
   259 ~~~~~~~~~~~~~
       
   260 
       
   261 While you are typing in a file, it is in-convenient to keep moving your fingers from the standard position for typing to the arrow keys. Vim, therefore, provides alternate keys for moving in the document. Note again that, you should be in the command mode, when issuing any commands to vim. 
       
   262 
       
   263 The basic cursor movement can be achieved using the keys, ``h`` (left), ``l`` (right), ``k`` (up) and ``j`` (down). 
       
   264 ::
       
   265  
       
   266              ^
       
   267              k              
       
   268        < h       l >        
       
   269              j              
       
   270              v
       
   271 
       
   272 Note: Most commands can be prefixed with a number, to repeat the command. For instance, ``10j`` will move the cursor down 10 lines. 
       
   273 
       
   274 Moving within a line
       
   275 ++++++++++++++++++++
       
   276 
       
   277 +----------------------------------------+---------+
       
   278 | Cursor Movement                        | Command | 
       
   279 +========================================+=========+
       
   280 | Beginning of line                      | ``0``   |
       
   281 +----------------------------------------+---------+
       
   282 | First non-space character of line      | ``^``   |
       
   283 +----------------------------------------+---------+
       
   284 | End of line                            | ``$``   |
       
   285 +----------------------------------------+---------+
       
   286 | Last non-space character of line       | ``g_``  |
       
   287 +----------------------------------------+---------+
       
   288 
       
   289 Moving by words and sentences
       
   290 +++++++++++++++++++++++++++++
       
   291 +------------------------------+---------+
       
   292 | Cursor Movement              | Command |
       
   293 +==============================+=========+
       
   294 | Forward, word beginning      | ``w``   |
       
   295 +------------------------------+---------+
       
   296 | Backward, word beginning     | ``b``   |
       
   297 +------------------------------+---------+
       
   298 | Forward, word end            | ``e``   |
       
   299 +------------------------------+---------+
       
   300 | Backward, word end           | ``ge``  |
       
   301 +------------------------------+---------+
       
   302 | Forward, sentence beginning  | ``)``   |
       
   303 +------------------------------+---------+
       
   304 | Backward, sentence beginning | ``(``   |
       
   305 +------------------------------+---------+
       
   306 | Forward, paragraph beginning | ``}``   |
       
   307 +------------------------------+---------+
       
   308 | Backward, paragraph beginning| ``{``   |
       
   309 +------------------------------+---------+
       
   310 
       
   311 More movement commands
       
   312 ++++++++++++++++++++++
       
   313 +---------------------------------+------------+
       
   314 | Cursor Movement                 | Command    |
       
   315 +=================================+============+
       
   316 | Forward by a screenful of text  | ``C-f``    |
       
   317 +---------------------------------+------------+
       
   318 | Backward by a screenful of text | ``C-b``    |
       
   319 +---------------------------------+------------+
       
   320 | Beginning of the screen         | ``H``      |
       
   321 +---------------------------------+------------+
       
   322 | Middle of the screen            | ``M``      |
       
   323 +---------------------------------+------------+
       
   324 | End of the screen               | ``L``      |
       
   325 +---------------------------------+------------+
       
   326 | End of file                     | ``G``      |
       
   327 +---------------------------------+------------+
       
   328 | Line number ``num``             | ``[num]G`` |
       
   329 +---------------------------------+------------+
       
   330 | Beginning of file               | ``gg``     |
       
   331 +---------------------------------+------------+
       
   332 | Next occurrence of the text     | ``*``      |
       
   333 | under the cursor                |            |
       
   334 +---------------------------------+------------+
       
   335 | Previous occurrence of the text | ``#``      |
       
   336 | under the cursor                |            |
       
   337 +---------------------------------+------------+
       
   338 
       
   339 Note: ``C-x`` is ``Ctrl`` + ``x``
       
   340 
       
   341 The visual mode
       
   342 ~~~~~~~~~~~~~~~
       
   343 The visual mode is a special mode that is not present in the original vi editor. It allows us to highlight text and perform actions on it. All the movement commands that have been discussed till now work in the visual mode also. The editing commands that will be discussed in the future work on the visual blocks selected, too. 
       
   344 
       
   345 Editing commands
       
   346 ~~~~~~~~~~~~~~~~
       
   347 
       
   348 The editing commands usually take the movements as arguments. A movement is equivalent to a selection in the visual mode. The cursor is assumed to have moved over the text in between the initial and the final points of the movement. The motion or the visual block that's been highlighted can be passed as arguments to the editing commands. 
       
   349 
       
   350 +-------------------------+---------+
       
   351 | Editing effect          | Command |
       
   352 +=========================+=========+
       
   353 | Cutting text            | ``d``   |
       
   354 +-------------------------+---------+
       
   355 | Copying/Yanking text    | ``y``   |
       
   356 +-------------------------+---------+
       
   357 | Pasting copied/cut text | ``p``   |
       
   358 +-------------------------+---------+
       
   359 
       
   360 The cut and copy commands take the motions or visual blocks as arguments and act on them. For instance, if you wish to delete the text from the current text position to the beginning of the next word, type ``dw``. If you wish to copy the text from the current position to the end of this sentence, type ``y)``.
       
   361 
       
   362 Apart from the above commands, that take any motion or visual block as an argument, there are additional special commands. 
       
   363 
       
   364 +----------------------------------------+---------+
       
   365 | Editing effect                         | Command | 
       
   366 +========================================+=========+
       
   367 | Cut the character under the cursor     | ``x``   |
       
   368 +----------------------------------------+---------+
       
   369 | Replace the character under the        | ``ra``  |
       
   370 | cursor with ``a``                      |         |
       
   371 +----------------------------------------+---------+
       
   372 | Cut an entire line                     | ``dd``  |
       
   373 +----------------------------------------+---------+
       
   374 | Copy/yank an entire line               | ``yy``  |
       
   375 +----------------------------------------+---------+
       
   376 
       
   377 Note: You can prefix numbers to any of the commands, to repeat them.
       
   378 
       
   379 Undo and Redo
       
   380 ~~~~~~~~~~~~~
       
   381 You can undo almost anything using ``u``. 
       
   382 
       
   383 To undo the undo command type ``C-r``
       
   384 
       
   385 Searching and Replacing
       
   386 ~~~~~~~~~~~~~~~~~~~~~~~
       
   387 
       
   388 +-----------------------------------------+---------+
       
   389 | Finding                                 | Command |
       
   390 +=========================================+=========+
       
   391 | Next occurrence of ``text``, forward    |``\text``|
       
   392 +-----------------------------------------+---------+
       
   393 | Next occurrence of ``text``, backward   |``?text``|
       
   394 +-----------------------------------------+---------+
       
   395 | Search again in the same direction      | ``n``   |
       
   396 +-----------------------------------------+---------+
       
   397 | Search again in the opposite direction  | ``N``   |
       
   398 +-----------------------------------------+---------+
       
   399 | Next occurrence of ``x`` in the line    | ``fx``  |
       
   400 +-----------------------------------------+---------+
       
   401 | Previous occurrence of ``x`` in the line| ``Fx``  |
       
   402 +-----------------------------------------+---------+
       
   403 
       
   404 +---------------------------------------+------------------+
       
   405 | Finding and Replacing                 |  Command         |
       
   406 +=======================================+==================+
       
   407 | Replace the first instance of ``old`` |``:s/old/new``    |
       
   408 | with ``new`` in the current line.     |                  |
       
   409 +---------------------------------------+------------------+
       
   410 | Replace all instances of ``old``      |``:s/old/new/g``  |
       
   411 | with ``new`` in the current line.     |                  |
       
   412 +---------------------------------------+------------------+
       
   413 | Replace all instances of ``old``      |``:s/old/new/gc`` |
       
   414 | with ``new`` in the current line,     |                  |
       
   415 | but ask for confirmation each time.   |                  |
       
   416 +---------------------------------------+------------------+
       
   417 | Replace the first instance of ``old`` |``:%s/old/new``   |
       
   418 | with ``new`` in the entire file.      |                  |
       
   419 +---------------------------------------+------------------+
       
   420 | Replace all instances of ``old``      |``:%s/old/new/g`` |
       
   421 | with ``new`` in the entire file.      |                  |
       
   422 +---------------------------------------+------------------+
       
   423 | Replace all instances of ``old`` with |``:%s/old/new/gc``|
       
   424 | ``new`` in the entire file but ask    |                  |
       
   425 | for confirmation each time.           |                  |
       
   426 +---------------------------------------+------------------+
       
   427 
       
   428 SciTE
       
   429 -----
       
   430 
       
   431 SciTE is a *source code* editor, that has a feel similar to the commonly used GUI text editors. It has a wide range of features that are extremely useful for a programmer, editing code. Also it aims to keep configuration simple, and the user needs to edit a text file to configure SciTE to his/her liking. 
       
   432 
       
   433 Opening, Saving, Editing files with SciTE is extremely simple and trivial. Knowledge of using a text editor will suffice. 
       
   434 
       
   435 SciTE can syntax highlight code in various languages. It also has auto-indentation, code-folding and other such features which are useful when editing code. 
       
   436 
       
   437 SciTE also gives you the option to (compile and) run your code, from within the editor. 
       
   438 
       
   439 Personalizing your Environment
       
   440 ==============================
       
   441 
       
   442 .bashrc
       
   443 -------
       
   444 What would you do, if you want bash to execute a particular command each time you start it up? For instance, say you want the current directory to be your Desktop instead of your home folder, each time bash starts up. How would you achieve this? Bash reads and executes commands in a whole bunch of files called start-up files, when it starts up. 
       
   445 
       
   446 When bash starts up as an interactive login shell, it reads the files ``/etc/profile``, ``~/.bash_profile``, ``~/.bash_login``, and ``~/.profile`` in that order. 
       
   447 
       
   448 When it is a shell that is not a login shell, ``~/.bashrc`` is read and the commands in it are executed. This can be prevented using the ``--norc`` option. To force bash to use another file, instead of the ``~/.bashrc`` file on start-up, the ``--rcfile`` option may be used. 
       
   449 
       
   450 Now, you know what you should do, to change the current directory to you Desktop. Just put a ``cd ~/Desktop`` into your ``~/.bashrc`` and you are set!
       
   451 
       
   452 This example is quite a simple and lame one. The start-up files are used for a lot more complex things than this. You could set (or unset) aliases and a whole bunch of environment variables in the ``.bashrc``. We shall look at them, in the next section where we look at environment variables and ``set`` command.
       
   453 
       
   454 
       
   455 .vimrc
       
   456 ------
       
   457 ``.vimrc`` is a file similar to ``.bashrc`` for vim. It is a start-up file that vim reads and executes, each time it starts up. The options that you would like to be set every time you use vim, are placed in the ``.vimrc`` file, so that they are automatically set each time vim starts. The recommended place for having your ``.vimrc`` is also your home directory. 
       
   458 
       
   459 The file ``/etc/vimrc`` is the global config file and shouldn't usually be edited. You can instead edit the ``~/.vimrc`` file that is present in your home folder. 
       
   460 
       
   461 There are a whole bunch of variables that you could set in the ``.vimrc`` file. You can look at all the options available, using the ``:set all`` command in vim. You could use the ``:help option_name`` to get more information about the option that you want to set. Once you are comfortable with what you want to set a particular variable to, you could add it to ``.vimrc``. You should also look at ``:help vimrc`` for more info on the ``.vimrc`` file. If you already have a ``.vimrc`` file, you can edit it from within vim, using ``:e $MYVIMRC`` command. We shall look at some of the most commonly used options. 
       
   462 
       
   463 +----------------------------------+-----------------------------------------------------------------------------------+
       
   464 |Command                           | Vim action                                                                        |
       
   465 +==================================+===================================================================================+
       
   466 |``set nocompatible``              | Explicitly disable compatibility with vi                                          |
       
   467 +----------------------------------+-----------------------------------------------------------------------------------+
       
   468 |``set backspace=indent,eol,start``| In the insert mode, vim allows the backspace key to delete white spaces at the    |
       
   469 |                                  | start of line, line breaks and the character before which insert mode started.    |
       
   470 +----------------------------------+-----------------------------------------------------------------------------------+
       
   471 |set autoindent                    | Vim indents a new line with the same indentation of the previous line.            |
       
   472 +----------------------------------+-----------------------------------------------------------------------------------+
       
   473 |set backup                        | Vim keeps a backup copy of a file when overwriting it.                            |
       
   474 +----------------------------------+-----------------------------------------------------------------------------------+
       
   475 |set history=50                    | Vim keeps 50 commands and 50 search patterns in the history.                      |
       
   476 +----------------------------------+-----------------------------------------------------------------------------------+
       
   477 |set ruler                         | Displays the current cursor position in the lower right corner of the vim window. |
       
   478 +----------------------------------+-----------------------------------------------------------------------------------+
       
   479 |set showcmd                       | Displays the incomplete command in the lower right corner.                        |
       
   480 +----------------------------------+-----------------------------------------------------------------------------------+
       
   481 |set incsearch                     | Turns on incremental searching. Displays search results while you type.           |
       
   482 +----------------------------------+-----------------------------------------------------------------------------------+
       
   483 
       
   484 You can see the effect of the changes made to your ``.vimrc`` file by restarting vim. If you want to see the changes that you made to your ``.vimrc`` file immediately, you could source the file from within vim.
       
   485 
       
   486 If the ``.vimrc`` file has been sourced when this instance of vim was started, you could just resource the file again::
       
   487   :so $MYVIMRC
       
   488 
       
   489 If you just created the ``.vimrc`` file or it was not sourced when you stared this instance of vim, just replace the ``$MYVIMRC`` variable above, with the location of the ``.vimrc`` file that you created/edited.
       
   490 
       
   491 Subshells and ``source``
       
   492 ========================
       
   493 
       
   494 A subshell is just a separate instance of the shell which is a child process of the shell that launches it. Bash creates a subshell in various circumstances. Creation of subshells allows the execution of various processes simultaneously.   
       
   495 
       
   496   * When an external command is executed, a new subshell is created. Any built-in commands of bash are executed with int the same shell, and no new subshell is started. When an external command is run, the bash shell copies itself (along with it's environment) creating a subshell and the process is changed to the external command executed. The subshell is a child process of this shell. 
       
   497 
       
   498   * Any pipes being used, create a subshell. The commands on the input and output ends of the pipe are run in different subshells. 
       
   499 
       
   500   * You could also, explicitly tell bash to start a subshell by enclosing a list of commands between parentheses. Each of the commands in the list is executed within a single new subshell.   
       
   501 
       
   502 To avoid creating a subshell, when running a shell script, you could use the ``source`` command. 
       
   503 ::
       
   504 
       
   505   $ source script.sh
       
   506 
       
   507 This will run the ``script.sh`` within the present shell without creating a subshell. The ``.`` command is an alias for the source command. ``. script.sh`` is therefore equivalent to ``source script.sh``.