Difference between revisions of "Vim"

From HalfgeekKB
Jump to navigation Jump to search
Line 7: Line 7:
  
 
If you haven't set up vim at all for a given environment, this script will set up the user autoload and bundle dirs, install pathogen, and place a .vimrc that calls pathogen#infect().
 
If you haven't set up vim at all for a given environment, this script will set up the user autoload and bundle dirs, install pathogen, and place a .vimrc that calls pathogen#infect().
 +
 +
Exercise caution if the vimrc and vimfiles are already set up—clobbering may occur.
 +
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
(
+
# Unixy systems or Cygwin
 
VIMRC=~/.vimrc
 
VIMRC=~/.vimrc
 
VIMFILES=~/.vim
 
VIMFILES=~/.vim
  
 +
# Windows systems (for native Windows gvim, but run this script from Cygwin)
 +
#CYG_USERPROFILE="`cygpath "$USERPROFILE"`"
 +
#VIMRC="$CYG_USERPROFILE/_vimrc"
 +
#VIMFILES="$CYG_USERPROFILE/vimfiles"
 +
 +
basic_setup() {
 
echo "Creating $VIMFILES with autoload and bundle dirs" &&
 
echo "Creating $VIMFILES with autoload and bundle dirs" &&
 
mkdir -p "$VIMFILES" &&
 
mkdir -p "$VIMFILES" &&
Line 28: Line 37:
 
filetype plugin indent on
 
filetype plugin indent on
 
EOF
 
EOF
 +
echo "Basic setup OK."
 +
}
 +
 +
palofobsidian_setup() {
 +
echo "Getting palofobsidian color scheme as bundle" &&
 +
cd "$VIMFILES/bundle" &&
 +
git clone https://github.com/psmay/vim-colors-palofobsidian &&
 +
echo "Adding colors palofobsidian to $VIMRC" &&
 +
echo 'colors palofobsidian' >>"$VIMRC" &&
 +
echo "palofobsidian setup OK."
 +
}
 +
 +
(
 +
basic_setup &&
 +
palofobsidian_setup &&
 
echo OK
 
echo OK
 
)
 
)
 +
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 06:16, 7 May 2014

Template:Unix Vim (command: vim), aka Vi IMproved, is a text editor. Emacs enthusiasts are religious about hating it. I myself couldn't live without it.

First, a working .vimrc. My .vimrc does most or all of the keyboard mapping work necessary to make insert mode operate correctly using normal keys (arrows, home, end, backspace, et cetera) instead of freaking out.

Super-quick bootstrap for pathogen

If you haven't set up vim at all for a given environment, this script will set up the user autoload and bundle dirs, install pathogen, and place a .vimrc that calls pathogen#infect().

Exercise caution if the vimrc and vimfiles are already set up—clobbering may occur.

# Unixy systems or Cygwin
VIMRC=~/.vimrc
VIMFILES=~/.vim

# Windows systems (for native Windows gvim, but run this script from Cygwin)
#CYG_USERPROFILE="`cygpath "$USERPROFILE"`"
#VIMRC="$CYG_USERPROFILE/_vimrc"
#VIMFILES="$CYG_USERPROFILE/vimfiles"

basic_setup() {
echo "Creating $VIMFILES with autoload and bundle dirs" &&
mkdir -p "$VIMFILES" &&
mkdir -p "$VIMFILES/autoload" &&
mkdir -p "$VIMFILES/bundle" &&
echo "Getting pathogen as bundle" &&
cd "$VIMFILES/bundle" &&
git clone https://github.com/tpope/vim-pathogen.git &&
echo "Adding script to sourcing pathogen from autoload" &&
echo 'runtime bundle/vim-pathogen/autoload/pathogen.vim' \
	> "$VIMFILES/autoload/pathogen.vim" &&
echo "Adding basic material to $VIMRC" &&
cat >>"$VIMRC" <<EOF &&
execute pathogen#infect()
syntax on
filetype plugin indent on
EOF
echo "Basic setup OK."
}

palofobsidian_setup() {
echo "Getting palofobsidian color scheme as bundle" &&
cd "$VIMFILES/bundle" &&
git clone https://github.com/psmay/vim-colors-palofobsidian &&
echo "Adding colors palofobsidian to $VIMRC" &&
echo 'colors palofobsidian' >>"$VIMRC" &&
echo "palofobsidian setup OK."
}

(
basic_setup &&
palofobsidian_setup &&
echo OK
)

Windows

On Windows,

  • ~/.vimrc is called %USERPROFILE%\_vimrc
  • ~/.vim/ is called %USERPROFILE%\vimfiles\

where %USERPROFILE% refers to e.g. C:\Users\username.

The actual location for the vimrc file is given as part of the output of command:

:version

On Windows, the first part that relates is something like

   system vimrc file: "$VIM\vimrc"
     user vimrc file: "$HOME\_vimrc"

On Mint, it looks more like

   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"

You can find the values of $VIM and $HOME within vim itself:

:echo $VIM
:echo $HOME

See the help for the runtimepath (or rtp) setting to determine the system equivalent for ~/.vim:

:h 'rtp'

For unix, "$HOME/.vim" (i.e., "~/.vim") is listed, while for Windows "$HOME/vimfiles" is given instead.

For its current value, do

:echo &rtp

pathogen

pathogen is a handy script addition that allows vim addons to be installed as easy-to-install, easy-to-update, easy-to-remove bundles rather than being unloaded into the existing directories where they can't be easily managed.

Vundle and NeoBundle are newer takes on the same idea, but I haven't auditioned them yet.

Install

# Set up runtime directories
mkdir -p ~/.vim          # Windows: mkdir %USERPROFILE%\vimfiles
cd ~/.vim                # Windows: cd %USERPROFILE%\vimfiles
mkdir -p autoload bundle # Windows: mkdir autoload bundle

