Skip to content

Instantly share code, notes, and snippets.

@BrunaNayara
Last active June 26, 2023 13:31
Show Gist options
  • Save BrunaNayara/cdf7cf97ee0bd64e175d2121ff343f7a to your computer and use it in GitHub Desktop.
Save BrunaNayara/cdf7cf97ee0bd64e175d2121ff343f7a to your computer and use it in GitHub Desktop.
Vim presentation

How to vim

Basics and not so basics for future vimers

Why vim?

  • Not the easiest
  • But always there
  • Good for small things
  • Easy setup

First things first

You can exit by typing :q!

Modes

Vim has different modes. We'll cover some of them here

  • Normal
  • Insert
  • Visual
  • Command

Normal mode

It's the default mode, and where you can move around. You can use these to move the cursor:

  • arrow keys (basic)
  • hjkl as arrow keys <v^> (very advanced)
  • w goes to next word
  • e goes to end of word
  • b goes back to beggining of words

Normal mode

Now we can combine number and letters (omg why? just because we can)

  • 3j goes down 3 lines

  • 5k goes up 5 lines

  • same goes for h, l, w, e, and b

  • Show :set relativenumber

Normal mode

Now we can combine number and letters (omg why? just because we can)

  • 3j goes down 3 lines

  • 5k goes up 5 lines

  • same goes for h, l, w, e, and b

  • Show :set relativenumber

Normal mode

But maybe you want to move more in a line... That's ok

  • 0 goes to the beggining of the line (0th char)
  • ^ goes to the first non-blank character
  • $ goes to the end of the line

Normal mode

Enough with lines. You wanna move more

  • gg goes to first line
  • G goes to last line

Insert mode

Now that we know how to move, we can start editing right? Okay, alright? I get it.

You can use a bunch of different things to start editing

  • i inserts where the cursor is
  • I inserts in the beginning of line
  • a adds in the next character
  • A Adds in the end of line
  • o opens a new line below
  • O opens a new line above

Visual mode

We can visually select things

  • v character visual mode
  • V line visual mode
  • ctrl+v block visual mode

More normal mode stuff (Operators)

  • yy copy line (yank)
  • y copy visually selected
  • p paste after
  • P paste before
  • dd delete line
  • D delete forward

While we're talking about copying and pasting, we can talk about pasting from the clipboard (not from vim) You should enter insert mode and use OS paste command (ctrl+v or cmd+v)

More normal mode stuff (Operators)

  • >> shift right
  • << shift left
  • ~ swap case
  • gu make lowercase
  • gU make uppercase

Combining things

You can combine things to make your life a bit easier

  • dw delete word
  • yw copy word
  • cw cange word
  • ci( change inside (

Searching for patterns

I need to find the occurrences of something in a file

  • ?word searches for word
  • n goes to next occurrence
  • N goes to previous occurrence

Alternatively, you can select the word under the cursor

  • # highlights all the occurrences of the word under the cursor

Command mode

  • :q quits vim
  • :w saves file (write)
  • :q! forces quit (without saving)
  • :wq saves and quit
  • :wqa saves and quit all
  • :number goes to line number

Special comment to say you don't need to quit vim to go back to the terminalo You can use ctrl+z as it suspends (doesn't interrupt) terminal processes

Command mode

In this mode, you can setup your preferences

  • :set nu sets number
  • :set relativenumber sets relative numbers

These preferences can be saved in the vimrc file and that's basically it

Tabs

You can open many tabs to work on many files if you want

  • :tabe Opens a new tab for editing
  • :tabe FILEPATH Opens a new tab for editing a file
  • :tabe . Opens the current folder for selecting a file

Tabs

Ok, you have too many tabs now, how do you go to each of them?

  • gt goes to next tab
  • gT goes to previous tab
  • 3gt goes to tab number 3

Splitting screen

Cool, tabs are cool. But noy you want to split your screen, because you always want more... There's a couple of ways to do this. I'm going over only one. Enough is enough right?

  • Ctrl + w to enter window controlling

And combine it with some other commands

  • s split window
  • v split vertically

Now you can make the windows bigger and/or smaller

  • N+ Adds N lines below

  • N- Removes N lines from below

  • N>

Splitting screen

Moving from a place to another

  • Ctrl + w to enter window controlling

And combine again

We need to remember the moving buttons (hjkl)

  • l go to the window in the right
  • h go to the window in the left
  • j go to the window below
  • k go to the window above

Splitting screen

Let's move things around

  • Ctrl + w to enter window controlling

And combine again

We need to remember the moving buttons (hjkl)

  • L move window to the right
  • H move window to the left
  • J move the window below
  • K move the window above

Search and replace

Searching and replacing nedds a bit of knowledge of REGEX

**:%s/searching_pattern/replacing_word/options

Where to search and replace

  • % means the whole file will be searched
  • 1,10 means lines 1 to 10 (separate them with ,)
  • visually select the portion you need and type :s

Searching pattern

Here you can use your REGEX knowledge (out of scope of this)

Useful options

  • g All occurences in line (otherwise only first)
  • c Confirmation before replacing

Registers

If you're not yet, you'd be mad with me. But why is that?

Because you might run into this scenario:

  1. You y copy a line
  2. You want to delete another line to paste the one you just copied
  3. Cool, you're smart and dd delete the line
  4. Then you p paste and you have the line you just dded

INFURIATING

Worry not! You about to become vim-smarter!

Registers

We have a magical thing called REGISTERS It's basically a storage for strings

  • :reg shows you the registers list

When you copy or delete something you change the content of the "default" register And when you paste, that's the register vim uses. But we can change that!

  • " is the character that access a register
  • 0 is the yank yy register

So now we can paste using our new knowledge

  • "0p pastes the content of the yank register
  • "ap pastes the content of the register a

Macros

Repeating things!

We have the mini macro .

  • . repeats the last change

Your last change can be deleting, pasting, whatever

A change starts when you enter insert mode and stops when going back to normal mode

Macros

You can create your own macros. YAY!!

  • qa starts recording a macro in register "a

You choose you register and start having fun

  1. qa starts
  2. Start your changes
  3. Not only insert mode changes
  4. q in normal mode to stop recording

You can see your macro on the :reg

And you can execute your macro with @a (a being the reg you chose)

Don't forget we can always (always? not sure) combine things

  • 10@a executes your macro in :reg a 10 times

Marks

You can mark and get back to it

  • m{a-z} Marks that place and save it in a {a,z}
  • `{a-z} goes to {a,z} mark

Jumps

  • ctrl+o goes to older jump
  • ctrl+i goes to newer jump

Tags

When working on a project you might need to jump into definitions Use ctags to generate them.

  • ctrl+] to jump to the definition
  • :tag {method name} to jump to the definition

Or from terminal

vim -t method-name

Plugins

Plugin managers

  • I use vundle to manage

Useful plugins

  • git gutter
  • black

References

vim game: https://vim-adventures.com/

vim movement cheat sheet: https://vim.rtorr.com/

Further info

This presentation is best viewed using Presenting vim and figlet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment