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

Post on 24-Jan-2021

9 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

LinuxDays

Lecturer:

Introduction to Vim

Lukas Tobler HS2017, Version 1.0

A brief history

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

[2] Richard Stallman [3] Bram Moolenaar

What is Vim?

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

Screenshot

Editor learning curves

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”

Why is it good?

● Can navigate large code bases comfortably● Distraction free environment

Why is it good?

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

The test of time

[1] Orignial presentation by Bram Moolenaar: Vim 25

The test of time

[1] Orignial presentation by Bram Moolenaar: Vim 25

What it is not

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

PhpStorm, …● But doesn’t have to!

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

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

If you depend on IDEs

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

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

Modal editing

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

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

Transitions

Insert Visual

Normal

v, VESC

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

c, r, ...

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

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.

Normal Mode - Demo

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

ADM-3A keyboard

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

Arrow keys are the devil - Demo

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

h left

k up

j down

l right

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!

System settings - recommendations

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

$ xset r rate 230 30

Delay in ms Repeat speed in Hz

Undo / Redo

● u undo● Ctrl-r redo

Movement: Words

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

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.

Actions: Deleting stuff

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

Actions: Changing stuff

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

Examples - Demo

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

Movement

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

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

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”

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”

Number prefix

● Most commands can be prefixed with a number:

● 100G Go to line 100

Command repetition

● . Repeat last command

Insert Mode - Demo

● Type to insert text

● i Go to insert mode

● ESC Back to normal mode

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

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!

Yanking

● yy yank line● yiw yank inside word

Visual mode - Demo

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

Sync system clipboard with Vim

● Put this in ~/.vimrc:

● set clipboard=unnamedplus

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

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!

Tabs - Demo

● :tabnew Open new tab

● gt, Ctrl-PageUp Next tab

● gT, Ctrl-PageDown Previous tab

Useful key mapping:

nnoremap gn :tabnew<CR>

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!

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 ;)

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

Plugins - Demo

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

Plugins: NERDTree (file browser)

[4]

Plugins: vim-fugitive (git wrapper)

[5]

Plugins: YouCompleteMe

[6]

Plugins: multiple cursors ([7])

● Demo!

Plugins: Color themes

[8] PaperColor Theme

Your next steps

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

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

top related