Vi, Vim, GVim and Nano for RTL/DV

Learning objectives

Choose an available editor, navigate and modify RTL files, work across multiple files, search a project, apply team indentation settings, save safely, and exit without losing work.

1 Objective→ 2 Learn→ 3 Example→ 4 Practice→ 5 Verify→ 6 Continue
How to study this lessonUnderstand the idea first. Use the example to see the behaviour, practise where provided, then make sure you can explain the result before continuing.

Explanation

Choose the editor that fits the session

Linux engineering systems may provide several editors. Use the one installed and approved on the workstation.

Editor How it works When it is useful
vi Standard terminal editor with command and insert modes. The exact implementation varies by system. Minimal servers and recovery environments.
vim Vi-compatible terminal editor with syntax highlighting, multiple undo, and more navigation features. Regular RTL, testbench, script, and configuration editing.
gvim Graphical Vim. It uses Vim commands but opens a desktop window. Workstations with a graphical session.
nano Modeless terminal editor with shortcuts shown at the bottom. Small edits and learners who need a simpler interface.

Check availability before choosing:

command -v vi
command -v vim
command -v gvim
command -v nano

A missing command is not an error in your work. Choose another installed editor or ask which editor the lab supports.

Vim and Vi modes

Vim starts in Normal mode. Keys in Normal mode are commands rather than inserted text.

Action Keys or command
Enter Insert mode before the cursor i
Enter Insert mode after the cursor a
Return to Normal mode Esc
Save :w then Enter
Save and exit :wq then Enter
Exit when nothing changed :q then Enter
Discard changes and exit :q! then Enter
Undo / redo u / Ctrl+r
Copy a line / paste after yy / p
Delete one line dd
Go to a line :25 then Enter
Search forward /pattern then Enter
Next / previous match n / N
Replace in the whole file with confirmation :%s/old/new/gc
Show line numbers :set number
Enable syntax highlighting :syntax on

Use :q! only when you deliberately want to discard every unsaved change.

Move through a file in Vim

Return to Normal mode with Esc before using these keys.

Movement Key Meaning
One character h, j, k, l Left, down, up, and right. Arrow keys also work in most installations.
Next / previous word w / b Move by words instead of characters.
Start / first text / end of line 0 / ^ / $ Move within the current line.
First / last line gg / G Jump to the beginning or end of the file.
Specific line 25G or :25 Jump to line 25.
Page forward / backward Ctrl+f / Ctrl+b Move one screen at a time.
Matching bracket % Move between matching (), [], or {}.

Edit and indent efficiently in Vim

Vim combines an action with a movement. For example, dw deletes from the cursor to the next word and 5dd deletes five lines.

Action Key Result
Open a new line below / above o / O Creates a line and enters Insert mode.
Delete one character x Removes the character under the cursor.
Replace one character r followed by a character Replaces without entering Insert mode.
Change one word cw Deletes the word portion and enters Insert mode.
Delete to end of line D Removes from the cursor to the line end.
Repeat the previous edit . Repeats the most recent change.
Select characters / lines / block v / V / Ctrl+v Starts a Visual selection.
Indent / unindent selected lines > / < Shifts the selected text.
Re-indent the whole file gg=G Applies Vim indentation from first to last line. Review the result before saving.

Use Visual block mode carefully when editing aligned declarations. A rectangular edit can change several signal declarations at once, but it can also damage columns that were not meant to change.

Nano shortcuts

Nano shows shortcuts using ^ for Ctrl and M- for Alt. For example, ^O means Ctrl+O.

Action Shortcut
Save Ctrl+O, confirm the filename, then Enter
Exit Ctrl+X
Search Ctrl+W
Replace Ctrl+\
Cut current line Ctrl+K
Paste the cut text Ctrl+U
Start or finish a selection Alt+A
Copy selected text Alt+6
Undo / redo Alt+U / Alt+E
Go to line Ctrl+_
Show help Ctrl+G

Start Nano with visible line numbers when reviewing diagnostics that contain a line number:

nano --linenumbers rtl/counter.v

For multiple files, start nano --multibuffer file1 file2. Use Alt+< and Alt+> to move between buffers. Availability of long options can vary; check nano --help on the workstation.

Work with multiple RTL files in Vim

Open related files together:

vim rtl/counter.v tb/tb_counter.v
Task Vim command
List argument files :args
Next / previous argument :next / :previous
List open buffers :buffers
Next / previous buffer :bnext / :bprevious
Open a horizontal / vertical split :split file / :vsplit file
Move between split windows Ctrl+w followed by h, j, k, or l
Close the current split :close
Open a new tab :tabnew file
Next / previous tab gt / gT