# Get pathogen.vim into autoload dir
cd autoload
wget https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
# curl: curl -Sso pathogen.vim https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
# web browser: Save https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim to autoload dir

# Set pathogen to run from vimrc
echo "execute pathogen#infect()" >> ~/.vimrc # Windows: ... >> %USERPROFILE%/_vimrc

Add modules

pathogen will rewrite rtp so that anything under the bundle directory will itself become a runtime path. This works especially nicely with modules retrievable from github.

# Example: Install palofobsidian color scheme
cd ~/.vim/bundle # Windows: cd %USERPROFILE%\vimfiles\bundle
git clone https://github.com/psmay/vim-colors-palofobsidian

# Then add `colors palofobsidian` to your vimrc.

Saving state

If you've got a notion to keep what's currently going on in your vim session as your new .vimrc, try

:mkvimrc filename

This should store the state to the file.

Embedding a per-file setting

It is possible to embed vim settings in a source file using a specially-formatted comment. Vim apparently scans the first and last few lines of a file it opens looking for something like this:

# vim:ts=4:sw=4

The source says to see :help auto-setting and :help modeline for exactly what to put there.

Pasting things without auto-indenting them

  1. :set paste
  2. Paste some code
  3. :set nopaste

via

Set up new file types and syntaxes

Run this script to set up the user vim directories. Add syntax files to the syntax directory. Follow the example in filetype.vim (or /usr/share/vim/vim72/filetype.vim) to set up extensions to match those syntaxes. This script will add a line to your .vimrc to source this file, completing the circle.

#!/bin/sh
cd
mkdir .vim
cd .vim
mkdir doc
mkdir syntax
mkdir plugin
mkdir colors
echo '
" A decent example of associating a syntax with an extension
" au BufNewFile,BufRead *.g	setf antlr3
' >> filetype.vim
cd ..
echo '
" Add extra syntax types
source ~/.vim/filetype.vim
' >> .vimrc

256-color schemes at the terminal

Here's a short description of how to set up gvim color schemes to work in vim. The concept is elaborated upon elsewhere.

Correct a terminal that is misreporting itself

Vim must know that the terminal it's running in is 256-color capable; it detects this automatically if the environment is set correctly.

  • Requires ncurses (supplies tput).
  • Ubuntu: Requires ncurses-term (contains settings for gnome-256color).

In .correct-term-setting:

#!/bin/sh

# From a comment on
# http://vim.wikia.com/wiki/256_colors_in_vim

# In cygwin, make sure ncurses is installed (tput needs it).
# In ubuntu, be sure to install ncurses-term so that gnome-256color is
# recognized.

if [ "$TERM" = "xterm" ] ; then
	if [ -z "$COLORTERM" ] ; then
		if [ -z "$XTERM_VERSION" ] ; then
			echo "Warning: Terminal wrongly calling itself 'xterm'."
		else
			case "$XTERM_VERSION" in
			"XTerm(256)") TERM="xterm-256color" ;;
			"XTerm(88)") TERM="xterm-88color" ;;
			"XTerm") ;;
			*)
				echo "Warning: Unrecognized XTERM_VERSION: $XTERM_VERSION"
				;;
			esac
		fi
	else
		case "$COLORTERM" in
			gnome-terminal)
				# Those crafty Gnome folks require you to check COLORTERM,
				# but don't allow you to just *favor* the setting over TERM.
				# Instead you need to compare it and perform some guesses
				# based upon the value. This is, perhaps, too simplistic.
				TERM="gnome-256color"
				;;
			*)
				echo "Warning: Unrecognized COLORTERM: $COLORTERM"
				;;
		esac
	fi
fi

SCREEN_COLORS="`tput colors`"
if [ -z "$SCREEN_COLORS" ] ; then
	case "$TERM" in
		screen-*color-bce)
			echo "Unknown terminal $TERM. Falling back to 'screen-bce'."
			export TERM=screen-bce
			;;
		*-88color)
			echo "Unknown terminal $TERM. Falling back to 'xterm-88color'."
			export TERM=xterm-88color
			;;
		*-256color)
			echo "Unknown terminal $TERM. Falling back to 'xterm-256color'."
			export TERM=xterm-256color
			;;
	esac
	SCREEN_COLORS=`tput colors`
fi
if [ -z "$SCREEN_COLORS" ] ; then
	case "$TERM" in
		gnome*|xterm*|konsole*|aterm|[Ee]term)
			echo "Unknown terminal $TERM. Falling back to 'xterm'."
			export TERM=xterm
			;;
		rxvt*)
			echo "Unknown terminal $TERM. Falling back to 'rxvt'."
			export TERM=rxvt
			;;
		screen*)
			echo "Unknown terminal $TERM. Falling back to 'screen'."
			export TERM=screen
			;;
	esac
	SCREEN_COLORS=`tput colors`
fi

In .bashrc:

if [ -f ~/.correct-term-setting ]; then
    . ~/.correct-term-setting
fi

Correct mintty on Cygwin

mintty defaults to reporting itself as "xterm". Technically, it should be something like "mintty-256color", but "xterm-256color" will typically work.

  • mintty: Options : Terminal : Type = "xterm-256color"

Correct iTerm 2 on OS X

Set iTerm's terminal setting to "xterm-256color".

Install scheme degrader for vim

A script is used to adapt color schemes intended for gvim to work with console vim by converting to the nearest 256-color-safe equivalenta.

Install CSApprox.

Install the color scheme

For example, sonofobsidian:

Install sonofobsidian scheme in ~/.vim/colors.

In .vimrc:

colorscheme sonofobsidian

Cheat sheets

If these pages vanish from the web, I've also seen them in the Wayback Machine, so no worries.