COMPUTER TRAINING: Working with Terminal/Shell

Monday, 24 December 2012

Working with Terminal/Shell

Working with Terminal/Shell
 
Introduction to Linux shell
 
Nowadays, as soon as you get Linux installed, you get a nice graphical interface and rarely if ever need to make use of the so-called terminal mode (aka shell prompt). However, in Linux the simple, modest terminal is not merely an afterthought, but an extremely powerful tool.
 
While it may be true that you don’t need to use it, it’s not that difficult to learn, and very useful to know. Fortunately for yourself, with Linux’s users and security, you can create a new user for playing around, then you can experiment to your heart’s content without breaking anything.
 
The most generic sense of the term shell means any program that users use to type commands. Since in the Unix operating system users can select which shell they want to use (which program should execute when they login), many shells have been developed.
 
It is called a "shell" because it hides the details of the underlying operating system behind the shell's interface. (In contrast with the "kernel", which refers to the lowest-level, or 'inner-most' component of an operating system). Similarly, graphical user interfaces for Unix, such as GNOME and KDE, are occasionally called visual shells or graphical shells.
 
By itself, the term shell is usually associated with the command line. In Unix, any program can be the user's shell. Users who want to use a different syntax for typing commands can specify a different program as their shell.
 
What is Terminal/Shell?
 
In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in Os there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is pass to kernel.
 
Shell is a user program or it's environment provided for user interaction. Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
 
Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc. Several shells are available for Linux including:
 
Shell Name
Developed by
Where
Remark
BASH ( Bourne-Again SHell )Brian Fox and Chet RameyFree Software FoundationMost common shell in Linux. It's Freeware shell.
CSH (C SHell)Bill JoyUniversity of California (For BSD)The C shell's syntax and usage are very similar to
the C programming language.
KSH (Korn SHell)David KornAT & T Bell Labs
--
TCSHSee the man page.
Type $ man tcsh
--
TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).
 
$ cat /etc/shells
 
Note that each shell does the same job, but each understands different command syntax and provides different built-in functions.
 
A "shell" is a program which interprets commands, either typed in directly by the user, or contained in a file called a "shell script", which is a simple interpreted program. The equivalents in WindowsTM would be "command processor" for shell, "COMMAND.COM" or "CMD.EXE" instead of bash, and ".BAT files" instead of shell scripts.
 
Linux has a variety of different shells, but certainly the most popular is "bash", so it is this one which will be described here (even though many of these instructions apply to all shells). Some of the others are retained simply because there are lots of people who got used to them and don’t wish to change, or because they are aimed at a specialised set of users.
 
 
 
 
Working with Terminal/Shell
 
Starting and Customizing Shell interface
 
To Start Shell in Gnome, first click on Applications and select Accessories from the Applications menu.
 
 
From the Accessories sub menu select Terminal
 
 
 
This is Linux Shell/Terminal, similar to DOS interface. Unlike DOS, Linux Shell's are much more powerful and the interface of the shell can be customized in a variety of way by the shell user. User can create multiple profiles and customize each profiles look and feel.
 
Customizing Shell interface
 
Creating a new Shell Profile
 
To create a new Shell Profile Click on File menu and select New Profile.. option from the drop down menu.
 
 
Type the name of the new profile in the Profile name field on the New Profile dialog box
 
 
After Entering the Profile name click on Create button to create the profile.
 
Editing Profile dialog box will be displayed. This dialog box is used to customized the look and feel on the Shell interface.
 
Click on Colors tab to customize the color schema of the Shell interface.
 
 
Click on Effects tab to use a background image for Shell background instead of plain color. After customizing the profile click on Close button to close the Editing Profile dialog box.
 
Using the New Shell profile
 
To use the New Profile instead of the Default profile click on File-->New Tab/File-->New Terminal and select the profile that you want to use.
 
 
Shell interface with newly configured shell profile.
 
Basic Shell Interface Commands/Shortcuts:
 
Open New Terminal Window : Ctrl+Shift+N

Open New Terminal Tab : Ctrl+Shift+T

Close Terminal Window : Ctrl+Shift+Q

