Skip to content

Latest commit

 

History

History
106 lines (85 loc) · 5.84 KB

102_vim.md

File metadata and controls

106 lines (85 loc) · 5.84 KB

Text 102 - Vim

Description

"Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple OS X." - vim.org

Vim is a lightweight text editor that reduces repetative and reduntant actions to a minimum, allowing for more focus on writing text or code and spending less time on selecting text or cursor positioning. Vim is also present out of the box on most machines.

Being able to open and edit a text file with Vim is considered a base skill.

You are very likely to run into a program such as man or less that supports a lot of 'vi-like' command, for example, across vim, man and less you can search text by typing /. This alone is plenty of reason to pick up and actively improve your Vim-skills.

Installation

On MacOS: MacOS comes with Vim already installed. On Debian based Linux: Most distros come with Vim already installed (sometimes as vi). Otherwise install it with apt:

sudo apt install vim

Configuration

Vim can be configured from it's dotfile. You can easily find prebuild Vim configurations through Google.

Usage

Modes

Once you launched Vim, you will notice that you cannot start typing in text like you usualy would in another text editor. This is because Vim starts in so called 'normal mode'. While in normal, all the buttons on the keyboard are bound to 'commands': for example, w can be used to skip the cursor one word ahead. By pressing i Vim will go into 'insert mode'. While in insert mode, text can be entered like in any other text editor. By pressing ESC, Vim will return to 'normal mode'.

While in Vim, keep an eye on the bottom left of the screen and see what happens once you enter 'insert mode'.

Saving and exiting

By typing : in normal mode, you can enter a command into Vim. By typing : and w and then RET you can save a file ('w' for write). Entering :q quits Vim. Entering :wq saves and quits.

Cheat Sheet

This is a very incomplete list of just a few usefull Vim commands:

key effect
h/j/k/l move cursor left/down/up/right
w move cursor one word ahead
b move cursor one word back
e move cursor to end of word
i insert mode, before cursor
a insert mode, behind cursor
I insert mode, start of line
A insert mode, end of line
0 insert mode, on line below
) insert mode, on line above
r replace under cursor
cw change word
cb change word backwards
cc change whole line
C change everything after after the cursor
ciw" esc p wrap word in parenthesis
dw delete word
dd delete line
yy copy line
yw copy word
ze minimize function
v visual mode, to select
p paste
gg top of buffer
[b or ]b switch between buffers
G bottom of buffer
/ search (RET to confirm entry)
alt+shift+5 search and replace word
n next search result
N previous search result
: command mode
1 enter a number to repeat a command n times
ESC return to normal mode

Once you've gotten used to these, get yourself a bigger cheat sheet through Google. You'll probably pick up a few new usefull commands right away.

Combos

Take a look at w and b, now take a look at c w and c b. Since you know what e does; what does c e do?

A lot of Vim commands work exactly like in the example. Not everything will be fully 'obvious', but by learning just a few commands and combining them, learning the basics Vim is a lot less hard than it seems. By memorizing a few basic commands, you can easily compose more advanced commands.

Training

You can start vimtutor with the following terminal command to practise Vim:

vimtutor

In Spacemacs you can use SPC h T to start Evil-tutor, which is basically vimtutor.

Vim Adventures is a more playfull way to practise Vim.

Improving

Once you get the hang of basic Vim text editing, try to make Vim really work for you. Once you are done writing something, reflect on what you did, find out what you did not like and proceed to look up how Vim could have fixed your (text based!) problems.

By trying to focus on learning Vim commands that are usefull to you, it should be relatively easy to learn one extra command a day/week, depending on coding frequency.

   Copyright 2018 Opensource Academy

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.