CIS241

System-Level Programming and Utilities

Viewing and Editing Files // vim and nano

Erik Fredericks, frederer@gvsu.edu
Fall 2025

Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers

top-right (vim)

CIS241 | Fredericks | F25 | 4-vim-nano

First...

We will need some files to play with!

In your home directory

  • cd ~

Download the following files:

Click each of the CSV files in GitHub, then click 'Raw', copy the URL, and then use wget to pull down both files:

  • wget <URL>

If you type ls afterwards you should see the two CSV files!

.
(scared)

How do we view files in the terminal?

.

Viewing commands

cat list entire file in terminal
less interactively view file
more interactively view file
head print first NN lines (N=10N = 10 by default)
tail print last NN lines (N=10N = 10 by default)
uniq removes adjacent repeated lines
sort prints file with lines sorted
.

Oops the file was too big and cat doesn't work well

Can pipe! We'll get into that more later, but

cat superbigfile | more

Sends output of the first command into the input of the second!

.
(scared)

How do we edit files in the terminal?

.

We'll need some form of editor

Depending on your Linux distribution, some may not come pre-installed!

If you don't have vim locally, and you're using a Debian-based system, that will be sudo apt install vim

That brings us to a good point!

You should update your system often (unless if you're using Arch, then that may be different...)

  • sudo apt update && sudo apt upgrade

    • You can't do this on EOS? Why?
.

OK then editors now

We'll use them for a lot! Writing C programs, bash scripts, etc.

  • Similar to Notepad / Notepad++
  • Use whichever editor you're most comfortable with!
    • I pretty much exclusively use vim

Many are out there!

  • Common ones: nano, vim, emacs, gedit
    • gedit - very easy to use
    • nano - easier to use
    • vim - very keystroke-oriented
    • emacs - very shortcut-oriented

Holy wars: https://en.wikipedia.org/wiki/Editor_war

.
(nano)

nano

.
(nano shortcuts)

nano shortcuts

Write normally and use shortcuts!

.

Try it out!

nano brings up an empty file and prompts you to save

nano filename creates (or opens) a file named filename

.

A permissions concern!

What happens if you try to work on a file to a protected area? (Note - this will apply to vim too)

  • All your hard work will disappear
  • Save your buffer locally (~/mynewfile)
.
(vim)

vim

.

vim (necessities)

top-right (vim)

Exit vim:

  • :q <enter>

Save:

  • :w <enter>
  • :wq <enter> (save and quit)

Quit without saving

  • :q!
.

vim usage

Different than nano - you start in command (i.e., normal) mode

  • You have to 'enter' editing mode
    • Great for power users
    • Annoying for new users
.

vim usage

vim has three modes of operation: normal, insert, and visual

By default, vim starts in normal mode. It doesn’t let you insert text, but rather interprets keystrokes as commands.

Insert mode will allow you to modify the file

Visual mode allows you to highlight text in the file

  • To switch to insert mode, type i
  • To switch to visual mode, type v
  • To switch to normal mode, type Esc

bottom-corner (vim modes)

.

vim usage - moving cursor

Can use standard arrow keys

As a shortcut, can use letters as arrow keys (in normal mode):

  • h: ←, j: ↓, k: ↑, l: →

More shortcuts:

  • PageDown (or Ctrl+F) to move forward one screen of data
  • PageUp (or Ctrl+B) to move backward one screen of data
  • G to move to the last line in the buffer
  • num G to move to the line number num in the buffer
  • gg to move to the first line in the buffer
.

vim shortcuts

To get to command line mode, press : key in normal mode

More shortcuts:

  • q to quit if no changes have been made to the buffer data
  • q! to quit and discard any changes made to the buffer data
  • w filename to save the file under a different filename
  • wq to save the buffer data to the file and quit
.

vim shortcuts

  • 2dd (or other number) will delete 2 lines starting at the line of the cursor position
  • 5x (or other number) will delete 5 characters starting at the cursor position

 

vim cheat sheet: https://vim.rtorr.com/

vim cheat sheet: https://devhints.io/vim

.
(vim shortcuts)
.

vim copy/paste

  • Cut + paste: use dd to cut the line, then p to paste it at the cursor position

  • Copy: y is the copy command (yank), and works similar to d: yw is yank word, y$ to yank to end of line

    • yy is also yank the full line
    • Yanking can be tricky because can’t see specifically what you’re copying
  • Visual mode lets you highlight text. Move cursor to where you want to start yanking, and press v. When you’ve highlighted the text you want to copy, press y. Then, move to where you want to paste, then press p.

.

vim find

top-right (waldo)

  • In normal mode, press the forward slash (/) key.

    • Type the text you want to find, then press Enter.
  • If the word appears after the current cursor location, it jumps to the first location where the text appears

  • If the word doesn’t appear after the current cursor location, it wraps around the end of the file to the first location in the file where the text appears (and indicates this with a message).

  • It produces an error message stating that the text was not found in the file.

  • To continue searching for the same word, press / and then press Enter, or use n (next)

.

vim replace

Must be in command line mode

  • General format: s/old/new/
  • :s/old/new/g to replace all occurrences of old in a line
  • :n,ms/old/new/g to replace all occurrences of old between line numbers n and m
  • :%s/old/new/g to replace all occurrences of old in the entire file
  • :%s/old/new/gc to replace all occurrences of old in the entire file, but prompt for each occurrence
.

vim configuration

Perhaps you don't want to reconfigure it each time

  • e.g., the color scheme, or number of spaces for a tab
    • or spell checking...

Create a .vimrc file in your home directory

  • ~/.vimrc
    • (Your first dot file!)

For example, in the file:

colorscheme morning
set spell
set spelllang=en_us
.

Colors?

Check available:

:colorscheme <space> Ctrl+d

or

:colorscheme <space> <tab>

 

All the options:

https://vimdoc.sourceforge.net/htmldoc/options.html

.

the carat is 'ctrl'

* ctrl+k / ctrl+u - cut and paste * M -> alt

- Go over difference between two modes: normal and insert - By default, normal mode to insert keystrokes - To switch, type i - To switch from insert to normal, type escape - To exit, :q - To save and exit, :wq

the VI keys!

Create new text file, add gibberish Insert mode: Ctrl-X s