Docsity

Vim Text Editor for Programmers: A Comprehensive Guide - Prof. Prasad Kulkarni, Lab Reports of Operating Systems

Partial preview of the text

Download Vim Text Editor for Programmers: A Comprehensive Guide - Prof. Prasad Kulkarni and more Lab Reports Operating Systems in PDF only on Docsity!

Text Editors for Programmers

Dr. Prasad Kulkarni

Michael Jantz

Some Common Editors

  • Lightweight UI, limited features
    • pico, nano
  • Heavier UI, limited features
    • gedit, kate
  • Heavier UI, good features, customizable
    • Eclipse, Anjuta
  • Lightweight UI, good features, customizable
    • emacs, vim

Focus On

  • Vim
  • xemacs (an extension of emacs)
    • Slides are provided, but this lab will cover only Vim.

Using Vim on a Simple Example

  • Goto http://people.eecs.ku.edu/~mjantz/678labs/lab01/
    • Make a directory for this lab and save simple.c, simple.h, and makefile in this directory.
    • Save vimrc as .vimrc in your home directory
      • Use mv to rename the file
        • mv vimrc ~/.vimrc
  • .vimrc
    • A collection of vim commands run each time you start vim
    • Used to set mappings / options that are not otherwise set by default.

Vim โ€“ Modes of Operation

  • Command Mode
  • Input Mode
  • Visual Mode Command Input Visual V, v, ctrl - v I, i, A, a, O, o, R, r Escape Escape

Essential Commands

  • :e file
    • Edit file in a new buffer
  • :w
    • Save any modifications to the current buffer.
  • :q
    • Quit Vim. If you have modifications you do not want to save, use :q!

Input Mode

  • The following commands switch to input mode:
    • i โ€“ characters inserted just before the cursor position
    • I โ€“ characters inserted at the beginning of the line
    • a โ€“ characters inserted just after the cursor position
    • A โ€“ characters appended to the end of the line
    • o โ€“ characters inserted in a new line below the cursor
    • O โ€“ characters inserted in a new line above the cursor
    • C โ€“ Often overlooked, deletes the line after the cursor position and start inserting characters at this position
  • After you're done editing, press Escape to go back to command mode, and :w to write the changes

Common Editor Commands

  • Cut/copy/paste in command mode:
    • dd โ€“ cut a line of text
    • yy โ€“ copy (โ€œyankโ€) a line of text
    • P/p โ€“ paste a line of text above / below the cursor position
  • Commands in Vim can be applied to multiple lines by typing the number of lines you want before the command: - โ€œ12ddโ€ cuts 12 lines of text - (^) โ€œ4jโ€ moves the cursor down 4 lines

Searching

  • / word โ€“ Search for occurrences of word
    • Cursor jumps to the next occurrence of word
    • n/N โ€“ jump to the next / previous occurrence of word -? word โ€“ search initially jumps to previous occurrence of word.
  • :set ic โ€“ ignore case
  • :nohlsearch to turn off highlighting from last

search. Mapped to nh in .vimrc.

Find/Replace

  • :s / search_for / replace_with /
  • Variations
    • :s / s / r /g โ€“ Replace every occurrence on the line (not just the first)
    • :%s / s / r /g โ€“ Replace every occurrence in the current buffer
    • :s / s / r /g 12 โ€“ Replace for the next 12 lines
    • :s / s / r /gc โ€“ Replace, but get confirmation before doing so
    • :s / s / r /gi โ€“ Ignore case when searching for s.

Buffers

  • Vim allows you to edit multiple files in one

session using buffers

  • <c-w> v to split the screen vertically
  • <c-w> s to split the screen horizontally
  • <c-w> w to switch to the other screen
  • :V/Sex โ€“ splits the screen vertically or horizontally and opens a file explorer in the new screen.
  • Select simple.h to open it in the new screen.

Installing Buffer Explorer

  • Vim has built-in commands to work with its open buffers, but there is a plugin that allows you to visualize and navigate the open buffers. - Goto http://vim.sourceforge.net/scripts/script.php?script_id= - Download the latest version of bufexplorer.zip and extract it - In your home directory, if a .vim/ directory does not exist, create it: - ls -a .v* - If .vim/ is not present, do mkdir .vim - Now, move the contents of the extracted bufexplorer folder into .vim/: - mv bufexplorer/doc/ bufexplorer/plugin/ .vim/ - Inside your vim session do: - :helptag ~/.vim/doc/ - Quit and reopen vim

Tagging the Source

  • Big advantage to Vim (and other more

sophisticated editors) is its integration with a

source code tagging program.

  • Inside a terminal, goto the directory of the

simple source and type:

  • (^) ctags -R
  • Should create a file named tags. Now, reopen

simple.c in Vim.

Using Tags with Vim

  • <c-]> - With your cursor over a variable,

jump to the declaration of that variable.

  • <c-t> - Having jumped to a declaration, go

back to the spot you jumped from

  • You can use <c-]> multiple times before using <c-t>. The functionality operates like pushing and popping frames on a stack.
  • Extremely helpful for browsing and learning

large programs.