Preface
The first time I used vim I could not do anything or moving one step, and getting knowlage of modes makes me could use it as an editor, I mean, at least I could insert something into the file.
The second thing I have learned is about some comman commands in normal mode
where dealing the keyboard input as command to do specific operations, for instance saving change, exiting vim, and searching something. There are many commands in vim and part is used for editing, like deleting in various ways, copying as you wishand so on.
For then, I thought I got it basically(Wow, it’s pretty ridiculus), but it bothered me why people said vim is powerful. I used vim to edit web files like html, js, without considering my typing rate, it is not efficiency. Constract to other IDE, the vim looks so simple without auto-complete, highlighting and other usual functions. So the configuration of vim starts there. Vim has great flexibility and extendibility by its lush plugins.
According to my personal above experience of dancing with vim, this post would introduce the basics in order to help the freshman go into the world of vim.
Content Table
-
Introduce the background of vim and fellowing the trend to commend its power. Yep, this magical tool do worth it.
-
To help you start to use it by figuring out which is the enter door.
-
This section is to make you feel better to use it as an editor with the commands dealing with basic tasks of an editor. Including moving cursor, changing modes, and deleting, copying and pasting contents.
-
Firstly introduce the config file with simple config sentences. To make the vim more situation adjustment is its plugins. I will introduce how to manage the plugins in a usual way.
-
There are many good resources of vim in the Internet and I list some ones I have read meanwhile recommended.
Vim Intro
According to the url I judge this site should be the official website of vim.
Vim is a highly configurable text editor for efficiently creating and changing any kind of text. There is a noun vi looking similar as vim, which is a built-in editor of all Unix-like systems. And vim could be thought of an upgraded version of it, which means vim is more powerfu and could be support in all Unix-like systems by default. You could use it nearly in any system.
In the age of rapid change, vim still has a large proportion users as a tool with some history. This also could proof something.
Vim Modes
As the above said, vim could be running in all Unix-like system, so just open your terminal, and type
$ vim filename
and now you should have entered vim environment of the filename file, and if filename doesn’t exist this will touch filename first. Don’t be in a hurry to type anything out, as you wouldn’t see any character you entered. When you are in vim you just in one specific state of vim, and you could switch current state anytime. We called the state as mode. Vim has a conception of modes to be responsible for different things. By default, vim is on normal mode
when you first start it.
There are three modes totally:
- normal mode: also known as command mode. All keystrokes are interpreted as commands
- insert mode: most keystrokes are inserted as text(leaving out those with modifier keys)
- visual mode: helps to visually select some text, may been seen as a submode of the command mode. And this post wouldn’t talk about it.
And normal mode
and insert mode
should be cared about first for a novice. The insert mode
of vim deal with the keyboard input as normal to edit the file content.
If you want to enter the insert mode
from the normal mode
(belonged to command mode), you could type i, a, I or A. And they aren’t just simple eplacements, they are slightly difference.
- i: switch to insert mode before the current position
- a: switch to insert mode after the current postion
- I: switch to insert mode and jump to the first non-black character in the current line
- A: switch to insert mode and jump to the last character of the current line
Actually there are other ways to enter insert mode
. You could find nearly everything of vim in the help-file which is included in the vim progress. And you could read it by input
:help
in the normal mode
. The colon is the start sign for command line, and it is necessary to type.
And if you want reversely switch the modes you could enter Esc.
Vim Common Commands
This part is the real meat for the vim dinner. And command has two types, one is just like the above of help, you input commands in the bottom which starts with a colon and it will executed when you enter the return key. Another is invisible style command, the keyboard entry will executed when it could be explained as a command, like the following part Copy and Paste, that uses this style command.
But whichever style command should be executed in normal model
, make sure you’re in the current mode. And this is a comman error that a noviciate makes.
Working with file
If you read this post in order you should be able to text your content in the vim for now, and the second thing you should master is to save and quit the file.
- vim filename: open your file in vim
- :w: write your changes to the file
- :q!: get out of vim(q), and without saving changes(!)
- :wq: write your changes and exit vim
- :saveas ~/some/path/: save your file to that location
- ZZ: this is the invisible style command and work same as :wq
Movement in the file
Basic motions
- j: move down one line
- k: move up one line
- h: move left one character
-
l: move right one character
And the direction keys has the same function.
Moving within the line
These commands are working on the line you’re on.
- 0: move to the begining of the line
- $: move to the end of the line
- ^: move to the first non-blank character of the line
- t": jump to the right before the next quotes
- f": jump and load on the next quotes
Moving by word
- w: move forward one word
- b: move back one word
-
e: move to the end of the word
When you use uppercase you ignore some delimiters within a string that may break it into two words.
- W: move forward one big word
- B: move back one big word
Moving by sentence or paragraph
- ): move forward one sentence
- (: move back one sentence
- }: move forware one paragraph
- {: move back one paragraph
Moving within the screen
- H: move to the top of the screen
- M: move to the middle of the screen
- L: move to the bottom of the screen
- gg: move to the top of the file
- G: move to the bottom of the file
Jumping back and forth
While you’re in normal mode
it’s possible to jump back and forth between two places, which is pretty handy.
- Ctrl+i: jump to your previous navigation location
- Ctrl+o: jump back to where you were
Searching text
Searching by string
One of most basic and powerful ways to search something in vim is to type /, this will take you in the bottom of the window and enter what you are looking for and press Enter. If there are some finding results, these text will highlight, and you can use n to move to the next result location, N to go to the previous one.
- *: search for other instances of the word under your cursor
Jumping to certain characters
In vim, you can search for and jump to specific characters from anywhere. If you editing HTML, you can always jump to <
character to be at the end of the sentence.
- f+character: for exampel f<, will jump forward and land on the
<
character - t+character: will jump forward and land right before the character
Deleting text
Basic deletion options
- x: delete the character under the cursor
- X: delete the character before the cursor
- dd: delete the current line
- D: delete to the end of the line
- J: join the current line with the next one(delete what’s between
Copy and Paste
Copying text
- y: yank(copy) whatever is selected
- yy: yank the current line
Cutting the text
Cutting text is the same with the deleting text. As actually you just pull the deleted text into a buffer and prepare it to be pasted.
Pasting text
- p: paste the copied(or deleted) text after the current cursor postion
-
P: paste the copied(or deleted) text before the current cursor positon
There is a trick to implement switching two lines ddp
Redo and Undo
You can’t have a text editor without undo and redo~
- u: undo your last action
- Ctrl+r: redo the last action, and it could undo the undo action
There is a powerful command in vim, the period . can let you repeat whatever you just did. Take a try~
Vim Configuration
As people really have different works, the target of vim is various, you could search well-directed configuration in the Internet. This section only introduces the most basic.
The configuration file is ~/.vimrc where you could change its content to implement the customization. And this file located in ~ directory will only determine the vim behavior for the current operating system user.
If you are a newbie for vim, don’t copy a lengthy and complex config from the network into your own config file. Trust me, it won’t be good for you. What is good for you, start with a basic setup but not zero, and nearly every line you know what it means.
" enable syntax highlighting
syntax enable
" show line numbers
set number
" set tabs to have 4 spaces
set ts=4
" indent when moving to the next line while writing code
set autoindent
" expand tabs into spaces
set expandtab
" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4
" show a visual line under the cursor's current line
set cursorline
" show the matching part of the pair for [] {} and ()
set showmatch
" enable all Python syntax highlighting features
let python_highlight_all = 1
" map jk to <ESC>
inoremap jk <ESC>
This is one way to configure with some specific instruction like inoremap in whcih you can define you own command by mapping them to vim original command, and set keyword to set up something. So you could type jk instead of Esc. People have set this because it is “a little far” to reach the Esc when you are typing in the main area of keybord and it influences efficiency.
But this way only could do “limited” things. Some people use vim more than an text editor but an IDE. Some complicated features could implemented by vim plugins.
Vim has ample plugins, and there are ways to manage these plugins. The following two are the main way to take vim plugins management. And most vim plugins in github have the instructions to help you install their with following methods.
- Vundle: short for VIM Bundle. It is also a vim plugin itself which provide a clean easy way to manage other plugins in an elegent way.
- Pathogen
This part are just a few things to get you started, actually I’m still a fresher, you can spend a lifetime optimizing your vim configuration file.
Vimrc Resources
- A Good Vimrc is a detailed overview and opinionated guide to configure vim.
- This repository’s folder with Vimrc files has examples configurations that are well commented and easy to learn from.
- 5 Essential vim plugins