Close Tab : Ctrl+Shift+W

Copy : Ctrl+Shift+C

Paste : Ctrl+Shift+V

Full Screen : F11
 
 
 
 
Working with Terminal/Shell
 
Basic Shell commands
 
As we know, Linux like Unix was an CLI (Command Line Interface) based operating system in its early days, that means everything that user want to do, has to performed with the help of commands. Even today, when Linux has a lot of Window and Desktop managers, there are a lot of things that are dependent on the terminal and in order to perform those actions we have to give certain commands.
 
Linux is tightly integrated with Shell/Terminal, and has a wide range of Command Library. All commands are discussed in detail in our next Linux tutorial “Linux Administration”, and of the basic shell commands are discussed here.
 
Basic commands:
 
1. cd
 
Used for: Used to change directories.
 
Description: Type cd followed by the name of a directory to access that directory. Keep in mind that you are always in a directory and allowed access to any directories hierarchically above or below.
 
Ex: cd download
 
If the directory games is not located hierarchically below the current directory, then the complete path must be written out.
 
Ex: cd /home/super/Download
 
 
To move up one directory,
use the shortcut command.
Ex: cd ..
 
2. clear
Used for:

Used to clear the command prompt equivalent to DOS cls
Description: Type clear to clean up your command prompt window. This is especially helpful when you are typing lots of commands and need a clean window to help you focus.
Ex: clear
 
after executing the clear command
 
 
This is also useful when you are getting ready to type a rather long command and do not wish to become confused by other details on the screen.
 
3. date Used for: 
Used to set/view your server's date and time.
Description: date command with any argument is used to view the date and time.
Ex:
 
To change the servers date and time, type date followed by the two digit month, the two digit date, the two digit time, and two digit minutes.
 
The syntax is easy enough and resembles this: MMDDhhmm This command is helpful but must be used when superuser or logged in as root. Otherwise you will get an "Operation not permitted" reply.
 
As root user you can use the command such as:
 
date 09261330
 
The above command will set the server date and time to the 9th month (September), the 26th day, at 1:30pm.
 
4. df 
Used for: to check disk space.
Description: Typing df provides a very quick check of your file system disk space.
 
 
Type df -h to get a more easily readable version of the output.
 
 
Notice that this command will include all applicable storage such as your hard disk/s (hda, hdb etc.) and your server SWAP file (shm).
 
To list disk space including filesystem type:
 
df -h -T
 
5. finger 
Used for: to see who's on the system
Description: Typing finger allows you to see who else is on the system or get detailed information about a person who has access to the system.
 
Type finger followed by the name of a user's account to get information about that user. Or, type finger and press enter to see who's on the system and what they are doing.
 
Ex: finger super
 
 
6. logout
 
Used for: to quit using the system
 
Description: 
hey, you guessed it, executing logout will log your account out of the system.
 
Type logout at the prompt to disconnect from your Linux machine or to logout a particular user session from the system. Keep in mind that although rudimentary, leaving your critical account logged on may be a security concern. We always recommend promptly using logout when you are finished using your root account!
Ex: logout
 
7. ls Used for:
Used to list files and directories Description: Type ls to see a list of the files and directories located in the current directory. If you’re in the directory named Download and you type ls, a list will appear that contains files in the Download directory and sub-directories in the Download directory.
 
 
Examples: ls jdk6
 
 
ls /usr/bin
 
 
Type ls -alt to see a list of all files (including .rc files) and all directories located in the current directory. The listing will include detailed, often useful information. Examples: ls -alt
 
 
ls -alt /usr/bin
 
 
If the screen flies by and you miss seeing a number of files, try using the |more at the end like: ls -alt |more
 
* In Bash (Linux shell) often the abbreviated command L is available. To get a verbose listing of files and directories you could therefore simply type: l
 
ls command options: 
Option     Action
 
-a    list hidden files

-d     list the name of the current directory

-F     show directories with a trailing '/' executable files with a trailing '*'

-g     show group ownership of file in long listing

-i     print the inode number of each file

-l     long listing giving details about files and directories

-R     list all subdirectories encountered

-t    sort by time modified instead of name
 
