Configuring vim

apt-get install vim

vim is called The God of Editorsopen in new window. You will use vim for coding in all PAs and Labs, as well as editing other files. If you prefer to emacs, you can download and install emacs from network mirrors.

Learning vim

You are going to be asked to modify a file using vim. For most of you, this is the first time to use vim. The operations in vim are quite different from other editors you have ever used. To learn vim, you need a tutorial. There are two ways to get tutorials:

  • Issue the vimtutor command in terminal. This will launch a tutorial for vim. This way is recommended, since you can read the tutorial and practice at the same time.
  • Search the Internet with keyword "vim 教程", and you will find a lot of tutorials about vim. Choose some of them to read, meanwhile you can practice with the a temporary file by
vim test

PRACTICE IS VERY IMPORTANT. You can not learn anything by only reading the tutorials.

Why don't you teach me how to use GNU/Linux in class?

You may be thinking: This is my first time with GNU/Linux, why don't you show me how to use it in class?

This is because manuals are not meant to be taught, they are meant to be read and try. You don't learn about tools by hearsay, you learn by doing. What you should learn in a college classroom is knowledge. Then you need to think about the principles and ideas behind that knowledge, and be able to apply those principles and ideas to new areas.

We have designed these experiments so that you will realize that you are capable of learning new tools on your own by watching tutorials; as well as, in the future, being exposed to new things. You should not wait for someone else to tell you something new in the future, but should take the initiative to find a tutorial yourself.

Some games operated with vim

Here are some games to help you master some basic operations in vim. Have fun!

The power of vim

You may never consider what can be done in such a "BAD" editor. Let's see two examples.

The first example is to generate the following file:

1
2
3
.....
98
99
100

This file contains 100 lines, and each line contains a number. What will you do? In vim, this is a piece of cake. First change vim into normal state (when vim is just opened, it is in normal state), then press the following keys sequentially:

i1<ESC>q1yyp<C-a>q98@1

where <ESC> means the ESC key, and <C-a> means "Ctrl + a" here. You only press no more than 15 keys to generate this file. Is it amazing? What about a file with 1000 lines? What you do is just to press one more key:

i1<ESC>q1yyp<C-a>q998@1

The magic behind this example is recording and replaying. You initial the file with the first line. Then record the generation of the second. After that, you replay the generation for 998 times to obtain the file.

The second example is to modify a file. Suppose you have such a file:

aaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccddddddddddddddddddddddddd
eeeeeeeeeeeeeeeeeeeeeeeeefffffffffffffffffffffffff
ggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhh
iiiiiiiiiiiiiiiiiiiiiiiiijjjjjjjjjjjjjjjjjjjjjjjjj

You want to modify it into:

bbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaa
dddddddddddddddddddddddddccccccccccccccccccccccccc
fffffffffffffffffffffffffeeeeeeeeeeeeeeeeeeeeeeeee
hhhhhhhhhhhhhhhhhhhhhhhhhggggggggggggggggggggggggg
jjjjjjjjjjjjjjjjjjjjjjjjjiiiiiiiiiiiiiiiiiiiiiiiii

What will you do? In vim, this is a piece of cake, too. First locate the cursor to first "a" in the first line. And change vim into normal state, then press the following keys sequentially:

<C-v>24l4jd$p

where <C-v> means "Ctrl + v" here. What about a file with 100 such lines? What you do is just to press one more key:

<C-v>24l99jd$p

Although these two examples are artificial, they display the powerful functionality of vim, comparing with other editors you have used.

Enabling syntax highlight

vim provides more improvements comparing with vi. But these improvements are disabled by default. Therefore, you should enable them first.

We take syntax highlight as an example to illustrate how to enable the features of vim. To do this, you should modify the vim configuration file. The file is called .vimrc, and it is located under /etc/vim directory. We first make a copy of it to the home directory by cp command:

cp /etc/vim/vimrc ~/.vimrc

And switch to the home directory if you are not under it yet:

cd ~

If you use ls to list files, you will not see the .vimrc you just copied. This is because a file whose name starts with a . is a hidden file in GNU/Linux. To show hidden files, use ls with -a option:

ls -a

Then open .vimrc using vim:

vim .vimrc

After you learn some basic operations in vim (such as moving, inserting text, deleting text), you can try to modify the .vimrc file as following:

--- before modification
+++ after modification
@@ -17,3 +17,3 @@
 " Vim5 and later versions support syntax highlighting. Uncommenting the next
 " line enables syntax highlighting by default.
-"syntax on
+syntax on

We present the modification with GNU diff formatopen in new window. If you do not understand the diff format, please search the Internet for more information.

Why STFW?

You might think, "I'm asking to save my time.

