Using Vim - CMU-18240/240-How-to GitHub Wiki

General Usage

Vim is a very powerful editor that is installed on nearly every Unix-like machine you log into. This makes it a good tool to learn because it is so portable. Several good tutorials exist, including https://www.openvim.com/, and the VimTutor (run vimtutor).

Vim Setup for 18-240

[!CAUTION] USE AT YOUR OWN RISK!

  1. Open up your vimrc
    • Type this into your terminal: vim ~/.vimrc
  2. Copy the vimrc from the pastebin link
    • The vimrc can be found here
    • Note: this vimrc is what I use. You may want to change a few minor things
  3. Go into insert mode by hitting i
  4. Hit colon (:) then type set nopaste
  5. (MAKE SURE FILE IS EMPTY) Right click in your vimrc to paste the copied vimrc file
    • If right click doesn’t work, try Ctrl+v.
    • If the file is not empty, type gg, then type dG (must be capital G!)
  6. Exit your vimrc by hitting ESC, then do one of 3 commands
    • a. Shift+zz (hold Shift, hit z twice)
    • b. (type) :wq (write, quit)
    • c. (type) :x (same as :wq)
  7. Install the plugin manager by typing in the following command into your terminal to install the Vundle plugin manager
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  1. Open your vimrc back up again
    • Type this into your terminal vim ~/.vimrc
  2. Hit colon (:), then type in PluginInstall and hit enter
  3. Exit your vim by hitting ESC to go to normal mode, then use the command of your choice described in step 6a, b, or c.
  4. Type the following commands into your terminal to install Pathogen plugin manager
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  1. Finally, to install Syntastic (linter/syntax checker), type the following commands into your terminal. This will NOT check c0 or SystemVerilog syntax, but it will check C syntax.
cd ~/.vim/bundle && \
git clone --depth=1 https://github.com/vim-syntastic/syntastic.git
  1. Type cd into your terminal to reach your home directory.

Short overview of helpful Vim commands

[!Caution] You MUST be in Normal Mode!!

  • Buffer Navigation:
    • Buffers are sort of like tabs in a browser. You can switch between them or have multiple open side by side to view different files.
    • Ctrl + N = open up file explorer
    • Ctrl + O, P, switch between left (O) and right buffer (P). Opened buffers can be seen at the top of the Vim window
    • :vsplit (or :vs) = split the screen vertically (can have two different files side by side)
    • :split (or :sp) = split the screen horizontally
    • Switching between open buffers: Ctrl + W to go into window selection mode. By default, the currently selected buffer is selected.
      • Hit an arrow key or hjkl equivalent to set focus to different buffer
      • Alternatively you can click on the buffer you want to edit with your mouse
    • :bd = close a buffer (a split screen or an open file or an open terminal)
      • If you want to close without also closing the file you can do Ctrl + W, then type q
    • :ter ++curwin = open a terminal window in the current buffer
      • You will notice that once you have an open terminal window, normal vim shortcuts stop working. To stop the terminal window from receiving text, hit Ctrl + W, then type Shift + N
      • This only works for the Linux machines
    • :terminal bash = open a terminal window, but works on ECE and Shark machines
  • Find and Replace
    • / [string to find] = search
    • ? [string to find] = search from back
    • :substitute (or :s) /[target string]/[replacement string] = find and replace
  • Copying and pasting (aka programming):
    • v (while in normal mode) = go to visual mode
      • Arrow keys to move highlight
      • Hit y to yank selected text into a buffer
      • Hit ESC to exit out of visual mode
    • You can also select text by clicking and dragging with mouse (same commands: y for copy, x for cut, p for paste)
    • p = paste whatever is in your buffer
      • Notice if you delete any lines using d, x, or otherwise, they will be stored in your buffer instead!
  • Code folding
    • Fold all functions and comments: type z, then type Shift+m
    • Unfold all functions and comments: type z, then type Shift+r
    • Unfold specific function: Go to the very left of the file, hit the + sign on the line you want to unfold
    • Fold specific function: Go to the very left of the file, hit the - sign on the line you want to unfold.
    • Additional commands
    • :Rename [filename] = renames a file
    • :! [terminal command] = runs a terminal command
    • gg=G Fixes your indentation over the entire file.

How do I exit vim?

See here and here

Other Tips and Tricks

  • To use vim to edit a file, run vim [file-name]
    • This will open up the file in the terminal.
  • The default mode of vim is normal mode. In this mode, you cannot edit the file.
  • To edit the file, press the i key. This puts you into insert mode, where you can edit the file as normal.
    • To return to normal mode, press the ESC key.
  • To navigate the file, use the hjkl keys or the arrow keys.
  • To save, in normal mode, type :w and press enter.
  • To exit the file, in normal mode, type :q and press enter.
    • You can both save and exit at the same type by typing :wq and pressing enter in normal mode With this, you know how to use vim at a very basic level. To find more commands, you can either use this resource: https://vim.rtorr.com/ or use google.
  • Expanding tabs to spaces in Vim

Syntax Highlighting

Due to the old Vim version on the ECE machines (7.4), Vim does not have syntax highlighting for SystemVerilog. We can add this functionality by executing the following commands:

  1. Create the required directories:

mkdir --parents ~/.vim/syntax ~/.vim/ftdetect

  1. Copy all of the needed files into these folders:

cp /afs/ece.cmu.edu/class/ece240/lib/sverilog-vim/verilog.vim ~/.vim/syntax/

cp /afs/ece.cmu.edu/class/ece240/lib/sverilog-vim/systemverilog.vim ~/.vim/syntax/

cp /afs/ece.cmu.edu/class/ece240/lib/sverilog-vim/verilog-detect.vim ~/.vim/ftdetect/

Credit to Michael Crotty for making these files!