using option R&g with ls command to view the contents of directories beginning with latter j, will produce the following result:
 
 
You can try these switches to narrow down your search results.
 
8. man 
Used for: Used to pull up information about a Linux command
Description: Type man followed by a command to get detailed information about how to use the command.
Ex: man ls
 
 
Type man -k followed by a word to list all of the commands and descriptions that contain the word you specified.
Ex: man -k logout
 
9. more 
Used for:
Used to read the contents of a file
Description: 
Type more followed by the name of a text file to read the file’s contents. Why do we emphasize using this on a "text" file? Because most other types of files will look like garbage!
 
img
 
 
10. nano
Used for:
Description:
 Typing nano will start a basic text editor on most Linux systems.
 
img
 
 
Type nano followed by the filename you wish to edit. This basic editor is quick and easy to use for beginners. However, it is very important that you also learn about other text editors available on Linux and UNIX systems.
Ex: nano /home/super/Download/simple.txt
 
img
 
/home/super/Download/simple.txt file in opened in nano
 
img
 
nano is covered in detail, later in this chapter.
 
11. passwd 
Used for:
Used to change your current password
Description: 
Type passwd and press enter. You'll see the message Changing password for yourname. At the Old password: prompt, type in your old password . Then, at the Enter new password: prompt, type in your new password . The system double checks your new password. Beside the Verify: prompt, type the new password and press again.
 
img
 
 
Create a secure password that combines parts of words and numbers. For instance, your dog's name may be Rufus. He may have been born in 1980. Create a password that uses parts of both the name and date of birth, such as 80rufuS. Note the use of at least one capital letter. This is a fairly secure password and easy to remember.
 
12. pwd
Used for:
Used to list the name of your current directory

Description: 

Type pwd and hit enter. You'll see the full name of the directory you are currently in. This is your directory path and is very handy. This is especially handy when you forget which directory you’ve changed to and are trying to run other commands.
 
img
 
If you were in a different directory, the output would have been different:
 
img
 
 
 
 
Working with Terminal/Shell
 
Basic File Manipulation commands
 
1. chmod
Used for:


The chmod command allows you to alter access rights to files and directories. All files and directories have security permissions that grant the user particular groups’ or all other users’ access.

Description:

To view your files' settings, at the shell prompt type: ls -alt
 
You should see some files with the following in front of them (an example follows):
 
Col 1Col 2Col 3Col 4Col 5Col 6Col 7
-rw-rw-r--1supersuper1026Oct  8 16:43simple.txt
-rw-rw-r--1supersuper3551Oct  8 16:43ChangeAddress
(2).php
-rw-rw-r--1supersuper3551Oct  1 09:49ChangeAddress
.php
-rw-rw-r--1supersuper8944694Sep 28 15:58MadeForYou
.tar.gz
-rw-rw-r--1supersuper787516Sep 28 15:47Almond-rounded
.zip
-rw-rw-r--1supersuper5986411Sep 27 15:34apache-tom-
cat-6.0.14.-
tar.gz
-rw-rw-r--1supersuper890880Sep 27 15:32apache-tomcat
-6.0.14-
deployer.tar.gz
-rw-rw-r--1supersuper363811Sep 25 17:02presentationus1
_new.zip
-rw-rw-r--1supersuper4208Sep 25 14:18adobe-release-
i386-1.0-1.-
noarch.rpm
Drwxrwxrwx2supersuper4096Sep 24 18:08jdk6
 
What different columns represents in the result?
 
Col 1: 

The first character in this column tells what kind of file this is. The dash represents a normal file; the d represents a directory. The remaining characters describe the file’s permissions, which we will cover in detail in the next pages.
 
Col 2 

This column tells how many hard links there are for this file.
 
Col 3 & 4 

These two columns tell you the name of the owner of the file and the group to which the owner belongs.
 
Col 5 

The size of the file, in bytes. For directories, this is the size of the directory, not the total of the directory’s contents!
 
Col 6 

This column contains the date and time at which the file was last modified. Note that you get time of day if the modification was during this year; otherwise you see the year.
 
Col 7 

