Using the Vi Text Editor

Similar documents
Basic vi Commands. What is vi? To Get Into and Out Of vi

vi filename edit filename starting at line 1

Editors in Unix come in two general flavours:

CSCI 211 UNIX Lab. Text Editor - Vi. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Software I: Utilities and Internals. What is vi?

vi Primer Adapted from:

VIP Quick Reference Card

Computer Programming Lecture 3 이윤진서울대학교

Statistics 202A - vi Tutorial

Using CRISP. I. CRISP Basics To create a new file, at the UNIX prompt type: cr newfilename

Text Editing in UNIX. A short introduction to vi, pico, and gedit

BEG 6. 50p. Getting Started with the Emacs Screen Editor. An introduction to the Emacs screen editor, which is available on Unix systems.

Bok, Jong Soon

Program Development Tools. Lexical Analyzers. Lexical Analysis Terms. Attributes for Tokens

UNIX: Using PICO on Central UNIX

Using the Command-Line Interface

Appendix C. Icon and Text Editors

Vim Habits. Detect inefficiency Find a quicker way Make it a habit

Lab 3a Using the vi editor

VIM Tips and Tricks. Search and replace a string in a document : :.,$s/search_string/replacement_string/g

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

VI (and Clone) Introductory Guide

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

Introduction to Linux Environment. Yun-Wen Chen

Text Editors for Programmers. Dr. Prasad Kulkarni Michael Jantz Jamie Robinson

Mastering Linux by Paul S. Wang Appendix: The emacs Editor

Introduction to EMACS. Harriet Borton Academic and Research Computing

NETW 110 Lab 3 Post-Installation Configuration Page 1

Once you have installed MobaXterm, open MobaXterm. Go to Sessions -> New Session, and click on the SSH icon.

BASIC VI COMMANDS. Move cursor down one line [or down-arrow] 2. k[or up-arrow] Move cursor up one line 3. h or<backspace> Move cursor left one line

<<<<<<<<<<BASIC VI COMMANDS>>>>>>>>>>>>>>>>>>

OFFICE OF INFORMATION TECHNOLOGY

VI Commands Cheat Sheets

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

Intel Edison Tutorial: Introduction to Vim 1

Introduction to the Emacs Editor

VIM Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : May, More documents are freely available at PythonDSP

Learning the vi text editor

Introduction to UNIX Part II

Getting Started. Running Utilities. Shells. Special Characters. Special Characters. Chapter 2 Unix Utilities for non-programmers

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Mastering Linux by Paul S. Wang Appendix: Text Editing with vi

FILE MAINTENANCE COMMANDS

Getting Started with UNIX

MICROSOFT EXCEL KEYBOARD SHORCUTS

Emacs manual:

Outline. Structure of a UNIX command

The Xemacs Editor. Overview

h/w m/c Kernel shell Application s/w user

Basic File Attributes

Introduction to GNU Emacs

Emacs Tutorial. Creating or Opening a File. Geog 405/605 Computer Programming for Environmental Research Fall 2018

Creating Your Own Student Accounts On Central UNIX and OpenMail

Using the Zoo Workstations

INTRODUCTION TO VIM. John Kerl University of Arizona Department of Mathematics Software Interest Group September 7, 2005

7.1 INTRODUCTION 7.2 STARTING Emacs 7.3 Emacs SCREEN Menu Bar Text Mode Menu Mode Line Echo Line

Text Editors for Programmers. EECS-678 Staff

Unix File System. Learning command-line navigation of the file system is essential for efficient system usage

Linux 下代码查看 编辑 编译与调试 (I) 袁华

Helpful Tips for Labs. CS140, Spring 2015

Unix Philosophy, Text Editors, IDEs

Lecture # 2 Introduction to UNIX (Part 2)

KEYBOARD SHORTCUTS AND HOT KEYS

Microsoft Excel > Shortcut Keys > Shortcuts

Std: XI CHAPTER-3 LINUX

The most important Mac keyboard shortcuts

Computer Networks Vim and Emacs. Seongjin Lee

Mills HPC Tutorial Series. Linux Basics I

Section 2. Opening and Editing Documents

entity tomove over backward forward character C-b C-f word M-b M-f line C-p C-n go to line beginning (or end) C-a C-e sentence M-a M-e paragraph M- M-

