170 likes | 349 Views
CO004 Projects on Operating Systems. UNIX - Basics 0 3. Outline. Text Editors Compile and run a simple c program. Text Editors. pico/nano - Simple, easy-to-use text editor Vi/vim - Text editor based on an underlying line editor ex emacs - Powerful and extensible
E N D
CO004 Projects on Operating Systems UNIX - Basics 03
Outline • Text Editors • Compile and run a simple c program
Text Editors • pico/nano - Simple, easy-to-use text editor • Vi/vim - Text editor based on an underlying line editor ex • emacs - Powerful and extensible - Hard to learn
Basic Usage of vi and vim • vim <filename> • It will create a new file for you if no such file exists. • You can just type vim hello.c to start editing a c source file.
Vim • The first thing you have to know about Vim is how to exit it. • <press ESC if you are in the editing mode> • :q –leave vim • :q! –leave without saving • :wq –save and leave • You can use :w to save without leave • and:w <filename> to save in a new name.
VIM • Now you can understand that Vim is a monster filled with hotkeys and commands. • Don’t be frightened by that, you only needs “i”, “:q!”, “:wq”and “ESC”in order to use Vim. • You can press any of the following keys to enter the editing mode: a, i, o, r, A, I, O, R, each has a different function.
VIM • Basic operations in Normal mode: • 0 / $–go to the beginning/end of the line. • gg / G –go to the beginning/end of the file. • x / dd–delete a world / a line. • yy /p–copy / paste a line • u / <ctrl>+r–undo / redo • / / n–search / search next • <ctrl>+v–block choose
VIM • You can also use number + command to execute multiple commands. • For example, 2dd deletes 2 lines, 2yy copies 2 lines. • Learn Vim by experience, do not try to recite all of the commands. • You can also download a windows version of Vim
Vim: Basic Editing Saving your work assume that you are in command mode :w save :q quit :wq save and quit Insert mode is actually very similar to a regular editor. You can use cursor/navigation keys, backspace, delete … Page 10
Operators and Repetition Examples: d3w: delete THREE words d2t,: delete up to but not including the second comma dd: delete a line d100d: delete 100 lines 11
Yank & Paste Examples: yy: yank (copy) a line into the buffer y7y: yank SEVEN lines 12
Vim Drill Using Vim Type: “vim hello.c”; Type: “i”, and you’ll see “-- INSERT --” appeared; Type: Type: “Esc” key, and you’ll see “-- INSERT --” disappeared; type “:wq”, and you’ll save the file and quit vim. #include <stdio.h> int main(void) { printf("hello world\n"); return 0; } 13
How to compile c programs in UNIX? • Use gcc! • gcc by default hides all the intermediate steps. • “gcc hello.c” generates the executable “a.out” by default. • “gcc -o hello hello.c” generates the executable “hello” directly.
Examples hello.c GCC – GNU C Compiler Output Flag: -o [output file name] gcc -o hello hello.c #include <stdio.h> int main(void) { printf("hello world\n"); return 0; } 1 2 3 4 5 6 The command: ./hello “hello” is the exe file generated by GCC. So, the command means: run the program “hello” in the current directory, where . means the current directory. $ gcc -o hello hello.c [ no error; no warning ] $ ls hello* hello hello.c $ ./hello hello world $
How to run your executable program? • ./ your_program • “.” means the current working directory • “./” means executing your program in the current working directory • ./a.out • ./hello • Note: an executable code is not identified by .exe but by its property.
More about Vim • http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html • http://www.tuxfiles.org/linuxhelp/vimcheat.html • http://www.fprintf.net/vimCheatSheet.html • http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html