2013-12-11

Choosing a Font for Vim

Unclear difference in appearance between 1 with l, 0 with O can be a source of inconvenience for computer users. This summer, I've found this site informative, but I quickly forgot it.
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
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 whole ctags58 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

Typing :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

  1. Type :old for a list of recently opened files (some may not appear)
  2. 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).

Something about Win* batch files

Get drive letter

If you run portable devices from a USB stick, sometimes you need to refer to other files that is in the same USB drive. Their relative position is stable, but the drive letter is not. Thus, referring to those files by absolute path doesn't work, unless you do the tedious modifications every time.
Method 1: set var=%cd:~0,3%
Method 2: set var=%~d0 (preferred) Use variable: %var%

Alias on Win*

If you're familiar with *nix and use M$-DOS, you may unconsciously type the ls command in DOS; or if you have a portable version of a software and you need to launch it by command, you will discover the difference between the name of the programs---one has portable at the end while the other one doesn't.
Copy and paste the following lines of code into any text editor.
@ECHO OFF
[command] %*
Save the whole file as [alias].bat and put the file under any folder found on PATH environment variable.
Run [alias] in the command prompt to see the effect.
Note: %* in Win* is the same as $* in *nix. [1]

Reference:
[1]: How can I output all of a batch file's arguments?

Using Vim Keystrokes in Web Browser

Vim fans will be delighted and amazed for knowing this the first time: Vimperator is the plugin for Vim-like behaviour on Firefox. It's the original one and it focuses on usability. Following the hints with f is great! Here're some useful keystrokes:
  • To open a link in a new tab, type F and follow the hints
  • To move the [num]-th tab, type [num]gt (applicable to Vim also)
  • To go to another page, type o
  • To go to another page with similar URL, type O
  • To open another tab, type t
  • To open another tab with similar URL, type T
  • To open another window, type w
  • To open another window with similar URL, type W
  • To copy the URL of the current page (including "#*"), type y
  • To copy the URL of the targeted link, type ;y
  • To copy the current selection, type Y
  • To copy the labeling text of a link, type ;Y
  • To disable Vimperator for one instance, type i
  • To toogle Vimperator, type <S-Esc>
  • To move the last edited text field, type gi
  • To open Vim for editing input in a textfield/textarea, type <C-i> in insert mode
  • To exit the insert mode of a textfield/textarea, the key is the same as the one you use for Vim.
  • To enter the insert mode of a textfield/textarea from the normal mode, either follow the suitable hint or use c to re-enter there (may fail and enter into the caret mode).
  • To search the history, type :hist [anything]
  • In a pop-up browsing history list, press <CR> and ;o to follow a hint. You can type more to filter the suitable one. Typing a capital O instead of a small letter o gives you :open [wanted_link]
More keystrokes from here, and a cheat sheet there.

Another useful plugin

Google/Yandex search link fix enables true copying of the link's URL in Google's search results.

Fast Compilation and Execution of Source Code

Possible software: Vim.

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?
  1. Diminished role of mouse and more use of keyboard due to modes
  2. Syntax highlighting and file type detection
  3. and more from my previous blog post

How? (Doesn't need c.vim)

  1. Set up makefile (simple tutorial about makefile)
  2. Run :make
  3. Run :!./%< on *nix (or simply :!%< on M$ Win*) [1]
Notes:
  1. set makeprg=mingw32-make in vimrc for MinGW make (more details about makeprg)
  2. % stands for the current file, and < stands for the file name without extension.
Advantages:
  • 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.
2 quotes from Sharpe (I don't remember the exact wordings):
"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 hello
Note: 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)
Go for FLOSS! Go for freedom!

Actual process

Original settings for Ado* Read*: let g: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:
  1. 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.)
  2. Set g:Tex_ViewRule_pdf='SumatraPDF'
  3. Disable g:Tex_ViewRuleComplete_dvi in _vimrc