Switch between open apps Close the active item, or exit the active app

Microsoft Office Word. Part1

CS4350 Unix Programming. Outline

Getting started with UNIX/Linux for G51PRG and G51CSA

CMPT 300. Operating Systems. Brief Intro to UNIX and C

AMS 200: Working on Linux/Unix Machines

Commodore Amiga Unix System V Release 4

Intro. To Unix commands. What are the machines? Very basics

Version Operator Orientation. TIMMS Client. A guide to using the TIMMS System. Training & Navigation Notes

Getting Started with Linux Editors. Network Startup Resource Center

Getting Started With UNIX Lab Exercises

CHE3935. Lecture 1. Introduction to Linux

Introduction. SSH Secure Shell Client 1

Short Read Sequencing Analysis Workshop

Textadept Quick Reference

Menu Symbols. Menu Symbol. Key on Keyboard

Refresher workshop in programming for polytechnic graduates General Java Program Compilation Guide

Getting Started With XEmacs

Linux/Cygwin Practice Computer Architecture

Console User Guide. Tones on For Console. Transferring a Call. Placing a Call on Hold. Answering Calls

90 + Excel Keyboard Shortcuts. for Mac users. How to master key board shortcuts

UNIX Quick Reference

Section 5: Editing Cells

Computer Shortcuts. Files menu options in current program. Edits options in current program Universal Help in almost every Windows program.

Using nu/tpu on UNIX Last revised: 1/28/00

Vi Command & Function Reference

Operating System Interaction via bash

Introduction. File System. Note. Achtung!

UNIX Commands. Ex: $ pwd $/tmp $cd/home/sales, this will change the directory from /tmp to /home/sales.

Transcription:

Using the Vi Text Editor This document is intended to provide you with the basic information you need to use the vi text editor from your WAM/Glue or OIT cluster UNIX account. This document assumes that you are using your account from some kind of terminal window either by dialing in from home, by using a Mac or PC in a computer lab or office at the University, or by using a terminal window generated by the xterm program on a UNIX workstation. Information on how to connect to the OIT host computers with a terminal program is available on-line at http: //www.helpdesk.umd.edu/topics/applications/terminal. Vi is a full-screen text editor that is almost universally available on UNIX-based computer systems. This editor is available on the UNIX systems and the WAM workstations. Vi is useful for editing program files, entering data, composing mail messages, and plain text editing. There are other text editors that can be used on UNIX systems; one of the most popular of these is pico. Pico is available on all of the UNIX systems maintained by OIT. Another text editor available on UNIX systems is emacs. Documents describing basic pico and emacs commands are available on-line at http: //www.helpdesk.umd.edu/documentation. In this document the word current, when used in reference to some unit of text, identifies the location of the cursor. For example: current character, current word, current line. This document also uses several fonts to help clarify the meaning of the text: fixed Literal commands you type appear in fixed format. italic Variables used with commands will appear in italic format. [ ] Optional variables will appear in square brackets. To edit a file with vi, type any of the following commands at the UNIX system prompt: Introduction Note: Access to WAM labs at the University is limited. You must have a valid University ID to use the labs. Note: For more about using the OIT UNIX cluster, refer to Introduction to the OIT UNIX System. It is available on-line at http://www.helpdesk.umd.edu/ documentation/unix/cluster.shtml. You may also take the Peer Training Course, Introduction to UNIX. Call 301.405.2941. Conventions Used in This Document Note: UNIX is case-sensitive. This means it makes a difference whether you type commands or filenames in UPPER or lower case. Thus FILE3 is different from file3. Starting Vi vi vi filename vi -r filename vi -R filename create a new, unnamed file edit an existing file or create a new file named filename recover an edit session that was interrupted by a system crash (this may not recover all of the changes you made to your file in your last editing session) open the file in Read Only mode Office of Information Technology University of Maryland, College Park, MD 20742 Updated October 2004