The last column contains the file name.
 
What do the letters mean in front of the files/directories mean?
 
r indicates that it is readable (someone can view the file’s contents)

w indicates that it is writable (someone can edit the file’s contents)

x indicates that it is executable (someone can run the file, if executable)

- indicates that no permission to manipulate has been assigned.
 
When listing your files, the first character lets you know whether you’re looking at a file or a directory. It’s not part of the security settings. The next three characters indicate Your access restrictions. The next three indicate your group's permissions, and finally other users' permissions.
 
Use chmod followed by the permission you are changing. In very simple form this would be:
 
chmod 755 simple.txt
 
The example above will grant you full rights, group rights to execute and read, and all others access to execute the file simple.txt.
 
#Permission
7full
6read and write
5read and execute
4read only
3write and execute
2write only
1execute only
0none
 
Still confused?
 
Use the table above to define the settings for the three "users." In the command, the first number refers to your permissions, the second refers to group, and the third refers to general users.
 
Typing the command: chmod 751 filename
 
gives you full access, the group read and execute, and all others execute only permission.
 
Execute ls -l command to view the content of current directory
 
img
notice the permissions of the file my_folder,
 
drwxrwxr-x 2 super super 4096 Sep 24 13:05 my_folder
 
here d indicates that my_folder is a directory, after that, 2nd 3rd and 4th characters,[ rwx]represents owner permission and here rwx means owner can read, write and search the directory, 5th, 6th and 7th [rwx] represents the group permission and rwx means group member can read, write and search the directory. Las t three characters represents the permission for others and r-x as permission means, others can read the content of directory and on directory, but they can not write to it.
 
Now, let us the alter the permission of the directory
type the following command:
chmod 777 my_folder
 
as described earlier 7 as permission string means full permission, this means we are assigning full permission to everybody.
 
img
 
Now, use the ls command again to see the effect of the chmod
 
img
 
now notice the change, now everybody has full permission to the my_folder directory. If you want to make this folder inaccessible to other users. You can do so by executing the following command:
 
chmod 770 my_folder
 
img
 
now use the ls -l command to view the effect of the last chmod command:
 
img
 
as you can see in the results the last thee characters that belongs to the others permission are blank, that means, users other that owner and group members have no access to the folder.
 
2. cp 
Used for : 
Used to copy file(s) from one location to another

Description : 

Type cp followed by the name of an existing file and the name of the new file. Similar to DOS copy .
 
cp file newfile 

To copy a file to a different directory (without changing th e file’s name), specify the directory instead of the new filename.

Example:
 
cp newfile test 

To copy a file to a different directory and create a new file name, you need to specify a directory/a new file name.
Example:
 
cp newfile testdir/newerfile

cp newfile ../newerfile
 
The .. represents one directory up in the hierarchy.
 
Now, let us copy a file with cp command
type the following command:

cp readthis.txt my_folder in this command readthis.txt is the file that we want to copy and my_folder is destination folder.
 
img
Now use the ls command to make sure, file has been copied to the destination folder
 
img
3. file 
Used for :
used to retrieve the information about the file type.

Description : 
Type file followed by the name of an existing file in the directory.

Ex: file contact_magnager.exe

OUTPUT: MS-DOS executable (EXE)
 
This command allows you to figure out what the file type is and how to use it. For instance the command will tell you whether it is an executable, a compressed file and which type, or something unusual.
 
This command is simplistic, but often can allow you to determine why a file does not respond the way you expect.
 
img
 
4. mv

Used for :
Used to move file(s) from one location to another

Description : Type mv followed by the current name of a file and the new name of the file. Similar to DOS move

Ex: 
mv oldfile newfile
 
Type mv followed by the name of a file and the new directory where you'd like to place the file.
Ex:
mv readthis.txt my_folder
 
This moves the file named readthis.txt to an existing directory named my_folder. Be certain you’re specifying a directory name or the mv command alters the name of the file instead of moving it.
 
The mv command moves files and directories from one directory to another, or renames a file or directory. If you move a file or directory to a new directory, it retains the base file name. When you move a file, all links to other files remain intact, except when you move it to a different file system. When you move a directory into an existing directory, the directory and its contents are added under the existing directory.
 
