Vim

From DISI
Revision as of 21:51, 9 October 2018 by Benrwong (talk | contribs)
Jump to navigation Jump to search

Introduction

Start vim by editing a single file with the command:

vim file

If you input a nonexisting file name, it will start creating a new file.

vim newfile

vim has two modes: edit mode and insertion mode. vim begins in edit mode which does not allow you to type text into the file.
Only in insertion mode can you insert text with the keyboard. You can enter insertion mode by pressing 'i'. While in insertion mode, you can press 'Esc' to return to edit mode.

Edit Mode Commands

x : deletes character that is under cursor
dw: delete from cursor to next word
dd: delete entire line that cursor is on
p : paste previously deleted material
wq or x : save and quit
w : save 
w filename: save as "filename"
q!: quit without saving
v: enters Visual Mode 

To comment out multiple lines at once: With the cursor at the first line you want to comment out type:

ctrl+v

Arrow down to last line you want commented out. Then type:

shift+i
#
esc
enter

To replace every occurrence of a word with another word (for example replace the word ‘foo’ with the word ‘bar’) type:

%s/foo/bar/g

Visual Mode

Visual mode in vim allows you to highlight characters or lines. Once your desired string has been highlighted, you can press 'y' to copy the highlighted string.
Use 'p' to paste the previously copied string.

Substitution

While vim is in an open file, use command ':set number' to see the line numbers of a text file.