Transcript Vim

Unix Environment
Input
Output
2

List Content (ls)

Make directory (mkdir)
◦ ls (list current directory)
◦ ls –all (include hidden files/folders)
◦ mkdir[directory name]

Change Directory (cd)

Remove Directory Entries (rm)

Java Compile (javac)
◦ cd [directory name]
◦ Use [Tab] button for auto-completion
◦ rm [file/folder name]
◦ javac *.java (shortcut to compile all java files)
◦ Similar effect as make command
3

Edit File in VIM (vim)
◦ vim [file name]

Running Java Program (java)
◦ java [program name without extension]
◦ java [class name] < [input file] > [output file]
(input/output redirection)

Checking for Difference (diff)
◦ diff [file name] [file name]
◦ Difference in a single space or an extra line will be noted

Manual (man)
◦ man [command name]
◦ When you are not sure what the command does, or what
parameter it expects
4


http://vim.rtorr.com/
3 main modes
◦ Normal/Command mode (default)
 For navigation and manipulation of text
 Everything the user types is interpreted as commands
 e.g. “h” to move left, “x” to remove a character
◦ Insert mode (i, I, a, A, o, O)
 For inserting new text
 Exit Insert mode to normal mode using Esc
◦ Visual mode
 For highlighting texts (useful for copy and paste)
5

Cursor movement (Only in command mode!)
◦
◦
◦
◦
◦
◦
◦
◦
h: move cursor left
j: move cursor down
k: move cursor up
l: move cursor right
0: jump to start of line
$ jump to end of line
G: end of file
5G: go to line 5
6


Insert mode – insert/append text
From command mode
◦
◦
◦
◦
◦
◦
◦
i: insert before cursor
I: insert at the beginning of the line
a: append after cursor
A: append at the end of line
o: open a new line below the current line
O: open a new line above the current line
ea: append at end of word
7

Editing
◦
◦
◦
◦
◦
◦
◦
◦
s: delete character and substitute text
cc: replace(change) entire line
cw: replace(change) to end of word
c$: replace(change) to end of line
x: cut character
u: undo
r: replace one character
R: replace all character until ESC
8

Visual mode
◦ v: start visual mode, mark lines, then yank (y)
◦ V: linewise
◦ Ctrl-v: visual block mode

Copy and Pasting
◦ Copy/yank: y
 Copy one line: yy, 2 lines: 2yy, to end of line: y$, one word:
yw
◦ Cut/delete: d
 Cut one line: dd, 2 lines: 2dd, to end of line: d$, one word:
dw
◦ Paste: p
9

Search
◦ /[pattern] → Search for the pattern
◦ n → Search next in the same direction
◦ N → Search next in the opposite direction

Saving and quitting
◦ Save (write): :w
◦ Save and quit: :wq
◦ Quit without saving: :q!

Tip
◦ Open 2 windows, one to code, other to compile
◦ gg=G : indent all lines
10

Write a Java program that prints “Hello World” out
to the console
In Vim
Compile and execute the program

vim [name of file].java


11
Unix Environment
Input
Output
12

Scanner Class
◦ Initialization: Scanner sc = new Scanner (System.in);
◦ hasNext() and variants
 Check if there exists another input
◦ next() and variants
 Get the next input
◦ Variants: Int, Line, Double
example: hasNextInt() and nextLine()
◦ There is NO nextChar() and hasNextChar() variant
13



Finds and returns the next complete token from
this scanner.
A complete token is preceded and followed by
input that matches the delimiter pattern.
By default: delimiter pattern is whitespace
14

next()
◦ reads the next token as a string

nextInt()
◦ reads the next token as an integer. If it is not: error

nextLine()
◦ Advance scanner, read and return all in current line (until
next newline character)
15


Input: 1 fish 2 fish 3 fish 4 fish /n end
next():
◦ 1 (as a string)

nextInt():
◦ error

next():
◦ fish

nextInt():
◦ 2 (as an integer)

nextLine():
◦ fish 3 fish 4 fish
16
o
o
o
Type 1: Number of Operations is specified
Type 2: Read until Terminating Special
Character
Type 3: Read until End of File
17
public static void main(String[] args) {
// … Code Section Omitted
// … Other Initialization
Scanner sc = new Scanner(System.in);
int numOps = sc.nextInt();
for (int i=0; i<numOps; i++) {
// Read Other Inputs
}
// … Code Section Omitted
}
18

1.
Steps:
Initialize the Scanner Class
Scanner sc = new Scanner(System.in);
2.
Read the First Input
String tempInput = sc.next();
3.
Loop until Terminating Special Character encountered
while (!tempInput.equals(TERMINATING_CHAR)) {
// Read Other Input (if exist)
// Step 4 Here
}
4.
Read into tempInput again
tempInput = sc.next();
19
public static void main(String[] args) {
// … Code Section Omitted
// … Other Initialization
Scanner sc = new Scanner(System.in);
String tempInput = sc.next();
while (!tempInput.equals(TERMINATING_CHAR)) {
// Read Other Input
tempInput = sc.next();
}
// … Code Section Omitted
}
20
Steps:
1. Initialize the Scanner Class

Scanner sc = new Scanner(System.in);
2.
Loop until End of File
while (sc.hasNext()) {
// Read Other Input
}
21
public static void main(String[] args) {
// … Code Section Omitted
// … Other Initialization
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
// Read Other Inputs
}
// … Code Section Omitted
}
22

Combines two or more different input types into a single
program
◦ e.g.: Type 3 Input on the Outside, Type 1 Input on the Inside.
public static void main(String[] args) {
// … Code Section Omitted
// … Other Initialization
Scanner sc = new Scanner (System.in);
while (sc.hasNext()) {
int numOps = sc.nextInt();
for (int i=0; i<numOps; i++) {
// Read Other Inputs
}
}
// … Code Section Omitted
}
23
Unix Environment
Input
Output
24

With Extra Line at the End
System.out.println("Output String Here");
System.out.print("Output String Here\n");

Without Extra Line at the End
System.out.print("Output String Here");
25

One per Line, Terminate with End Line
for (int i=0; i<numOutput; i++) {
System.out.println(outputString);
}

One per Line, Terminate without End Line
System.out.print(outputString);
for (int i=1; i<numOutput; i++) {
System.out.println();
System.out.print(outputString);
}
26

Matrix Type, End Space
for (int i=0; i<numLine; i++) {
for (int j=0; j<numOut; j++) {
System.out.print (output + " ");
}
System.out.println();
}

Matrix Type, End Not Space
for (int i=0; i<numLine; i++) {
System.out.print (output);
for (int j=1; j<numOut; j++) {
System.out.print (" " + output);
}
System.out.println();
}
27
END OF FILE