When you use the mv command to rename a file or directory, the TargetDirectory parameter can specify either a new file name or a new directory path name.
 
If moving the file would overwrite an existing file that does not have write-permission set and if standard input is a workstation, the mv command displays the file-permission code and reads a line from standard input. If that line begins with a y, or the locale's equivalent of a y, the mv command moves the file. If the response is anything other than a y, the mv command does nothing to that file and continues with the next specified file.
 
You can use the mv command to move files within the same file system or between file systems. Whether you are working in one file system or across file systems, the mv command copies the file to the target and deletes the original file. The mv command preserves in the new file the time of the most recent data modification, the time of the most recent access, the user ID, the group ID, and the file mode of the original file.
 
The mv command will modify either the source file or the destination path if the command is prematurely terminated.
 
Attention: The mv command can overwrite many existing files unless you specify the -i flag. The -i flag prompts you to confirm before it overwrites a file. If both the -f and -i flags are specified in combination, the last flag specified takes precedence.
 
-f
Does not prompt you before overwriting an existing file.
-i
Prompts you before moving a file or directory to an existing path name by displaying the name of the file followed by a question mark. If you answer with a line starting with y or the locale's equivalent of a y, the move continues. Any other reply prevents the move from occurring.
 
Now, let us move a file with mv command,

type the following command to move a file:

mv readthis.txt my_folder -i

-i in this command means, if a file named readthis.txt exists in the destination folder, then it will ask you if you want to overwrite the existing file.
 
img
 
5. rm [remove] 

Used for : Used to remove a file from the file system.

Description :
 
The rm command removes the entries for the specified File parameter from a directory. If an entry is the last link to a file, the file is then deleted. You do not need read or write permission for the file you want to remove. However, you must have write permission for the directory containing the file.
 
If either of the files . (dot) or .. (dot, dot) are specified as the base name portion of the File parameter, the rm command writes a diagnostic message to standard error and does nothing more with such parameters.
 
The rm command writes a prompt to standard error and reads a line from standard input if the -f flag is not specified, and either the File parameter does not have write permission and the standard input is a workstation, or the -i flag is specified. If the response is not affirmative, the rm command does nothing more with the current file and proceeds to the next file.
 
The files owned by other users cannot be removed if the sticky bit of the directory is set and the directory is not owned by the user.
 
Note: The rm command supports the -- (dash, dash) parameter as a delimiter that indicates the end of the flags.
 
Flags
 
-eDisplays a message after each file is deleted.
-fDoes not prompt before removing a write-protected file. Does not display an error message or return error status if a specified file does not exist. If both the -f and -iflags are specified, the last one specified takes affect.
-iPrompts you before deleting each file. When you use the -i and -r flags together, therm command also prompts before deleting directories. If both the -i and -f flags are specified, the last one specified takes affect.
-rPermits recursive removal of directories and their contents when the File parameter is a directory. This flag is equivalent to the -R flag.
-RPermits recursive removal of directories and their contents when the File parameter is a directory. This flag is equivalent to the -r flag.
 
Type rm followed by the name of a file to remove the file.
 
Ex:
rm readthis.txt
 
Use the wildcard character to remove several files at once.
Ex: rm r*
 
This command removes all files beginning with n. Type rm -i followed by a filename if you’d like to be prompted before the file is actually removed.
 
Ex:
rm -i readthis.txt
rm -i r*
 
By using this option, you have a chance to verify the removal of each file. The -i option is very handy when removing a number of files using the wildcard character *.
 
 
 
 
Working with Terminal/Shell
 
Basic Directory commands
 
1. cd 
Used for : 
Use cd to change directories.

Description : 
Type cd followed by the name of a directory to access that directory. Keep in mind that you are always in a directory and can navigate to directories hierarchically above or below.
 
Example:
 
cd download 

If the directory games is not located hierarchically below the current directory, then the complete path must be written out.
 
Ex:

cd /home/super/downlaods
 
img
 
To move up one directory, use the shortcut command.
 
Example:
cd ..
 
img
 