Vi Modes Entering Input Mode Using the Vi Text Editor Vi has two modes: input mode and command mode. While in input mode, everything you type is inserted into your document (including command mode commands). While in command mode, everything you type is executed as a command to edit your document. To change from input mode to command mode, press the Esc key. There are several ways to change from command mode to input mode that are listed below. All of the following commands will change your mode from command to input: i I a A o O enter input mode before the cursor enter input mode at the start of the current line enter input mode after the cursor enter input mode at the end of the current line create a new line below the cursor and enter input mode on it create a new line above the cursor and enter input mode on it Moving Around Within the Current Text Window The following commands allow you to move around the current text appearing on your screen. Most of the commands can be preceded by a number, which will move the cursor that number of characters, words, or lines. For example, 4j would move the cursor down four lines. When a number is not specified, one space or line is assumed. Please Note: Be careful when using the arrow keys on your keyboard as they often do not work properly with vi and may cause unpredictable results. [num] h move num spaces to the left [num] Backspace move num spaces to the left [num] l move num spaces to the right [num] Spacebar move num spaces to the right [num] j move down num lines in the same column [num] k move up num lines in the same column [num] Return move to the beginning of the numth line down [num] + move to the beginning of the numth line down [num] move to the beginning of the numth line up [num] w move right to the beginning of the numth word following the current word [num] e move right to the end of the numth word counting the current word [num] b move left to the beginning of the numth previous word ^ or 0 move to the beginning of the current line $ move to the end of the current line H move to the beginning of the first line on the screen M move to the beginning of the middle line on the screen L move to the beginning of the last line on the screen 2 University of Maryland

Using the Vi Text Editor These commands allow you to change the portion of your document that appears on your screen and to move around the document quickly. Ctrl-f move forward a screen Ctrl-b move back a screen Ctrl-u move up half a screen Ctrl-d move down half a screen num G move to the specified line (use Ctrl-g to display line number) :num move to the specified line G move to the last line in the file :$ move to the last line in the file Vi has search commands to find a particular section of your document quickly. Moving Around the Entire Document Searching for Text / pattern search forward through the document for the next occurrence of the pattern (or string of text)? pattern search backward through the document for the next occurrence of the pattern (or string of text) n repeat search in the same direction N repeat search in opposite direction f char search forward from the cursor in the current line for a single character (char) F char search backward from the cursor in the current line for a single character (char) ; repeat single character search in either direction (f or F) Below is an example of vi s searching feature. Pressing the ESC key, followed by the slash ( / ) to indicate a search (which will take you down to the command line), followed immediately by a searchstring (where searchstring in this example is EDITOR), vi will position the cursor at the location of the search string. # Run user environment customizations identified in your /.environment # file. This is the place to include your own environment variables, # tap commands, and other system wide setup commands. You can also # cancel default behaviors listed above with unsetenv or setenv. # /.environment is not sourced if NOCALLS is set (i.e., if # the xlogin SYSTEM option is selected). # The below line, if commented out, will cause X windows to automatically # come up if logging in on the console. setenv NO_X_WINDOWS 1 # uncomment the following three lines if your prefer vi #setenv EDITOR /usr/ucb/vi #setenv VISUAL /usr/ubc/vi #setenv TEXEDIT /usr/ucb/vi +%d %s # comment these out if you want vi or emacs setenv EDITOR /usr/local/bin/pico setenv VISUAL /usr/local/bin/pico setenv TEXEDIT /usr/local/bin/pico +%d %s /EDITOR Example Search Note: The cursor was located at the beginning of the document when the search was initiated. /searchstring Office of Information Technology 3

