Difference between revisions of "Vim"

From HalfgeekKB
Jump to navigation Jump to search
 
Line 4: Line 4:
 
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.
 
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=
+
==Super-quick bootstrap for pathogen==
  
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
Line 31: Line 31:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=Windows=
+
==Windows==
  
 
On Windows,
 
On Windows,
Line 71: Line 71:
 
  :echo &rtp
 
  :echo &rtp
  
=pathogen=
+
==pathogen==
  
 
[https://github.com/tpope/vim-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.
 
[https://github.com/tpope/vim-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.
Line 77: Line 77:
 
Vundle and NeoBundle are newer takes on the same idea, but I haven't auditioned them yet.
 
Vundle and NeoBundle are newer takes on the same idea, but I haven't auditioned them yet.
  
==Install==
+
===Install===
  
 
  <nowiki>
 
  <nowiki>
Line 94: Line 94:
 
echo "execute pathogen#infect()" >> ~/.vimrc # Windows: ... >> %USERPROFILE%/_vimrc</nowiki>
 
echo "execute pathogen#infect()" >> ~/.vimrc # Windows: ... >> %USERPROFILE%/_vimrc</nowiki>
  
==Add modules==
+
===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.
 
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.
Line 105: Line 105:
 
# Then add `colors palofobsidian` to your vimrc.</nowiki>
 
# Then add `colors palofobsidian` to your vimrc.</nowiki>
  
=Saving state=
+
==Saving state==
  
 
If you've got a notion to keep what's currently going on in your vim session as your new .vimrc, try
 
If you've got a notion to keep what's currently going on in your vim session as your new .vimrc, try
Line 113: Line 113:
 
This should store the state to the file.
 
This should store the state to the file.
  
=Embedding a per-file setting=
+
==Embedding a per-file setting==
  
 
It is possible to [http://ubergibson.com/article/embedding-vim-settings-in-the-file-youre-editing 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:
 
It is possible to [http://ubergibson.com/article/embedding-vim-settings-in-the-file-youre-editing 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:
Line 121: Line 121:
 
The source says to see <code>:help auto-setting</code> and <code>:help modeline</code> for exactly what to put there.
 
The source says to see <code>:help auto-setting</code> and <code>:help modeline</code> for exactly what to put there.
  
=Pasting things without auto-indenting them=
+
==Pasting things without auto-indenting them==
  
 
# <code>:set paste</code>
 
# <code>:set paste</code>
Line 129: Line 129:
 
[http://www.vim.org/tips/tip.php?tip_id=330 via]
 
[http://www.vim.org/tips/tip.php?tip_id=330 via]
  
=Set up new file types and syntaxes=
+
==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.
 
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.
Line 153: Line 153:
 
</nowiki>
 
</nowiki>
  
=256-color schemes at the terminal=
+
==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 [http://vim.wikia.com/wiki/256_colors_in_vim elsewhere].
 
Here's a short description of how to set up gvim color schemes to work in vim. The concept is elaborated upon [http://vim.wikia.com/wiki/256_colors_in_vim elsewhere].
  
==Correct a terminal that is misreporting itself==
+
===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.
 
Vim must know that the terminal it's running in is 256-color capable; it detects this automatically if the environment is set correctly.
Line 249: Line 249:
 
fi</nowiki>
 
fi</nowiki>
  
==Correct mintty on Cygwin==
+
===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 defaults to reporting itself as "xterm". Technically, it should be something like "mintty-256color", but "xterm-256color" will typically work.
Line 255: Line 255:
 
* mintty: Options : Terminal : Type = "xterm-256color"
 
* mintty: Options : Terminal : Type = "xterm-256color"
  
==Correct iTerm 2 on OS X==
+
===Correct iTerm 2 on OS X===
  
 
Set iTerm's terminal setting to "xterm-256color".
 
Set iTerm's terminal setting to "xterm-256color".
  
==Install scheme degrader for vim==
+
===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.
 
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.
Line 265: Line 265:
 
Install [http://www.vim.org/scripts/script.php?script_id=2390 CSApprox].
 
Install [http://www.vim.org/scripts/script.php?script_id=2390 CSApprox].
  
==Install the color scheme==
+
===Install the color scheme===
  
 
For example, sonofobsidian:
 
For example, sonofobsidian:
Line 276: Line 276:
 
colorscheme sonofobsidian</nowiki>
 
colorscheme sonofobsidian</nowiki>
  
=Cheat sheets=
+
==Cheat sheets==
  
 
If these pages vanish from the web, I've also seen them in the [[Wayback Machine]], so no worries.
 
If these pages vanish from the web, I've also seen them in the [[Wayback Machine]], so no worries.

Revision as of 05:58, 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

(
VIMRC=~/.vimrc
VIMFILES=~/.vim

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