2. mkdir
Used for : 
Used to create directories on disk. Similar to DOS MD

Description :
Use mkdir to make/create a brand new directory

Type mkdir followed by the name of a directory.

Ex:
mkdir testdir
 
img
 
Create one or more directories. You must have write permission in the parent directory in order to create a directory. The default mode of the new directory is 0777, modified by the system or user's umask.
 
Options
-m mode, --mode mode
 
Set the access mode for new directories. See chmod for an explanation of acceptable formats for mode.
 
-p, --parents
 
Create intervening parent directories if they don't exist.
 
-v, --verbose
 
Print a message for each directory created.
 
--help
 
Print help message and then exit.
 
&--version
 
Print version number and then exit.
 
-Z context, --context=context
 
Set security context in SELinux.
 
Examples
 
Create a read-only directory named personal:
 
mkdir -m 444 personal
 
img
 
3. rmdir
Used for :Used rmdir to remove an existing directory (assuming you have permissions set to allow this).

Description : Type rmdir followed by a directory's name to remove it.

Ex: rmdir personal
 
img
 
You CAN'T remove a directory that contains files with this command. A more useful command is rm -r that removes directories and files within the directories.
 
The rmdir command is used mostly to remove empty directories. If you have a desire to use this command then you'll need to delete or move the files before attempting to remove a full directory.
 
Options
 
--help
 
Print a help message and then exit.
 
--ignore-fail-on-non-empty
 
Ignore failure to remove directories that are not empty.
 
-p, --parents
 
Remove directories and any intervening parent directories that become empty as a result. Useful for removing subdirectory trees.
 
--verbose
 
Verbose mode; print message for each directory as it is processed.
 
--version
 
Print version information and then exit.
 
 
 
 
Working with Terminal/Shell
 
Using Vi/Vim editor
 
Vim, which stands for Vi IMproved, is an open source, multiplatform text editor extended from vi. It was first released by Bram Moolenaar in 1991. Since then, numerous features have been added to Vim, many of which are helpful in editing program source code. Vim and vi are very popular editors for programmers and users of Unix-like operating systems.
 
What is vi?
 
• vim is efficient and elegant. It does things quickly by the best way.

• vim is healthful. It doesn't make your fingers and arms painful.

• vim is small and fast. It fits in one or two floppies and starts instantly.

• vim is powerful. No feature useful for text editing is missing.

• vim is simple and clean. It doesn't offer to do things irrelevant to text editing.

• vim is running everywhere. If you're willing to use it, you can, no matter what computers and operating systems you use.

• vim is free.
 
The Unix editor vi has a long history, and is the one that vim is based on. I believe that the power, beauty and essence of vim always come from vi. A real vim user doesn't mind using vi temporarily, but feels great pain if forced a thing like Emacs. vim retains all the powers of vi, plus a lot of improvements and new features. And now vim is running virtually all platforms. So I see no reason to choose vi over vim
 
Starting vi/vim
 
Type vi or vim to start vi editor on prompt to start the vi editor:
 
img
 
or you can specify the file name at the command line. i.e.
 
img
press enter after entering the command.
 
img
this UI of vi/vim editor.
 
Working with vi editor
 
img
 
In the vi editor press i/ESC to toggle between insert/command mode..
 
Useful vi editor commands:
 
Move around: Use the cursor keys, or "h" to go left, "j" to go down, "k" to go up, "l" to go right.


Close this window: Use ":q<Enter>".

Get out of Vim: Use ":qa!<Enter>" (careful, all changes are lost!).

Save to file: “:w<Enter>”
 
img
Press Esc to toggle between command mode and insert mode.
 
Getting started with vim editor in simple steps:
 
1. Starting vim
 
to start vim and edit a file (new or existing) vim hello.cpp
 
img
• to start vim and open a new file
 
vim
 
You can give the stuff you typed in a name when later you save it as a file.
 
2. Enter text/command
 
If this is one of the first few times you use vim, perhaps you're wondering why you cannot type in anything. This is because you're in command mode. In command mode, your key strokes are interpreted as editing commands. To begin typing new text into the file, you need to switch to insert mode by press i. Then you can see each character you type appears on the screen.
 
