vim: a great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 vim...

13
1/13 Vim: A great tool for your toolbox!

Upload: others

Post on 24-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

1/13

Vim: A great tool for your toolbox!

Page 2: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

2/13

History

I Originally created for the Unix operating system by BillJoy (one of the founders of Sun)

I Created a visual interface for the original ex line editor onUNIX

I The name vi is derived from the shortest unambiguousabbreviation for the command visual in ex

I The Single UNIX Specification specifies vi, so everyconforming system must have it.

I vi is not open source but there are several clones with themost popular being Vim

Page 3: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

3/13

Vim

I Vim is a clone of vi created by Bram Moolenaar and wasfirst released in 1991

I Vim stands for Vi IMprovedI Vim is not 100% compatible with vi as defined in the

Single Unix SpecificationI Vim has many improvements over standard viI Vim has a built-in tutorial for beginners (accessible

through the vimtutor command)I Vim has 6 modes, 4 of which you will spend 99% of your

time in.

Page 4: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

4/13

Vim Modes

I Normal mode - For navigation and manipulation of text.This is the mode that vim will usually start in, which youcan usually get back to with ESC.

I Insert mode - For inserting new text. The main differencefrom vi is that many important "normal" commands arealso available in insert mode - provided you have akeyboard with enough meta keys (such as Ctrl, Alt,Windows-key, etc.).

I Visual mode -For navigation and manipulation of textselections, this mode allows you to perform most normalcommands, and a few extra commands, on selected text.

Page 5: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

5/13

Vim Modes - continued

I Select mode - Similar to visual but with a moreMS-Windows like behavior.

I Command-line mode - For entering editor commands -I Try :helpI Try :Ni!

I Ex-mode - Similar to the command-line mode butoptimized for batch processing.

Page 6: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

6/13

Using Vim

I When using Vim the goal is to keep your hands on thehome row

I Primarily you will use normal mode, and insert mode whenediting text

I Visual mode allows you to cut and paste large swaths oftext all at once

I Command mode allows you to send commands to theeditor such as save (:w) or quitting (:q)

Page 7: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

7/13

Learning Vim - basic commands

I Saving a file - ESC then :wI Opening a file - ESC then :e <filename>I Quitting - ESC then :qI Save then quit - ESC then :wqI To switch to insert mode hit i. To get back to normal

mode hit ESCI Here is good guide to get you started with the basics.

http://cs.boisestate.edu/~amit/teaching/handouts/vi-two-page-ref.html

I vimtutor will walk you through all the basic commands.

Page 8: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

8/13

Learning Vim - Regular expressions

I Vim has a powerful regular expression engine built in tohelp you find and edit text.

I While in normal mode type /<regex> and Vim will findall the text that matches.

I You can also do a complex find and replace with:%s/<regex>/<new text>/cg which can be read asglobally (g) find all the text that matches the <regex>and replace it with <new text> and confirm with me (c)before you actually make the change.

Page 9: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

9/13

Customizing Vim

I When Vim starts up it looks for a file named .vimrc inyour home directory

I You can use your .vimrc with any version of VimI You can customize your .vimrc to add in any plugins or

settings that make Vim easier to use. For examplechanging the font to something easier to read.

I Take a look a Shane’s vimrc as a starting pointhttps://github.com/shanep/vim

I Vim takes some time to learn but once you have masteredit the payoff is faster editing and coding

Page 10: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

10/13

Customizing Vim - Font example

I The font is bad with the default install you can fix it withthe following code in your .vimrc

I if has("win32")set guifont=Consolas 18

endifif has("unix")

if system('uname')=~'Darwin'set guifont=Menlo\ Regular:h18

elseset guifont=Inconsolata\ Medium\ 18

endifendif

Page 11: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

11/13

Vim plug-ins

I Vim has an extensive set of plug-ins that you can leverageto add additional functionality. Below is a few plug-insthat you may want to try out.

I NERDTree - Gives you a buffer view of all the files anddirectories in your current working directory. (Similar tothe project window in eclipse)

I TagBar - Gives you a layout off all the functions, methods,classes, and structs of the currently loaded buffer. (Similarto the class view window in eclipse)

I SuperTab - Ties the built in omnicomplete to the tabbutton. (Not as great as code complete in eclipse butbetter than nothing at all!)

Page 12: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

12/13

GVim and MacVim

I There is a GUI version of Vim that is easier to use andmore friendly in a graphical environment. You get a toolbar that provides many of the common commands so youdon’t have to memorize everything!

I You can install this on Fedora Linux with the command:dnf install vim-enhanced vim-X11

I MacVim is a version of vim that has been customized forMac

I GVim is also available on windows.I You should generally use GVim or MacVim for daily usage.

The terminal version is handy if you are working over aslow ssh connection.

Page 13: Vim: A great tool for your toolbox!cs.boisestate.edu/~cs253/handouts/vim-handout.pdf · 4/13 Vim Modes I Normalmode-Fornavigationandmanipulationoftext. Thisisthemodethatvimwillusuallystartin,whichyou

13/13

References

I Wikipedia entry on Vi:http://en.wikipedia.org/wiki/Vi

I Wikipedia entry on Vim:http://en.wikipedia.org/wiki/Vim_(text_editor)

I wikibooks entry on Vim: http://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim/Modes

I Vim homepage: http://www.vim.org