1 of 17

CSE 15L: SOFTWARE TOOLS �AND TECHNIQUES

Instructor: Onat Gungor

2 of 17

SCHEDULE

Last Lecture: Clean Coding

Today: Vim

3 of 17

LAST LECTURE REVIEW

  • Which one is a clean coding practice?
  • Redundant comments which facilitate understanding of your code
  • Functions doing more than one thing
  • Single letter variable names
  • Readable code that is hard to modify and maintain
  • None of them

4 of 17

LAST LECTURE REVIEW

  • Which statement is not true in clean coding?
  • Manually refactoring your code
  • Choosing clear and short variable names
  • Using verbs or verb phrases for function names
  • Functions having less than 3 arguments
  • Cleaning code instead of adding explanatory comments

5 of 17

TO DO

6 of 17

UNIX VIM EDITOR

  • Vi/Vim is a free and open-source, screen-based text editor program for Unix.

Why is it useful to know Vim?

Lightweight and found on every Unix system

Fast code editing

Commonly used by industry and popular

Very customizable and extensible

Other Choices: emacs, sublime, nano, …

Vim: Vi Improved

Good resource: vimgenius.com

7 of 17

UNIX VIM EDITOR

k

j

h

l

The cursor keys should also work. But using hjkl, you will be able to move around much faster, once you get used to it.

8 of 17

UNIX VIM EDITOR

Command

Description

vim <filename>

Edits an existing filename or creates a new one if filename does not exist.

:q

Exit without changes. Assumes no changes have been made.

:q!

Exit without changes. Assumes changes have been made.

:wq

Save the file and exit the editor.

:w

Write to the file that you are editing without exiting the editor.

x

Delete the character under the cursor.

File interaction Commands (open, close, save)

9 of 17

UNIX VIM EDITOR

Vim Modes

Command Mode

  • Default
  • Most powerful feature
  • Allows for efficient navigation and editing of existing text.

Insert Mode

  • Used to enter text into a file (exited via Esc key)
  • Multiple ways to enter
  • Not efficient for navigation or editing!

10 of 17

UNIX VIM EDITOR

Command

Description

i

Enter insert mode at this location

<esc>

Exit insert mode

o, O

Insert new line after/before current line

a, A

Append after cursor, end of current line

I (capital i)

Insert at beginning of line

Insert Mode

Command Mode

Command mode has also two sub-modes:

  • In line: where the cursor moves within the text
  • ex (last line): where the cursor moves to the bottom of the screen. ex commands are preceded with a colon (:), a forward slash (/), or a question mark (?).

11 of 17

UNIX VIM EDITOR

  • The general format for commands is

c[n]m

n: count

c: command

m: motion

e.g., d3w: delete 3 words

Delete Command Motions

Symbol

Description

d

line

w

to the start of the next word

e

to the end of the current word

$

to the end of the line

^

to the beginning of the line

Undo and redo changes

u: undo the last command

U: fix a whole line

CTRL-R: redo the command

Pressing just the motion while in Normal mode without a command will move the cursor as specified.

12 of 17

QUIZ TIME

  • Which of the following deletes 2 lines from the cursor?
  • d2d
  • d2w
  • 2$d
  • 2wd
  • d2e

13 of 17

UNIX VIM EDITOR

  • Some important commands

Command

Description

[n]Y OR [n]yy

copy (yank) n lines

P (p)

put yanked or deleted data before (after) the cursor

r<x>

 replace the character at the cursor with x

ce

change until the end of a word

cc

change until the end of a line

:s/old/new/g

substitute ‘new’ for ‘old’ in a line

:#,#s/old/new/g

substitute ‘new’ for ‘old’ between two lines

:%s/old/new/g

substitute ‘new’ for ‘old’ in a file

cnm format is applicable.

14 of 17

QUIZ TIME

  • Which commands should we use consecutively to cut 3 lines from the cursor and paste it before the cursor?
  • d3d,Y
  • 3Y,p
  • d3d,P
  • 3Y,d3d
  • None of the above

15 of 17

UNIX VIM EDITOR

  • Some important commands

Command

Description

CTRL-G

show your location in the file

G

move to the bottom of the file

gg

move to the start of the file

<line-number>-G

return to the line-number

/<phrase>

search for the phrase forward

?<phrase>

search for the phrase backward

n

search for the same text again

%

search for the matching parenthesis or bracket

Cursor location and file status

Search commands

16 of 17

UNIX VIM EDITOR

  • Splitting and joining lines
    • To split a line between words: position yourself on a space between the words to be split, then use the replace (r) followed by the return key. This will replace the space with a carriage return forming a new line.
    • To join or combine two lines: go to the upper of the two lines to be joined, then key in the uppercase “J” for join.
    • We wish to split the line below between the word “should” and “be”.

“This line should be split.”

  1. Position the cursor here
  2. Key in “r” followed by “enter”

“This line should

be split.”

  1. Position the cursor anywhere on this line
  2. Key in “J”

17 of 17

UNIX VIM EDITOR

  • Some “nice to know” features
    • To turn line numbers on and off use:
      • :set nu and :set nu!
    • To execute a single Unix command from inside the editor use:
      • :!cmd
    • To temporarily return to the shell use:
      • :sh (type “exit” to return to editor)
    • To repeat a colon command or to go back to earlier colon commands use:
      • : ↑ or ↓ (up arrow or down arrow)
    • To show the name of the current file use:
      • :f