Save each changed buffer deliberately. Vim warns when a command would abandon unsaved work; read the message instead of adding ! automatically.

Search an RTL project from Vim

From the project root, search Verilog files and open a quickfix list:

:vimgrep /module/j rtl/**/*.v
:copen
:cnext
:cprevious

For SystemVerilog, use rtl/**/*.sv. :vimgrep stores matches in the quickfix list, allowing you to visit each file and line. Close the list with :cclose.

GVim on a workstation

Start GVim with a file path:

gvim rtl/counter.v

GVim needs a graphical desktop or forwarded display. On a headless compute server, use terminal Vim instead. Do not enable graphical forwarding only to edit a small file if the approved environment does not support it.

Use a small, team-compatible Vim configuration

Test settings inside Vim before saving them in ~/.vimrc:

:set number
:set expandtab
:set tabstop=4
:set shiftwidth=4
:set softtabstop=4
:set autoindent
:syntax on

When the settings match the project style, a minimal ~/.vimrc can contain:

set number
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
syntax on
filetype plugin indent on

Follow the repository formatting rules when they differ. An editor configuration should help produce the required style, not silently rewrite an entire existing codebase.

Worked example

Create and edit a Verilog file with Vim

mkdir -p "$HOME/asic-linux-practice/editor-lab/rtl"
cd "$HOME/asic-linux-practice/editor-lab"
vim rtl/counter.v

Inside Vim:

  1. Press i to enter Insert mode.
  2. Type the module below.
  3. Press Esc to return to Normal mode.
  4. Enter :set number to show line numbers.
  5. Enter /count to search for count; use n for the next match.
  6. Enter :wq to save and exit.
module counter (
    input  wire       clk,
    input  wire       reset_n,
    output reg  [3:0] count
);
    always @(posedge clk or negedge reset_n) begin
        if (!reset_n) begin
            count <= 4'd0;
        end else begin
            count <= count + 4'd1;
        end
    end
endmodule

Verify the saved file without changing it:

sed -n '1,20p' rtl/counter.v
grep -En 'module|always|endmodule' rtl/counter.v

Expected result: the file contains one module, one sequential always block, and one endmodule. The line numbers from grep -En show where each item appears. This example uses Verilog-2001 syntax, so it does not require SystemVerilog mode.

If Icarus Verilog is available, perform a syntax-only compile:

iverilog -g2005 -tnull rtl/counter.v
printf 'compile-status=%s\n' "$?"

A valid module returns status 0 and normally prints no output.

Review two files side by side

After creating tb/tb_counter.v, open both files:

vim -O rtl/counter.v tb/tb_counter.v

Vim opens vertical splits. Use Ctrl+w followed by h or l to change windows. Search for counter in each file, run :set list to reveal tabs and trailing spaces, then use :set nolist to return to the normal display.

Repeat a small edit with Nano

nano rtl/counter.v

Use Ctrl+W to find counter, add a short comment, save with Ctrl+O, and exit with Ctrl+X. Run diff against a backup if you need to review exactly what changed.

Protect engineering work

Before a broad edit, create a deliberate backup or use version control:

cp -i rtl/counter.v rtl/counter.v.before-edit
vim rtl/counter.v
diff -u rtl/counter.v.before-edit rtl/counter.v

Vim may create a swap file when a session is interrupted. Read the recovery message carefully. Use vim -r path/to/file when recovery is appropriate; do not delete a swap file until you confirm that another editing session is not active.

Common mistakes

  • Typing text in Vim Normal mode and accidentally running commands.
  • Forgetting Esc before entering :wq or another command.
  • Using :q! and unintentionally discarding useful changes.
  • Running two editors on the same file and overwriting newer work.
  • Editing generated simulation output instead of the RTL or testbench source.
  • Opening GVim on a server without a graphical display.
  • Adding editor backup and swap files to source control.
  • Forcing a command with ! without reading why Vim refused it.
  • Re-indenting an entire existing file without reviewing the diff.
  • Mixing tabs and spaces against the repository formatting rules.

Check yourself

Which Vim key returns to Normal mode? How do w, gg, and % move the cursor? What is the difference between :q and :q!? Which command opens a vertical split? In Nano, which shortcut saves the current file?

Engineering practice

Practice: Vi, Vim, GVim and Nano for RTL/DV

Task→Run / reason→Check result→Explain why

Create tb/tb_counter.v. Use Vim or Vi to enter a small testbench skeleton, navigate with word and line movements, search for the module name, replace one signal name with confirmation, and save. Open the RTL and testbench in vertical splits and move between them. Reopen the testbench with Nano, add a comment, save it, and use diff -u to review the change. If an editor is unavailable, record the command lookup result and use an available editor.

Practice work is non-certified and is not stored by ASIC Campus.