linuxdays - [project 21] · – system administration: shell scripts, config files. where to get it...

58
LinuxDays Lecturer: Introduction to Vim Lukas Tobler HS2017, Version 1.0

Upload: others

Post on 24-Jan-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

LinuxDays

Lecturer:

Introduction to Vim

Lukas Tobler HS2017, Version 1.0

Page 2: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

A brief history

● 1976: Vi, Bill Joy● 1984: GNU Emacs, Richard Stallman● 1991: Vim, Bram Moolenaar (Amiga 2000)

[2] Richard Stallman [3] Bram Moolenaar

Page 3: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

What is Vim?

● Vi IMproved● Text editor (think Notepad++, but good)● Edits plain old text● Program source code● But also creative writing, note taking, …

Page 4: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Screenshot

Page 5: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Editor learning curves

Page 6: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Why is it good?

● Very efficient way to edit text● No mouse input required!● No awkward keyboard shortcuts● Commands are composable (Vim is a “language”)● “Ergonomic”

Page 7: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Why is it good?

● Can navigate large code bases comfortably● Distraction free environment

Page 8: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Why is it good?

● Highly flexible, configurable, extensible● Available everywhere (Linux, BSDs, Mac, Win, ...)● Free software, will never go away!● Lots of fun

Page 9: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

The test of time

[1] Orignial presentation by Bram Moolenaar: Vim 25

Page 10: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

The test of time

[1] Orignial presentation by Bram Moolenaar: Vim 25

Page 11: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

What it is not

● Not an IDE (pretty close though)● Cannot replace Eclipse, IntelliJ, Visual Studio,

PhpStorm, …● But doesn’t have to!

Page 12: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

What should I edit then?

● You don’t need/have an IDE for lots of things– C, C++, Python, Haskell, Rust, Go, Fortran– Build systems: Makefiles, CMake– Web development: HTML, CSS, JavaScript, PHP, Ruby– LaTeX, Markdown– System administration: Shell scripts, config files

Page 13: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Where to get it

● Newest Version: Vim 8.0● Linux: package manager. vim, gvim (might be named

differently, beware different versions!)● Mac: comes preinstalled, but use MacVim instead

(→ Homebrew package manger)● Windows: download from vim.org

Page 14: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

If you depend on IDEs

● Don’t worry!● Eclipse: Vrapper● IntelliJ: IdeaVim● Visual Studio: VsVim

Page 15: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Lets get into it

● Vim is very different from any other editor you might have used!

“Empty your cup so that it may be filled; become devoid to gain totality.” - Bruce Lee

Page 16: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Modal editing

● Few CTRL keybindings● “Letter” keybindings● Different modes!

– Normal mode– Insert mode– Visual mode– Command line mode

Page 17: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Transitions

Insert Visual

Normal

v, VESC

i, I, a, o, ... ESC, d, y, ...

c, r, ...

Page 18: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Command Line Mode - Demo

● Enter with :● :e[dit] edit a file

● :q[uit] quit

● :q[uit]! quit without saving

● :w[rite] save the open file (write)

● :wq write and quit

Page 19: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Command Line Mode - Demo

● Vim help pages

● :h[elp]● :help [topic] e.g. :help write

● CTRL-] follow a link

● CTRL-t jump back

● … or google it.

Page 20: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Normal Mode - Demo

● Navigation● Cursor movement● Deleting● Copy/Paste● Text modification

Page 21: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

ADM-3A keyboard

Page 22: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

System settings - recommendations

● Swap Caps Lock and Escape– Caps Lock is a comfortable key, but nobody uses it– Linux (temporary):

$ setxkbmap ch -option caps:swapescape

ch, de, en_US

Page 23: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Arrow keys are the devil - Demo

● Don’t use arrow keys! It slows you down.

h left

k up

j down

l right

Page 24: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Scrolling - Demo

● Don’t use j, k to scroll! Instead:

● Ctrl-e One line down

● Ctrl-y One line up

● Ctrl-d Half page down

● Ctrl-u Half page up

● Ctrl-f One page down

● Ctrl-b One page up

● Remember only the ones you like!

Page 25: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

System settings - recommendations

● Increase keyboard auto-repeat rate– Linux (temporary):

$ xset r rate 230 30

Delay in ms Repeat speed in Hz

Page 26: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Undo / Redo

● u undo● Ctrl-r redo

Page 27: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Movement: Words

● w, W start of next (big) word● e, E end of a (big) word● b, B backwards to start of (big) word

Page 28: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

What is a word?

● This is an example sentence.

● This one has a “string_literal” in it.

“small” word: only letters & numbers.

“big” word: anything thats not a space.

Page 29: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Actions: Deleting stuff

● d Delete● dd Delete line● D Delete rest of line● x Delete character

Page 30: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Actions: Changing stuff

● c Change● cc Change line● C Change rest of line● rx Replace character with x

Page 31: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Examples - Demo

● dW delete word forward● dB delete word backward● ciw change inside word

Page 32: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Movement

