summaryrefslogtreecommitdiff
path: root/_posts/2012-05-18-using-vim.md
diff options
context:
space:
mode:
authorAndreas Maunz <andreas@maunz.de>2012-05-18 09:50:29 +0200
committerAndreas Maunz <andreas@maunz.de>2012-05-18 09:50:29 +0200
commit3920cb52864e8811c8ef94c9c66b3fc9a7916304 (patch)
treeee32e919b62f7c4265deb9c2591e1cf4c28368d3 /_posts/2012-05-18-using-vim.md
parentde18df01f795d3760e0a5ca4e6fbaf8a49c204ec (diff)
git-vim
Diffstat (limited to '_posts/2012-05-18-using-vim.md')
-rw-r--r--_posts/2012-05-18-using-vim.md282
1 files changed, 282 insertions, 0 deletions
diff --git a/_posts/2012-05-18-using-vim.md b/_posts/2012-05-18-using-vim.md
new file mode 100644
index 0000000..fcacafc
--- /dev/null
+++ b/_posts/2012-05-18-using-vim.md
@@ -0,0 +1,282 @@
+---
+layout: post
+title: "Using Vim on Linux"
+description: "Using Vim on Linux"
+category: general
+tags: [vim, latex]
+---
+{% include JB/setup %}
+
+
+Installing vim
+==============
+
+ sudo apt-get install vim
+
+Add the following to your `~/.bashrc`:
+
+ export EDITOR=vim
+
+and do
+
+ source ~/.bashrc
+
+Now you can start the editor by entering `vim` at the command line.
+
+
+
+
+Basic setup
+===========
+
+Put this in your `~/.vimrc`:
+
+ " enable syntax highlighting
+ syntax on
+
+ " use spaces instead of tabs
+ set tabstop=2
+ set expandtab
+ set shiftwidth=2
+ set softtabstop=2
+
+ " always show ^M in DOS files
+ set fileformats=unix
+
+ " my terminal is white on black
+ set background=dark
+ highlight Comment ctermbg=blue
+ highlight Comment ctermfg=white
+
+ " always show line and col number and the current command, set title
+ set ruler
+ set showcmd
+ set title titlestring=vim\ %f
+
+ " caseinsensitive incremental search
+ set ignorecase
+ set incsearch
+
+ " Show matching brackets
+ set showmatch
+
+ " disable any autoindenting which could mess with your mouse pastes (and your head)
+ " (not useing 'set paste' here to keep fancy stuff like tab completion working)
+ set nocindent
+ set nosmartindent
+ set noautoindent
+ set indentexpr=
+ filetype indent on
+
+Your user experience should now be much better.
+
+
+
+
+Using vim
+=========
+
+Graphical Cheat Sheet
+---------------------
+
+
+![Vim Cheat Sheet](/images/vi-vim-cheat-sheet.gif)
+
+Learn vim with the [graphical cheat sheet](http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html).
+
+
+
+Toggle auto-indenting for code paste
+------------------------------------
+
+In a console or terminal version of Vim, there is no standard procedure
+to paste text from another application. Instead, the terminal may
+emulate pasting by inserting text into the keyboard buffer, so Vim
+thinks the text has been typed by the user. After each line ending, Vim
+may move the cursor so the next line starts with the same indent as the
+last. However, that will change the indentation already in the pasted
+text.
+
+Put the following in your `~/.vimrc`:
+
+ nnoremap <F2> :set invpaste paste?
+ set pastetoggle=<F2>
+ set showmode
+
+The first line sets a mapping so that pressing F2 in normal mode will
+invert the `'paste'`{.western} option, and will then show the value of
+that option. The second line allows you to press F2 when in insert mode,
+to toggle `'paste'`{.western}on and off. The third line enables
+displaying whether `'paste'`{.western} is turned on in insert mode.
+
+To paste from another application:
+
+- Start insert mode.
+
+- Press F2 (toggles the `paste` option on).
+
+- Use your terminal to paste text from the clipboard.
+
+- Press F2 (toggles the `paste` option off).
+
+Then the existing indentation of the pasted text will be retained.
+
+
+
+Using vim with Latex
+====================
+
+LaTeX is a system for setting characters ('types'). Actually, it is a wrapper of macros around TeX, which has a famous [history](http://www.tug.org/whatis.html). LaTeX is most widely used In technical writing, especially mathematics, physics, and computer science, because it can typeset beautiful mathematical formulae.
+
+
+
+System Setup on Debian/Ubuntu
+-----------------------------
+
+Install LaTeX:
+
+ sudo apt-get install \
+ texlive \
+ texlive-doc-de \
+ texlive-latex-extra \
+ texlive-lang-german \
+ texlive-science
+
+Install vim, the Gnome version:
+
+ sudo apt-get install \
+ vim-gnome \
+ vim-latexsuite
+ vim-addons install latex-suite
+
+Now you can edit and compile TeX files.
+
+
+
+Basic editing and compiling
+---------------------------
+
+
+Create file `~/.vim/ftplugin/tex.vim` and insert
+
+ " indentation
+ set sw=2
+
+ " compiler options
+ let g:Tex_DefaultTargetFormat='dvi'
+ let g:Tex_MultipleCompileFormats='dvi,pdf'
+ let g:Tex_FormatDependency_pdf = 'dvi,ps,pdf'
+ let g:Tex_CompileRule_dvi = 'latex --interaction=nonstopmode $* --src-specials'
+ let g:Tex_CompileRule_ps = 'dvips -Ppdf -o $*.ps $*.dvi'
+ let g:Tex_CompileRule_pdf = 'ps2pdf $*.ps'
+
+
+Add this to your `~/.vimrc`:
+
+ " REQUIRED. This makes vim invoke Latex-Suite when you open a tex file.
+ filetype plugin on
+
+ " IMPORTANT: win32 users will need to have 'shellslash' set so that latex
+ " can be called correctly.
+ set shellslash
+
+ " IMPORTANT: grep will sometimes skip displaying the file name if you
+ " search in a singe file. This will confuse Latex-Suite. Set your grep
+ " program to always generate a file-name.
+ set grepprg=grep\ -nH\ $*
+
+ " run latex
+ map ,l :!latex %
+
+ " Spelling:
+ autocmd FileType tex setlocal spell spelllang=en_us
+
+
+Now you can edit and compile `.tex` files from within gvim. Load a [template document](https://www.dropbox.com/sh/lppr2bhmrm0wpfc/KEokhp0usL) from my Dropbox.
+
+ gvim technical_report.tex
+
+In vim, press `\ll` to compile to dvi format. Start xdvi (the dvi viewer) to view results. Change something, compile again and reload (`Ctrl+R`) the viewer.
+
+
+Gvim can interact with xdvi
+--------------------------
+
+Goal: If one clicks at some place in xdvi (usually Ctrl+Button1), vim automatically jumps to the corresponding line in the LaTeX source file ("reverse search"). Also, from inside Vim, one can jump to the corresponding line in xdvi which becomes highlighted ("forward search").
+
+Note: This shows only the process for Gvim, the graphical vim version. It works analogous for the terminal Vim.
+
+First of all, the latex document need to tell the latex compiler to set marks in the dvi file. Only put:
+
+ \usepackage{srcltx}
+
+in the preamble of your latex document, or compile with:
+
+ latex --src [your_file.tex]
+
+In your `.vimrc` file, add at the end:
+
+ :map \ld :execute '!xdvi -editor "vim --servername 'v:servername' \
+ --remote +\%l \%f" -sourceposition ' . line(".") . expand("%") . \
+ " '" . expand(Tex_GetMainFileName(':r')) . ".dvi' >/dev/null&" <CR><CR>
+
+Note: The above should be a single line. The `\` are not part of the command and are there to indicate that the command continues on the next line!
+
+And in your `.bashrc`:
+
+ alias gvim='gvim --servername vimtex'
+
+
+Press `\ld` in Gvim in normal mode to start xdvi with forward search.
+For things to work you must, in order:
+
+1. Open the tex file with gvim (after `source`-ing your changed `~/.bashrc`),
+2. Open the dvi file with the command `\ld`
+3. reverse-search using Ctrl + left-mouse-click.
+
+Enjoy!
+
+
+
+Using Vim with Git
+==================
+
+
+What happens when you type `git diff`? You will receive a simple diff, but there is also a way to do a `vimdiff`.
+
+Here’s an example:
+
+ ![image](http://mps.s3.amazonaws.com/images/vimdiff.jpg "vimdiff")
+
+Vimdiff!
+
+Step 1: add this to your `.gitconfig`
+
+
+ [diff]
+ external = git_diff_wrapper
+ [pager]
+ diff =
+
+Step 2: create a file named `git_diff_wrapper` (and put it somewhere in your `$PATH`):
+
+
+ #!/bin/sh
+ vimdiff "$2" "$5"
+
+
+Step 3: there is still access to the default `git diff` behavior with the `--no-ext-diff` flag. Here’s a function I put in my bash configuration files:
+
+
+ function git_diff() {
+ git diff --no-ext-diff -w "$@" | vim -R -
+ }
+
+where the argument's meanings are:
+
+- `--no-ext-diff`: to prevent using vimdiff
+- `-w` : to ignore whitespace
+- `-R` : to start vim in read-only mode
+- `-` : to make vim act as a pager
+
+See [this tutorial](http://amjith.blogspot.com/2008/08/quick-and-dirty-vimdiff-tutorial.html) for Vimdiff usage.