Deleting Text Using the Vi Text Editor The following commands allow you to delete single characters, words, or whole lines of text with a single command. Most can be preceded by a number to delete more than one character, word, or line at a time. For example, 3dd would delete three lines at once. When the number is not specified, one character, word, or line is assumed. These commands put the deleted text into the buffer, which can then be placed elsewhere in the document by using the put commands. While in input mode, you can use the Backspace key to correct mistakes on the current line of text. x [num] x X [num] X [num] dw D [num] dd delete character under cursor delete num characters from cursor forward delete character before the cursor delete num characters before the cursor delete num words starting with the current word at the cursor delete text from the cursor to the end of the current line delete num lines starting with the current line Copying ( Yanking ) and Pasting Text Vi allows you to copy or delete blocks of text and place them elsewhere in your document. The put commands below insert any text which has been put into the buffer by the delete commands above or the yank (copy) commands below: [num] yw [num] yy [num] Y p P copy num words into the buffer, starting with the current word at the cursor copy num lines into the buffer, starting with the current line copy num lines into the buffer, starting with the current line put any text in the buffer after or below the cursor put any text in the buffer before or above the cursor position of cursor before 4yy performed position of cursor before p performed Note: The four lines have been duplicated immediately below their original position. 4 Below illustrates the copying feature. In this example, the cursor is positioned before uncomment, typing 4yy yanks 4 lines into the buffer, then the cursor moves to a destination position where the text is placed. Typing p, places the contents in the buffer below the cursor. # Run user environment customizations identified in your /.environment # file. This is the place to include your own environment variables, # tap commands, and other system wide setup commands. You can also # cancel default behaviors listed above with unsetenv or setenv. # /.environment is not sourced if NOCALLS is set (i.e., if # the xlogin SYSTEM option is selected). # The below line, if commented out, will cause X windows to automatically # come up if logging in on the console. setenv NO_X_WINDOWS 1 # uncomment the following three lines if your prefer vi #setenv EDITOR /usr/ucb/vi #setenv VISUAL /usr/ubc/vi #setenv TEXEDIT /usr/ucb/vi +%d %s # uncomment the following three lines if your prefer vi #setenv EDITOR /usr/ucb/vi #setenv VISUAL /usr/ubc/vi #setenv TEXEDIT /usr/ucb/vi +%d %s # comment these out if you want vi or emacs setenv EDITOR /usr/local/bin/pico setenv VISUAL /usr/local/bin/pico setenv TEXEDIT /usr/local/bin/pico +%d %s University of Maryland

Using the Vi Text Editor Vi has several commands that allow you to change an existing piece of text without first deleting it; the change commands overtype the current text. Changing Text [num] cw C r R s S change num words, starting at the cursor in the current word the word(s) will be replaced by any text you type until you press Esc change the current line from cursor to end the current line will be replaced by any text you type until you press Esc replace the current character (you do not need to press Esc) edit the current line from cursor to end in typeover mode you will remain in typeover mode until you press Esc (you can press Return to insert more lines before pressing Esc) replace the current character the current character will be replaced by any text you type until you press Esc replace the entire current line the current line will be replaced by any text you type until you press Esc There are several ways to save your document and exit vi. Be sure to use the save command often when editing an important document. :w save changes (i.e., write) to your file :w filename save changes (i.e., write) to the file specified :q quit vi (when you haven t made any changes) :q! quit without saving changes :wq or ZZ save changes to file and then quit :! cmd execute a single command (cmd) and return to vi : sh start up a new UNIX shell to return to vi from the shell, type exit or Ctrl-d Saving Your File and Quitting Vi Note: The :! and :sh commands make it easy to execute UNIX commands without exiting vi. Vi has several options which affect the way vi functions and alter your editing environment. These options can be set by hand from within vi (must be reset every time you use vi), or they can be made permanent (i.e., set automatically) by creating a.exrc file and including the commands there or setting the EXINIT environment variable in your.login or.cshrc file. (Do not alter the.login or.cshrc files.) Use the set all command to get a list of all of the options. :set :set all :set option=value :set option :set no option list the currently set editor options list all of the editor options set an option that takes a numeric or string value turn on a toggled option turn off a toggled option Office of Information Technology 5

Miscellaneous Vi Commands More Information? The following commands are helpful when using vi: Using the Vi Text Editor u undo the last change U undo the changes made to the current line. repeat your last command again return to your previous position return to the beginning of the line at your previous position in the document Ctrl-l redraw the screen if it has been altered by output from some other program or a transmission error (such as a talk request) Ctrl-g print the line number of the current line and how many lines are in the document % show matching (), {}, or [] when the current character is one of the characters [num] J join num lines together, starting with the current line (delete the Returns between the lines) :r filename read a file into the document below the current line change the case of the current character For technical assistance with the vi editor, one point of contact is the OIT Help Desk. There are many ways to contact this resource: Walk in: Computer & Space Sciences Building, room 1400, Monday - Friday, 8:00 a.m. - 5:00 p.m. Phone: 301.405.1500, Monday - Friday, 8:00 a.m. - 5:00 p.m. Web site: http://www.helpdesk.umd.edu/ You can also submit a problem report to the OIT Help Desk by typing request at the UNIX command prompt. 6 University of Maryland