● h,j,k,l left, down, up, right● 0, $ start / end of line● ^ first non-whitespace character● gg, G start / end of file

Page 33: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Find things - Demo

● fx go to next occurrence of x● Fx go to previous of x● tx go to before occurrence of x● Tx go to after previous of x● /findme go to next occurrence of findme

Page 34: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Composition “theory”

● Vim sentence: verb modifier object● Verb: v (visual), c (change), d (delete), y (yank)● Modifiers: i (inside), a (around)● Object (“movement”): w (word), s (sentence), p (paragraph),f (find), ...

● → cip “change inside paragraph”

Page 35: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Compose! (Examples)

● dj delete downwards● di” delete inside “”● 3dw delete three words● c$ change until end of line ● ^ct; change from start of line until just before “;”● ggdG delete the entire file● d/word delete until the next occurrence of “word”

Page 36: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Number prefix

● Most commands can be prefixed with a number:

● 100G Go to line 100

Page 37: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Command repetition

● . Repeat last command

Page 38: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Insert Mode - Demo

● Type to insert text

● i Go to insert mode

● ESC Back to normal mode

Page 39: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Ways to get into insert mode

● a append after cursor● A append at end of line● o new line below cursor● O new line above cursor

Page 40: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Copy / Paste

● y “yank” (copy)● p paste

● No Ctrl-V / Ctrl-C! However, copy paste behaviour depends on terminal.GNOME: Ctrl-Shift-c, Ctrl-Shift-v

● Can use middle mouse click!

Page 41: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Yanking

● yy yank line● yiw yank inside word

Page 42: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Visual mode - Demo

● v visual mode (character selection)● V visual mode (line selection)

Page 43: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Sync system clipboard with Vim

● Put this in ~/.vimrc:

● set clipboard=unnamedplus

Page 44: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Search and replace

● The command can be used for arbitrary complex edits. Example: Search / Replace

● :s/before/after/g

replace “before” with “after” on this line

● :%s/before/after/g

replace “before” with “after” on every line

Page 45: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Buffers - Demo

● A buffer holds a file in memory● Editing a file creates buffer● Accidentally opened a new file and you didn’t save the old

one? No problem!

● :ls, :buffers list buffers

● :b[uffer] [n] open n-th buffer

Many more options/commands available!

Page 46: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Tabs - Demo

● :tabnew Open new tab

● gt, Ctrl-PageUp Next tab

● gT, Ctrl-PageDown Previous tab

Useful key mapping:

nnoremap gn :tabnew<CR>

Page 47: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Windows - Demo

● Ctrl-w {h,j,k,l} Select left/down/up/right

● Ctrl-w {H,J,K,L} Move left/down/up/right

● Ctrl-w v New vertical split

● Ctrl-w s New horizontal split

● Ctrl-w r Rotate layout

● Ctrl-w w Next window

● Can use mouse to select/resize panes!

Page 48: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

I need a terminal! - Demo

● Ctrl-z suspend Vim to the background

● $ fg bring it back

● Really a shell feature, not Vim● Useful at exams ;)

Page 49: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

ctags - Demo

● Tool for easy code navigation● For most languages● Works like the Vim help page!

● Generate tags with command line tool: $ ctags -R● :!ctags -R Generate tags from inside Vim

● Ctrl-] Jump to definition

● Ctrl-t Go back in the stack

Page 50: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Plugins - Demo

● Thousands of plugins are available● Use a plugin manager, e.g. vim-plug [9]

Page 51: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Plugins: NERDTree (file browser)

[4]

Page 52: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Plugins: vim-fugitive (git wrapper)

[5]

Page 53: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Plugins: YouCompleteMe

[6]

Page 54: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Plugins: multiple cursors ([7])

● Demo!

Page 55: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Plugins: Color themes

[8] PaperColor Theme

Page 56: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Your next steps

● Use VIM● Go through vimtutor● Look for cheatsheets, or make your own

Page 57: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,

Sources● [1] Vim 25 presentation, Bram Moolenaar, November 2nd 2016:

https://www.youtube.com/watch?v=ayc_qpB-93o

● [2] Bram Moolenaar: https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Bram_Moolenaar_in_2007.jpg/220px-Bram_Moolenaar_in_2007.jpg

● [3] Richard Stallman: https://i1.wp.com/hipertextual.com/files/2015/03/manifiesto-gnu-richard_stallman.jpg?resize=600%2C400

● [4] NERDTree: https://github.com/scrooloose/nerdtree

● [5] vim-fugitive: https://github.com/tpope/vim-fugitive

● [6] YouCompleteMe: https://github.com/Valloric/YouCompleteMe

● [7] vim-multiple-cursors: https://github.com/terryma/vim-multiple-cursors

● [8] PaperColor Theme: https://github.com/NLKNguyen/papercolor-theme

● [9] vim-plug: https://github.com/junegunn/vim-plug

Page 58: LinuxDays - [project 21] · – System administration: Shell scripts, config files. Where to get it Newest Version: Vim 8.0 Linux: package manager. vim, gvim (might be named differently,