blogger.vim
is what I've found. I've tried this plugin but it doesn't work for my computer. Without Vim, writing code is painful for me. Read the authors' last Blogger post, which was, ironically, about his switch to WordPress, I followed his footstep.
2014-01-11
Migration to WordPress
2013-12-30
C++ Hello World Using clang_complete and Vim
The authors of the above posts are likely to work in the IT sector. For new comers of programming, they may like to start from zero. On which platform can they start from? I'd recommend popular *nix distributions (e.g. Debian-based/Fedora-based ones) due to their package managing tools like APT or YUM. For a M$ Vis* Stu* like code completion on Vim, clang_complete is the right answer, but it is often hard to install on M$ Win* [1], but the installation is incredibly easy on *nix. [2]
Many writers of posts on clang_complete installation stopped writing further after completing the whole installation and configuration of the plugin, and I haven't seen any post that uses "hello world" as an example. This is alright since many users of Vim are experienced programmers who would like to demonstrate some advanced features of the editor with the additional plugin (e.g. jumping between ctags), and a simple hello.cpp can't be used for that. Moreover, many teachers would teach programming on M$ Win*, and even if a *nix desktop is used, the work is most likely done using an IDE on GUI (Emacs is a text editor and IDE that runs on command line.) ... The concept of modal editing may be too hard for newbies to accept it, and you need to get several commands right before entering any text into a buffer. The steep learning curve might frighten learners away from Vim... Some computer users believe in the myth that Vim and Emacs are too old and nobody use them.
Anyways, the mixed use of both the built-in omni-complete function of Vim (invoked by
<C-n>
or <C-p>
)
and the popup menu of clang_complete (invoked by <C-x><C-u>
)to
write a "hello world" program
is a productive way of doing the job. However, due to the limitations of
human mind, we can remember very few unfamiliar things at a time. This
is
not the case in storage devices. They can capture every word that come
out of our mind, though many physical means of storage of imformation
are subject to aging, especially those in the past... Luckily,
information technology allows words, and even fonts, to be replicated
efficiently and almost identically. This has brought the human race a
variety of new subjects, such as web *ng, and has extended our old
knowledge into many fields, such as typography... In addition, the
advancement of technology enables us to capture visible images and copy
it rapidly, and I've recorded my experience of "hello world" using
clang_complete with Vim on a series of screenshots.In addition to adding Wu's custom vimrc configuration for clang_complete [3], I've also installed vim-snipmate and vim-snippets for more auto-complete mappings.
Before actually beginning the steps for writing a "hello world", we should know that we won't learn anything from successes. It's failures that help us reflect on the mistakes in our understand of theories. Therefore, I'll deliberately find something wrong, and elaborate on that using some ideas. In other words, that's "to blow water" in Cantonese.
1. Comparison between clang_complete and the editor's built-in completion in typing #include<iostream>
1.1 The editor's built-in completion
The following "hello world" procedure contains some wrong steps.
- Type
inc<Tab>
. - Vim-snippet completes the code, with
stdio.h
as default. The file name of the included file is selected so that you can input something else. - Since
cout
is used later, we need to#include<iostream>
. First typeio
to override the default input. - Type
<C-n>
or<C-p>
to get a completion list. - Select
iostream
(without 's'). - Continue typing the whole program, and compile it.
- The source code fails!
#include<iostream.h>
. Thinking about the auto-completion function again, one will notice that the correct step is to press <Esc>
after step 1, and use ci<
to clear the code inside the <>
block, that's a good try, but I personally think that the steps can be more beautiful.
1.2 clang_complete's solution
This is just a part of correct steps. I leave the remaining parts of codes of this simple program into later sections.
This time, after typing
#
at the beginning of the line, press <C-x><C-u>
to use the automatic completion list, and choose the # include
option with the $`header'
surrounded by a <>
block, not a double quote. Use <Alt-Tab>
to select the inner <>
block and type io<C-n>
to use the built-in (not plugin) function to complete the code. (The <Alt-Tab>
keystroke doesn't work in GUI, since it means changing the window on focus.)Note: In this method,
stdio.h
needs to be included. Otherwise, the built-in insert completion popup menu containing iostream
will not be shown.
2. Completion of using namespace std;
- Without clang_complete: Directly type in everything, or use
<C-n>
for word-by-word completion. - With clang_complete: The completion menu can complete the whole line.
![]() |
Without clang_complete: word-by-word completion |
![]() |
With clang_complete: completion of the whole line |
The remaining parts of this post doesn't make use of the clang_complete plugin.
3. The main method
Just typingmain<Tab>
will do. That's from vim-snippets.
4. Greeting the world!
If you typecout<Tab>
, you'll get std::cout<< <<std::endl;
, with the cursor located in between the two whitespace characters in the middle. Using a regular expression to delete the std::
in front of cout
and endl
.
5. Getting the indentation correct
Modifying the c.snippet file maybe a bit troublesome, and may bring strange consequences to the plugin. If you don't have time to study the plugin, you may not like to bother it. Manually decreasing the indentation of the linereturn 0;
each time is not likely to be an elegant solution for Vim Lovers. The
actual way to fix the problem (using the command gg=G
in the normal mode) is on p. 73 of the book Vi iMproved—Vim.
[4]
6. Running the program
For some newbies, it might be too much for them to digest if I discuss more on the creation of a sample makefile, which automatically executes more complicated compiling commands with a simplemake
command, searching "makefile tutorial" on the web will do.
If you don't like the clang compiler, just substitute clang++
with g++
.According to Henri Poincaré, a French mathematician, things are beautiful if they are simple. Repetition of facts build up our understanding. I end this post with a short and simple command that can be repeatedly used to compile .cpp source files with different file names. For more details about that, refer to my earlier post titled Fast Compilation and Execution of Source Code.

Reference:
[1]: http://blog.chinaunix.net/uid-53564-id-3558537.html
[2]: http://changyy.pixnet.net/blog/post/31706673-%5Bvim%5D-using-clang_complete-plugin-@-ubuntu-12.04
[3]: http://blog.wuwon.id.au/2011/10/vim-plugin-for-navigating-c-with.html
[4]: ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
2013-12-13
Writing Vim's Output to Files
Changing environment variables in vimrc
Refer to Vim Wiki's page on environment variables.The tips uses paths in *nix in the examples. However, for Win*, some inexperienced users don't know whether slashes or backslashes should be used. In fact, backslashes should be used, and the string should be enclosed by a pair of single quote (I don't know if double quotes work.)
Then things should run. If you're unlucky and receive a runtime error in the next time you start Vim, then it may be due to the missing space characters around the equal sign.
Writing external commands to files
It's easy! (:r ![command]
will do.)
Writing variables to files
Use the=
register. First get into normal mode.
"= [sth_to_eval]<CR> pThe
p
have to be typed before any further changes of contents. Otherwise, it may not work.For details, see Unix & Linux Stack Exchange question 8101.
Writing internal commands to files
A forum post has answered my doubt. I redirect interested readers to here.Quick reference lists of some easy Vim commands
There's "100 Vim commands every programmers should know".Another page: 130+ Essential Vim Commands
2013-12-12
OmniCppComplete Installation for Dummies
Background: OmniCppComplete is a great Vim plugin that has the popup menu like many big IDEs. A Youtube video can show the power of OmniCppComplete.
Download: OmniCppComplete requires ctags.
- OmniCppComplete: GitHub Repository (To avoid mixing up of files from different plugins, use one plugin management plugin like Pathogen or Vundle.)
- Ctags: Official homepage on SourceForge (The .zip file for Win* 2**0 is fine.)
Problem: Reading the OmniCppComplete's documentation only, you won't be able to understand how to use this plugin, so follow some tutorials, and the most viewed one should be on Vim Tips Wiki. Followed its guidance, I got error in typing
std::
in a new .cpp file. Omni completion told me something like "pattern not
match". I spent nearly 2 hours to figure out what's wrong, and finally
managed to fully understand the contents of this blog entry.Now, I'm going to make a Win* version of Vim Tips Wiki's tutorial, using the ideas from Carl.
Solution:
- I suppose OmniCppComplete and ctags are installed properly.
- OmniCppComplete does not mix up with other plugins.
Note: For gVimPortable, do store files under the Data folder, but NOT the $VIMRUNTIME file. Otherwise the settings will be lost after upgrading gVimPortable. - Typing
:h omnicppcomplete
in Vim normal mode gives up the help file for OmniCppComplete. - The folder for ctags is in the PATH environment variable.
Follow this page for setting the PATH variable.C:\ctags58 should be in the PATH variable. - Download the modified libstdc++ headers from GitHub.
- Unzip the folder and place it under "somewhere meaningful to you"
My suggestion:C:\Users\[your-user-name]\.vim
Here you can substitute[your user name]
with your real user name on Win*.
Note: In the dialogue (in case you use GUI), DON'T inputtags
at the end. Otherwise, you'll getC:\Users\[your-user-name]\.vim\tags\tags
- If things go right, you should have
C:\Users\[your-user-name]\.vim\tags
Go to that directory by typingcd .vim\tags
in Win*'s Command Prompt, which can be involked by typingcmd
in the "Start" menu. (For XP, choose "Run" and type "cmd" in the popup dialogue. If you like choosing from the menu, follow the way on the official website.)
At first, a commad prompt should be like this.The current directory is C:\Users\Owner
.Change the current directory to C:\Users\Owner\.vim\tags
.dir
(There're so many files that I use the/w
option to save space.) - Then you should have a huge list of ctags with the file name cpp. Not adding the additional tags for OpenGL, etc, is OK.
- Finally, adding the lines found in the Vim Tips Wiki into your vimrc will do.
Note: If you don't add the additional tags in step 5, then the following lines should NOT be added.set tags+=~/.vim/tags/gl set tags+=~/.vim/tags/sdl set tags+=~/.vim/tags/qt4
![]() |
The folder for different plugins are separated (except the plugin management plugin itself). |
![]() |
Typing :h omnicppcomplete gives you the corresponding help file. |
![]() |
The contents are in the right place, so we may proceed. |
![]() |
Get a list of ctags. |
>ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp .The one you see on Vim Tips Wiki:
>ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp cpp_src
Testing
A picture can illustrate the feature well. Typingstd::
, you'll get a popup list.
Incompatibility with clang_complete
Note that Omnicppcomplete is incompatible with another Vim plugin called clang_complete, due to its "unconditional popup of completion list". To understand this, the following pictures give a good interpretation.In Omnicppcomplete, after typing
std::
, the completion list will pop up.However, in clang_complete, since the information is from the clang compiler, you need to give the syntax in the right position for the compiler so as to let the completion list to show up.
![]() |
Missing #include<iostream> |
![]() |
Still wrong position of the input |
![]() |
Right position |
clang-dev
as well. For Win*, maybe you need to download the source. (I hope there will be a compiled version of the compiler.)Reference:
[1]: http://hi.baidu.com/dt_zhangwei/item/277572044909fc103a53eea5
2013-12-11
Choosing a Font for Vim
Installation of many fonts in some popular Linux distributions can be done in GUI just like Win*, so I attempted to change my default font on Vim, and a picture is worth a thousand words.
The following command from Vim Tips Wikia will do:
:set guifont=[font-name]:h[font-size]
. (Escape the whitespace character with a backslash, i.e. use \
to represent a whitespace.)![]() |
gVim with Inconsolata 12pt |
Links to Good Posts
Taglist installation
Taglist is a popular Vim plugin and it requires ctags. c9s has given a detailed description of the installation of ctags. I've found out that putting the wholectags58
folder under the C
drive and adding it to the PATH
environment variable will do. You don't need to copy ctags.exe
under the C
drive or $VIMRUNTIME
.TheGeekStuff has a web page which described numerous powerful functions of Taglist.
Some vimrc settings
I've learnt something about vim keyboard mappings by reading a post on customized vimrc settings.Using this knowledge, I can do something that I couldn't do last month.Automatic completion of unclosed braces/brackets
Unmatched brackets/braces are the cause of a host of syntax errors in source code. Some novices probably spend hours to find it out, and then say something that their high school teachers will never approve. a Vim Tips contains a simple solution that enable users to get the job done. However, I'm not accustomed to its differences with Vim-$\rm \LaTeX$, so I decided to add the following lines of code into my.vimrc
file:
inoremap ( ()<++><Left><Left><Left><Left><Left> inoremap (<CR> (<CR>)<Esc>O inoremap (( ( inoremap () ()<++><Left><Left><Left><Left><Left> inoremap [ []<++><Left><Left><Left><Left><Left> inoremap [<CR> [<CR>]<Esc>O inoremap [[ [ inoremap [] []<++><Left><Left><Left><Left><Left> inoremap { {}<++><Left><Left><Left><Left><Left> inoremap {<CR> {<CR>}<Esc>O inoremap {{ { inoremap {} {}<++><Left><Left><Left><Left><Left>After the user types
<C-j>
, the cursor will jump to the position of the placeholder <++>
and the whole placeholder will disappear, just like Latex-Suite.
New keyboard shortcuts of my gVim
Finally, I decided to use<F3>
and <F12>
in normal mode for toggling NERDTree and Taglist respectively.
nnoremap <F3> :NERDTreeToggle<CR> nnoremap <F12> :TlistToggle<CR>
Vim File Name Modifier
:h %:p
will give you a list of file name modifiers in cmdline.txt
, and the section below filename-modifiers
is extension-removal
. The list maybe too detailed, especially for users who don't have much time to learn it. Bram Moolenaar, the father of Vim once said that trying to learn all the things in his lecture called 7 Habits of Effective Text Editing (see habit 7). (The video is on Youtube.) Fortunately, there're some examples below the list. I found some useful in the following cases.
Case 1: Executing compiled programs
See my another blog post on programming with Vim for details.!Case 2: Finding old files
- Type
:old
for a list of recently opened files (some may not appear) - If you can find a right file, note its corresponding line number on the list and type
:e #<[num]
to open the file.
Case 3: Searching contents of The User Manual
In the very first section of the user manual, it's said that users should read the user manual "from the beginning to the end like a book". However, you sometimes don't have much time to do so and you need to quickly perform some tasks not covered in the sections of the user manual that you've read, such as compiling programs. Using the command:![compiler] [src-file] ...
and :![command-to-run-prog]
is already good since you don't have to leaving Vim, but you may want to see if Vim has some special features. Searching something like "vim c programming" probably gives users links to an array of GitHub repositories for Vim plugins. Even though you have Pathogen or Vundle, if things are in conflict with each other, you need a lot of time to figure out the culprits and fix the problem. If there's already something written on the official manual, why don't read them first?The official manual has 2 parts, namely the user manual and the reference manual. For the former, the contents are easier to learn, but the parts are often hard to locate with
:h howto
and search commands. For the latter, it's easier to locate with :h [sth]
since you can pass the [command]
that you don't know to [sth]
before reaching a relevant section that is too hard for you to keep reading. Therefore, searching contents in the user manual is needed.Then you have to extract the path of the documentation files, which in under some folders that you don't know. Searching it with a file browser will certainly be ineffective, which using searching commands in the terminal needs time and may not guarantee a success due to the possibility of making syntax errors. A more efficient approach is, assuming that the reference manual is near to the user manual, to first open only page in the reference manual in order to make use of
%
in Vim commands for the extraction of the path of the user manual. Of course you need to check the actual contents inside the directory. (I have NERDTree installed so I use :NERDTree %
for a popup file tree and cd
for a change of current directory.)Thus, with some knowledge in the reference manual (I learn this from
:h %:p
.), one will find out that the modifier needed is :h
. In *nix, the command grep
can find contents in a file; in Win*, I'm too lazy to something new so I use something like Cygwin for grep
.Case 4: More about extraction of path of the current file
In the previous case,%
and its modifiers enables you to refer to the path of the file, but you don't see its full path. In order to do so, type :echo expand("%")
. Conclusion: Stop repeatedly typing the same long thing
Leave machines to machines since they can efficiently replicate long things. That's another tip from Bram Moolenaar (Tip 2).Fast Compilation and Execution of Source Code
Why?
In a heavy IDE, you need to click a button or some keyboard shortcuts. Unless you frequently use the keystrokes, you'll likely forget them since they are usually non-transferable IT skills.It's different in Vim. Since it's an F/OSS programmable editor, you can add whatever plugins and extra functions you like. You can adjust you .vimrc file so that the editing environment won't have too many undesirable changes.
Vim is lightweight. If your computer collapses (or even gives you the notorious BoD before RiP) due to loading IDE, Vim is your choice since it may still work with some good design and functions.
What are the good things?
- Diminished role of mouse and more use of keyboard due to modes
- Syntax highlighting and file type detection and more from my previous blog post
How? (Doesn't need c.vim)
- Set up
makefile
(simple tutorial about makefile) - Run
:make
- Run
:!./%<
on *nix (or simply:!%<
on M$ Win*) [1]
set makeprg=mingw32-make
in vimrc for MinGW make (more details aboutmakeprg
)%
stands for the current file, and<
stands for the file name without extension.
- In the whole process, you don't have to leave Vim and keyboard.
- The commands are the same whatever your files are (already determined by
makefile
), so you don't waste time to think and type in the correct name.
"The battle is not in the hands of who give the most shots. It should be those who shot the best."
"A good soldier can fire 3-4 rounds a minute."
My sample
makefile
:hello : hello.c gcc -o hello hello.c clean: rm -f helloNote: Insert tabs for indentation, not whitespaces.
If the compiled program is
hello.exe
, perhaps the extension name is also needed for rm
. (I have never tested it.)
With c.vim
Follow the menu and you'll be also good.Reference:
[1]: How do I run a C program from Vim?
Vim-$\rm \LaTeX$'s \lv command with Sumatra PDF
Why do I switch my PDF viewer?
- Ado* Read* is proprietary. (EULA)
- Sumatra PDF is free. (GPLv3)
Actual process
Original settings for Ado* Read*: letg:Tex_ViewRuleComplete_dvi='C:/.../AcroRd32'
in _vimrc
(No extension name)Problem: How to change Vim-$\rm \LaTeX$'s setting of
\lv
?Trial 1: Set
g:Tex_ViewRuleComplete_dvi='C:/.../SumatraPDF'
in _vimrc
(No effect)Trial 2: Set
g:Tex_ViewRule_dvi='C:/.../SumatraPDF'
and disable g:Tex_ViewRuleComplete_dvi
in _vimrc
(No effect)Trial 3: Set
g:Tex_ViewRule_pdf='C:/.../SumatraPDF'
and keep g:Tex_ViewRuleComplete_dvi='C:/.../AcroRd32'
in _vimrc
(Ado* Read* launched instead)...
Trial n: Set
g:Tex_ViewRule_pdf='C:/.../SumatraPDF'
and disable g:Tex_ViewRuleComplete_dvi='C:/.../AcroRd32'
in _vimrc
(No effect)Solution:
- Change
PATH
environment variable. (You can change it for vim runtime and keep the original one in other programs. See my newer post for details.) - Set
g:Tex_ViewRule_pdf='SumatraPDF'
- Disable
g:Tex_ViewRuleComplete_dvi
in_vimrc
2013-11-06
This Weekend's Work on Vim
2. Do some custom insert mode keyboard mapping. (automatical completion of brackets)
2013-10-18
A Bug in Vim-$\rm \LaTeX$
This shows to good things of free and open source software:
- Find bugs quickly through effective communication between users and developers
- Fix bugs quickly through hands-on configuration of source code
- Everyone can tune the software to adapt to his/her needs.
- Everyone can share and contribute their changes. and more ...
2013-08-16
Why Vim?
Programmers
The video below is a talk given by Bram Moolenaar, creator of Vim. The handout for this talk is found on his website. However, he assumed that the audience had some basic knowledge on movements in Vim. For Chinese users, the slides below may help you. Otherwise, please go down to here-
Quick jumping of text: e.g.
5w
,2(
-
Text objects: e.g.
da<
,ci"(
,dci{(
- Vim-Macro: e.g.
5w
,2(
-
Folds: seeing structures and moving quickly
- Vim Folding Tips @ Hacker's Blog
- Make your own Vim folds (Chinese)
Writers
With many keyboard shortcuts, Vim enables you to move quickly between words, sentences and paragraphs, and thus focus on writing.See Randall Wood's Vim for Writers blog entry as well as his notes.
Vim Advocacy
Vim Game
Vim E-Book
- Vi IMproved—Vim by Steve Oualline
APA Citing
Typing in the APA reference list entries are tedious. Therefore, use the biblatex-apa
package.
Why biblatex
instead of $\text{Bib} \rm \TeX$?
Apart from the support of @online
tag, see this blog post on the comparison between the apacite
and biblatex-apa
packages.
For writing scientific reports, refer to $\rm \LaTeX$ Style and Bib$\rm \TeX$ Bibliography Formats for Biologists: $\rm \TeX$ and $\rm \LaTeX$ Resources
Edit $\rm \LaTeX{}$ equations on Vim
With the Vim-$\rm \LaTeX$ plugin, it's possible to edit $\rm \LaTeX$ equations with Vim quickly without making syntax errors. (e.g. undefined control sequence caused by mismatched braces '{}')
For the sample files, please click the above "Youtube" button. Why use Vim? Please refer to my post on Vim.
For a quick startup guide, see the official tutorial. If you've learnt those keyboard mappings, here's a quick review of some commands. For detailed list of all keyboard shortcuts and customizations (e.g. custom compile rule: use pdflatex
instead of latex
and dvipdfm
), you can refer to the user manual.
There're many blog entries about how to install $\rm \LaTeX$ on Vim/GVim on UNIX/Linux and M$. Searching "vim latex" gives you millions of search results. Summarizing the articles, the three major components are:
- Editor
- Vim (found on almost every distribution of *nix)
- GVim (on GUI of any platform)
- $\rm \LaTeX$
- $\rm \TeX \text{Live}$ (found in package manager on *nix)
- $\text{Mik}\rm \TeX$ (have a $\rm \LaTeX$ package manager)
- Vim plugin for writing $\rm \LaTeX$: Latex-Suite
For *nix, you may find some GUI tools for installing packages. (especially in some popular distributions)
You can also type the install commands for these packages on the terminal. For example:
-
Ubuntu
# Also include `vim-gnome' if you'd like to install GVim. $ sudo apt get install vim-gnome vim-latexsuite texlive
-
Fedora
# Also include `vim-X11' if you'd like to install GVim. [root@localhost]# yum install vim-latex latex
For M$, the installation procedures for GVim (portable), Vim-$\rm \LaTeX$ and $\text{Mik}\rm \TeX$ (portable) are as followed:
- Go to the official websites of GVim and $\text{Mik}\rm \TeX$ (or equivalent packages) and download the packages.
- Put $\text{Mik}\rm \TeX$ portable and GVim portable to any folder you like
- e.g.
C:\Temp
- DON'T put them under
C:\Program Files
- e.g.
- Download Vim-$\rm \LaTeX$ from SourceForge
- Follow the steps here