Skip to content

Latest commit

 

History

History
349 lines (272 loc) · 9.7 KB

fall2018.md

File metadata and controls

349 lines (272 loc) · 9.7 KB

Vim Workshop

Hosted by Robert Quitt
https://tinyurl.com/csuavimfa18

Who are we?

Computer Science Undergraduate Association (CSUA)

  • Open to all people and majors interested in CS
  • Dedicated to serving CS undergraduate community
  • Socials
  • Infosessions/tech talks
  • Workshops (like this one)
  • Tutoring
  • Computing resources

Who am I?

  • Robert Quitt
  • 3rd year CSUA Officer
  • Vim user for a couple of years

Purpose

  • People tend to be turned off by Vim/terminal workflows
  • By the end of today, you should know how to use Vim

Benefits of Vim

  • Fast editing, fast editor
  • Ubiquitous
  • Works remotely
  • Powerful
  • A fun learning experience
  • Unique

Vim Setup

Done with Setup?

  • Play around with a few things while you wait!
  • Vim tutor (you are new to Vim)
  • *nix: run vimtutor
  • Windows: Search for Vim tutor in windows search
  • Familiarize yourself with the interface

Download:

https://drive.google.com/file/d/1MU-_VfddpETYuA5l4tpTcbl4qmeAFAMB/view?usp=sharing

Schedule

  • Diagnostic Quiz
  • Lecture + Examples

What's Vim?

  • A terminal-based text editor, around since 1991
  • Based on an older Vi, written in 1976
  • By far the most popular text editor for *nix users
  • Self-documenting (:help)
  • Customize keybindings and extend with plugins (.vimrc)
  • Modal editor (has multiple modes) (you will learn these later)
    • Normal
    • Insert
    • Visual

Quiz!

What does each of the following sequences of keys do (assume we start in normal mode)

  1. ihi
  2. dd
  3. :q!
  4. :%s/foo/bar/g
  5. caw
  6. qsA;<ESC>qj@s
  7. yip
  8. <C-w>l
  9. :set expandtab
  10. u

Answers

What does each of the following sequences of keys do (assume we start in normal mode)

  1. ihi : enter insert mode, insert "hi"
  2. dd : delete line, send to default register (")
  3. :q! : force quit
  4. :%s/foo/bar/g : replace all instances of "foo" with "bar" multiple times per line
  5. caw : "change a word", deletes word under cursor and enters insert mode
  6. qsA;<ESC>qj@s : create macro s, which appends ; to the end of the line, move down a line, run macro s
  7. "tyip : "yank inner paragraph", yanks paragraph to register t
  8. <C-w>l : move to window to the right
  9. :set expandtab : sets expandtab to true, which causes tabs to be inserted as spaces
  10. u : undo last command

Philosophy of Vim

  • Edit text using a sequence of operations and motions
  • An operation needs a motion or selection to operate over
  • Have modes to do different things
  • Stay in normal mode, quickly go into other modes to do edits
  • Normal, Command, Visual, Insert
  • There can be multiple ways to do the same thing
    • But usually there's one best way
  • Manipulate text by composing many operations, motions, selections

Starting Vim

  • On command line: vim [filename]
  • Or: vim
    • then type :e <filename>

Entering Insert Mode

  • Vim starts in Normal Mode
  • Entering insert mode:
    • i : insert at cursor
    • I : insert at beginning of line
    • a : append after cursor
    • A : append at the end of line
  • Exiting insert mode:
    • Esc or C-c
  • Moving around in normal mode:
    • h : left
    • j : down
    • k : up
    • l : right

Command Mode

  • Invoke by using : in normal mode
  • Basic necessary commands
    • :w[rite] [filename]
    • :q[uit]
    • :q[uit]! : don't save
  • Some useful commands
    • :h[elp] [subject]
      • This is your best best friend for learning vim
    • :se[t] {option}
      • Used for setting vim settings in the current session

Exercise 1

  • Open ex1_getting_started.txt
  • Remember:
    • h j k l to move in normal mode
    • i to insert, a to append
    • I to insert at beginning of line, A to insert at end of line
    • ESC to exit insert mode back to normal mode
  • Typical flow: stay in normal mode, enter insert mode, edit, and then go back to normal.

Normal Mode Basics

  • Normal mode common commands
    • dd : delete line
    • yy : yank line
    • p : paste below
    • P : paste above
    • o : open line below--creates a new line below cursor and enters insert mode
    • O : open line above--creates a new line above cursor and enters insert mode
    • u : undo last command
    • C-r : redo last command

Search and Replace

  • Start with /
  • Press enter
  • n : next result
  • N : previous result
  • Supports regexes
  • Some settings:
  • :set incsearch : set incremental search--starts searching for partially typed queries
  • :set hlsearch : set highlight search
  • :noh : clears highlighted search

Operator/Motion Composing

  • Vim has operations and motions
  • This allows a lot of flexibility in your commands
  • Operators:
    • d : delete
    • y : yank
    • c : change
    • gU : uppercase
    • = : format
    • > : shift right
  • Motions/Text object motions(just a few)
    • w : word
    • b : previous word
    • aw : "a workd"
    • 4j : 4 lines down
    • /foo : next occurence of foo
    • tc : "unTil c"
    • fr : right aFter "r"

Operator/Motion Examples

  • Additionally, can use numbers to repeat many operators
    • 3dd : delete 3 lines
  • Also, repeat the operator to operate on that line
    • yy, dd, cc, >>, gUU should be self-explanatory
  • Composing operators and motions:
    • ciw : change inner word
    • gUi) : make the text inside a pair of parentheses uppercase
    • yap : yank a paragraph
    • d4W : delete the next 4 WORDs

