Blog 1

Random Talk on Random Thoughts

Writing Vim's Output to Files

| Comments |

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, then type the following command.

"=sin(1)p

The 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 Object Mix.


(Added on APR 09, 2016)

Fearing that this post will be deleted, I added tianlan’s code below.

:redi @+
:version
:redi END
"+p

Quick reference lists of some easy Vim commands

Choosing a Font for gVim

| Comments |

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 M$ 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.)

fig1
GVim with Inconsolata 12pt

Links to Good Posts

| Comments |

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 Le’s 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. Tips 630 in Vim Tips Wiki 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:

1
2
3
4
5
6
7
8
9
10
11
12
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 $\rm \LaTeX$-Suite.


(Added on DEC 13, 2014)

These custom mappings will interfere with those of $\rm \LaTeX$-Suite if one typed :so foo.tex in a buffer in which an HTML file is opened. Now, I have improved my mappings.


New keyboard shortcuts of my GVim

Finally, I decided to use <F3> and <F12> in normal mode for toggling NERDTree and Taglist respectively.

1
2
nnoremap <F3>   :NERDTreeToggle
nnoremap <F12>  :TlistToggle

Vim File Name Modifier

| Comments |

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.

Something About Win* Batch Files

| Comments |

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 M$ 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.

1
2
@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 M$ Win* is the same as $* in *nix.1


  1. How can I output all of a batch file’s arguments? by John Savill in Windows IT Pro. 

Fast Compilation and Execution of Source Code

| Comments |

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 FLOSS programmable editor, you can add whatever plugins and extra functions you like.1 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.

Error of the Font of the Cited URL in Bibliography Using Bib$\rm \LaTeX$ in APA Style

| Comments |

This midnight, I tried to compile my $\rm \LaTeX$ document with a so-called “bibliography”, which contains a URL. The link should be in typewriter font after using the command \urlstyle{tt} under adding the url package to the preamble. I googled for almost 2 hours, but the blog posts suggested that using this method should work. I even tried to copy some minimum working examples from the web to test if they work. They really worked and so I’m puzzled. Ten minutes ago, I just find out what’s going wrong: the url package should never go before the several lines in the preamble that are responsible for adding an APA-style bibliography.

The \ll command in Vim-$\rm \LaTeX$ seems not doing well with Biber. Whenever the AUX file exists, \ll won’t execute biber, causing the resulting PDF file to have no change in the bibliography.

I hope I can use less time to figure out any mistake next time.