The two basic modes of vim are:
 
* Insert mode, in which anything you type (except some special keys) will appear on the screen and become part of your file buffer.

* Command mode, in which your key strokes are interpreted as commands.
 
After you start vim, you're in command mode.
 
How to Switch between modes
 
1. From command mode to insert mode, press
 
img
 
2. from insert mode to command mode, press ESC
 
3. How to Navigate ?
 
In both the command mode and the insert mode you can always use the arrow keys to move the cursor, and with gvim you can click mouse to get to new position. However, these are not the ways of vi guys. If you do that, you're not using vim, you're using notepad.
 
If you want to call yourself a vi guy, forget about the arrow keys, backspace, delete, insert, ... and of course, the mouse. The most effective ways of moving cursor is first go to the command mode by pressing ESC and then use vim's cursor-moving commands to move around.
 
The 4 basic commands are:
 
• j move down one line

• k move up one line

• h move left one character

• l move right one character
 
Remember: these commands work only in command mode. At first you may feel a bit uncomfortable. After you get familiar using these commands you will stick to them and forget the arrow keys.
 
4. Copy, Cut and Paste
 
How to COPY, CUT and PASTE
 
If you run the GUI version of vim, gvim, you can use mouse and the pull-down menus to do that---the same fashion with other editors. However, that is not the preferred style. You'll feel better off if you can live without a mouse.
 
1. Enter the command mode by pressing ESC

2. Move the cursor to the line which you want to make a copy, by pressing j or k

3. press

yy
to make a copy of the line, or
dd
to cut it and make a copy
 
img
 
4. now move cursor (by pressing k or j) to the the location where you want to put this copy

5. press

p

to put the buffer after current line, or

P

to put the buffer before current line
 
 
img
 
If you want to copy or cut several lines, put a number before the yy or dd command, like
 
2yy
 
to copy 2 lines.
 
5. Searching
 
Suppose you want to find all the words apple in your file
 
1. Make sure you are in command mode by hitting ESC

2. type

/eBIZ.com
 
followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted.
 
img
 
3.

4. after you got the first eBIZ.com, you can keep typing

n

to find other eBIZ.com
 
6. Exisiting vim
 
• save your file before you exit: press ESC, then type :

wq

or

ZZ
 
img
 
• save your file as newname before you exit: press ESC, then type :

wq newname
 
• Exit without saving, press ESC, then type :

q
 
• Forced quit

If :q doesn't work, it's probably because you didn't save the change. If you want to save, use :wq. If you don't want to save the changes, type :

q!
 
ing
 
 
 
 
 
Working with Terminal/Shell
 
Using GNU nano editor
 
nano is a curses-based text editor for Unix and Unix-like systems, licensed under the GNU General Public License. It is a clone of Pico, the editor of the Pine email client. Instead of being mouse-oriented, nano, like Pico, is controlled with control keys.
 
For example, Control-O saves the current file; Control-W goes to the search menu. nano puts a two-line "shortcut bar" at the bottom of the screen, listing many of the commands available in the current context. For a complete list, Control-G gets the help screen.
 
img
 
Starting and using nano
 
To start nano editor type nano at the prompt and press enter:
 
this is nano interface
 
In my personal opinion working with nano is quite simpler than working with vi/Vim editors. Its fast,simple and easy to use even for the first timers.
 
img
 
Starting with GNU nano

to start nano and edit a file (new or existing)

nano hello.txt

to start nano and open a new file
 
nano
 
Nano shortcuts:
 
Ctrl+G(^G) : Help

Ctrl+O(^O) :Write Out(Save file)

Ctrl+R(^R) :Read File(Open file)

Ctrl+W(^W) :Search

Ctrl+X(^X) :Exit nano

Ctrl+Y(^Y) :Previous Page

Ctrl+V(^V) :Next Page

Ctrl+K(^K) :Cut Text

Ctrl+U(^U) :Paste (Uncut Text)
 
 
img
 
after making changes in the file press Ctrl+O to save the file.
 
 
 
 

No comments:

Post a Comment