Working with Indentation

  • Need to :set shiftwidth to choose how far to change indent
  • << and >> change indentation for current line
  • {visual}< and {visual}> change indentation for selection
  • Use . to repeat indentation change
  • Exercise: ex3_bad_indent.c

Macros

  • Macros allow you to repeat repetitive keystroke sequences
  • Examples tend to be things like, adjust the format of particular lines
  • Use q<register> followed by the keys you want to use
  • Using ESC to go from normal mode to insert mode sometimes screws things up
  • If that happens, use C-c instead
  • To run a macro, use @<register>
  • Repeat last macro: @@
  • Repeat multiple times: 50@@

Visual and Visual Block mode

  • Use this mode to select text to then use an operator on
  • Useful for selecting text precisely
  • v : enter visual mode
  • V : enter visual line mode
  • C-v : enter visual block mode
    • Special: for visual block mode, use A or I to add to the end of each line
    • Use the motions ^ for beginning of line and $ for end of line

Exercise: Vim Golf

  • Use your documentation-reading abilities to find a solution to the following problem.
  • In ex4_vim_golf.txt, transform the text in the fewest keystrokes possible!
  • Find a way to replace all instances
  • be sure to look at :h motions.txt

Vim Tips

  • Change your keyboard settings to have low repeat delay and high repeat rate for a more enjoyable Vim experience

Customizing Vim

"Hey wanna check out my dotfiles"

  • Use .vimrc to customize your vim experience
  • .vimrc is executed as commands when vim is started up
  • Can contain plugins, settings, keybinds
  • Vimawesome.com lists the most popular vim plugins

Vim Colors

  • Vim has very extensible syntax highlighting
  • :setf[iletype] {filetype}
  • This changes the syntax type as well
  • Use this when vim has the wrong syntax identified
  • Install support for other syntax files using plugins
  • Colorschemes - change the way vim looks
  • Set with :colo[rscheme] {name}
  • Many many colorschemes around.
  • Some personal favorites: Molokai, Solarized.
  • Install the same way as plugins

How to Learn More?

  • Countless talks on Vim productivity
  • Most popular text editor -- there's a large community
  • Print out a cheatsheet!
  • Keep using it! Don't give up!
  • vimcasts.org
  • Webcasts of practical examples of things to do with Vim
  • vimawesome

Extra Content (time permitting)

Dotfiles

  • Put them in a repo so that you can easily copy them and keep them updated on all your machines you work on
  • Lets you move your config around easily!
  • Good to have an understanding of how git works to do this
  • First thing I do when I log into a machine is clone my dotfiles!

Whitespace Management

  • :set list helps view tabs vs spaces
  • Should also set listchars in .vimrc
  • See ex5_whitespace.py

Vim Tabs and Windows

  • Can have multiple buffers open at once as tabs
  • From :help windows.txt
  • A buffer is the in-memory text of a file.
  • A window is a viewport on a buffer.
  • A tab page is a collection of windows.
  • A window is a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers.
  • A buffer is a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file.
  • Use C-w s for split, C-w n for new, C-w = for set windows equal size

Autocompletion

  • Gazillions of articles online about how to do this
  • Options
    • youcompleteme
    • OmniComplete (Vim >8.0)
  • Read more about these, I personally don't use them. you can use the * command to search for a keyword

Vim in Chrome

Vimium is a plugin that lets you use the vim keybinds for Chromium Very fast web browsing

How to develop dotfiles

  • Start with a base
  • Modify and add things until you are happy with what you have
  • Keep modifying it forever, you will never be 100% happy with it
  • Many online resources (or just search for dotfiles on github!)

Questions?

Thanks for coming!
Please fill out feedback form!
https://goo.gl/forms/yFEW5A966XzsD3812