But this is the age of the Internet, where you can get all kinds of information. For example, standardized information such as diff format is 100% available on the Internet The problem you are having, there is a high probability that someone else has asked for help on the Internet. If, for a problem that you could have found a solution to by typing a few keywords into a search engine, instead of making such a small effort, you first try to find someone else to solve it for you, taking up someone else's valuable time. You will be the loser of the age.

Thus the term STFW (Search The F**king Web), which means, Try to find a solution independently by using a search engine in the right way before asking for help.

The right approach to STFW can increase the probability of finding a solution, including

If you haven't used the above to STFW, please don't complain that you can't find a solution and start asking for help. You should think, "Oh I just used Baidu, I should try Google next". About using Google, you can try to set up IPv6 at school, or set up "Scientific Internet Access", please STFW.

Why not use Baidu?

I'm sure you've all used Baidu to search for non-technical questions, and it's usually easy to find answers. But as questions become more technical, Baidu's search results become less and less reliable. If you persist in using Baidu to search for technical questions, you are likely to encounter one of the following situations.

  • You are frustrated by the lack of relevant results.
  • You get results that seem relevant, but it can't solve the problem, and you realize that you've wasted a lot of time while feeling frustrated.
  • You find a solution to the problem, but you don't find the cause analysis, and you don't know the details behind the problem.

You may think "I can solve the problem, I don't need to know the details". But for some problems (e.g., programming problems), knowing the details is equivalent to learning something new So you should learn the details, so you can learn more.

If Google can provide solutions with a higher probability of solving a problem, with an analysis of why, you should have no reason to use Baidu to solve your problems. There should be no reason to use Baidu to search for technical problems. If you still insist on using Baidu, there's only one reason: you don't want to grow.

After you are done, you should save your modification. Exit vim and open the .vimrc file again, you should see the syntax highlight feature is enabled.

Why all this trouble?

After all this trouble, you realize it's just a single character change, so why not just say so?

It's for the sake of "doing as the Romans do": we want you to understand how to use computer thinking to express what we want to do in a concise and accurate way. The diff format is a common way of describing changes to files. In fact, there are a lot of conventions in the computer world. and as you get to know them, you'll realize how the computer world works without realizing it.

Enabling more vim features

Modify the .vimrc file mentioned above as the following:

--- before modification
+++ after modification
@@ -21,3 +21,3 @@
 " If using a dark background within the editing area and syntax highlighting
 " turn on this option as well
-"set background=dark
+set background=dark
@@ -31,5 +31,5 @@
 " Uncomment the following to have Vim load indentation rules and plugins
 " according to the detected filetype.
-"filetype plugin indent on
+filetype plugin indent on
@@ -37,10 +37,10 @@
 " The following are commented out as they cause vim to behave a lot
 " differently from regular Vi. They are highly recommended though.
 "set showcmd            " Show (partial) command in status line.
-"set showmatch          " Show matching brackets.
-"set ignorecase         " Do case insensitive matching
-"set smartcase          " Do smart case matching
-"set incsearch          " Incremental search
+set showmatch          " Show matching brackets.
+set ignorecase         " Do case insensitive matching
+set smartcase          " Do smart case matching
+set incsearch          " Incremental search
 "set autowrite          " Automatically save before commands like :next and :make
-"set hidden             " Hide buffers when they are abandoned
+set hidden             " Hide buffers when they are abandoned
 "set mouse=a            " Enable mouse usage (all modes)

You can append the following content at the end of the .vimrc file to enable more features. Note that contents after a double quotation mark " are comments, and you do not need to include them. Of course, you can inspect every features to determine to enable or not.

setlocal noswapfile
set bufhidden=hide 
colorscheme evening 
set number 
set cursorline 
set ruler 
set shiftwidth=2 
set softtabstop=2 
set tabstop=2 
set nobackup 
set autochdir 
set backupcopy=yes 
set hlsearch 
set noerrorbells 
set novisualbell 
set t_vb= 
set matchtime=2 
set magic 
set smartindent 
set backspace=indent,eol,start 
set cmdheight=1 
set laststatus=2 
set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ Ln\ %l,\ Col\ %c/%L%) 
set foldenable 
set foldmethod=syntax 
set foldcolumn=0 
setlocal foldlevel=1 
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR> 

An editor that improves development efficiency

You learned to use Visual Studio in your programming class, and you probably thought that's how programmers write code. Not really, programmers look for ways to improve efficiency.

::::It's not that the GUI is bad, it's that you're just writing code the way you'd write it in Notepad.

So you need to change, and try some tools that can help you be more productive.

In GNU/Linux, learning the basics of vim can be a big improvement over the notepad approach. There are also various plug-ins to enhance vim, such as ctags for variable jumping in code, etc. You can take some time to configure vim, please STFW. In short, the "God of Editors" is not for nothing.