Difference between revisions of "Vim"
(3 intermediate revisions by the same user not shown) | |||
Line 74: | Line 74: | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | ==Sharing config between Windows gvim and Cygwin vim== | ||
+ | |||
+ | With some additional preparation, it should be possible to keep vim configuration for Windows gvim and Cygwin vim in the same place. | ||
+ | |||
+ | The following script will symlink ~/.vim and ~/.vimrc to their respective Windows counterparts. | ||
+ | |||
+ | <syntaxhighlight lang=bash> | ||
+ | CYG_USERPROFILE="`cygpath "$USERPROFILE"`" | ||
+ | VIMRC="$CYG_USERPROFILE/_vimrc" | ||
+ | VIMFILES="$CYG_USERPROFILE/vimfiles" | ||
+ | if [ -e ~/.vim -o -e ~/.vimrc ]; then | ||
+ | echo Move any existing ~/.vim or ~/.vimrc out of the way and try again. | ||
+ | else | ||
+ | ln -s "$VIMRC" ~/.vimrc && | ||
+ | ln -s "$VIMFILES" ~/.vim | ||
+ | fi | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===Not an editor command: ^M=== | ||
+ | |||
+ | While loading scripts in the Windows dirs, vim in Cygwin may complain | ||
+ | |||
+ | E492: Not an editor command: ^M | ||
+ | |||
+ | This is a line endings issue. Windows gvim doesn't mind LF, but Cygwin can't do CRLF. Open the offending script and change the line endings: | ||
+ | |||
+ | :set ff=unix | ||
+ | :wq | ||
+ | |||
+ | ===Conditionals=== | ||
+ | |||
+ | Conditionals can be used to run whatever code is necessary for the current platform. | ||
+ | |||
+ | <syntaxhighlight lang=vim> | ||
+ | if has('win32') || has('win64') | ||
+ | call SomeFunctionForWindowsNativeVim() | ||
+ | elseif has('win32unix') | ||
+ | call SomeFunctionForCygwinVim() | ||
+ | else | ||
+ | call SomeOtherFunction() | ||
+ | endif | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | See <code>:help has</code>, <code>:help feature-list</code>. | ||
==Windows== | ==Windows== | ||
Line 319: | Line 364: | ||
<nowiki> | <nowiki> | ||
colorscheme sonofobsidian</nowiki> | colorscheme sonofobsidian</nowiki> | ||
+ | |||
+ | ==Use like <code>less</code> (but with syntax colors)== | ||
+ | |||
+ | <syntaxhighlight lang=bash> | ||
+ | some-script-gen | vim -c 'set ft=somelanguage' -R - | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | It will actually work with as little as <code>vim -</code>, but: | ||
+ | |||
+ | * <code><nowiki>-c 'set ft=somelanguage'</nowiki></code> (e.g. <code><nowiki>-c 'set ft=perl'</nowiki></code>) causes the text to be properly highlighted for the given language | ||
+ | * <code>-R</code> sets readonly mode so that you can quit with <code>:q</code> instead of <code>:q!</code> | ||
==Cheat sheets== | ==Cheat sheets== |
Latest revision as of 05:20, 16 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.
Contents
- 1 Super-quick bootstrap with pathogen, sensible, and palofobsidian
- 2 Sharing config between Windows gvim and Cygwin vim
- 3 Windows
- 4 pathogen
- 5 Saving state
- 6 Embedding a per-file setting
- 7 Pasting things without auto-indenting them
- 8 Set up new file types and syntaxes
- 9 256-color schemes at the terminal
- 10 Use like less (but with syntax colors)
- 11 Cheat sheets
Super-quick bootstrap with pathogen, sensible, and palofobsidian
If you haven't set up vim at all for a given environment, this script will
- set up the user vimfiles with autoload, bundle, plugin dirs
- install tpope's pathogen so bundles installed under bundle work
- install tpope's sensible so that sane modern defaults are set concerning behavior
- install psmay's palofobsidian so that syntax highlighting isn't such an eyesore
For unixish systems, the script below can be used as-is. To set up Windows gvim using Cygwin, uncomment the lines under "Windows systems" to direct at the paths used under Windows.
Exercise caution if the vimrc and vimfiles are already set up—clobbering may occur.
# Unixy systems
VIMRC=~/.vimrc
VIMFILES=~/.vim
# Windows systems (for native Windows gvim, but this script should be run from Cygwin)
#CYG_USERPROFILE="`cygpath "$USERPROFILE"`"
#VIMRC="$CYG_USERPROFILE/_vimrc"
#VIMFILES="$CYG_USERPROFILE/vimfiles"
subdir_setup() {
echo "Creating $VIMFILES with subdirs" &&
mkdir -p "$VIMFILES" "$VIMFILES/autoload" "$VIMFILES/bundle" "$VIMFILES/plugin" &&
echo "Subdirs setup OK."
}
pathogen_setup () {
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 pathogen material to $VIMRC" &&
cat >>"$VIMRC" <<EOF &&
execute pathogen#infect()
syntax on
filetype plugin indent on
EOF
echo "pathogen setup OK."
}
sensible_setup () {
echo "Getting sensible as bundle" &&
cd "$VIMFILES/bundle" &&
git clone git://github.com/tpope/vim-sensible.git &&
echo "Adding sensible to $VIMRC" &&
echo 'runtime! plugin/sensible.vim' >>"$VIMRC" &&
echo "sensible 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."
}
(
subdir_setup &&
pathogen_setup &&
sensible_setup &&
palofobsidian_setup &&
echo OK
)
Sharing config between Windows gvim and Cygwin vim
With some additional preparation, it should be possible to keep vim configuration for Windows gvim and Cygwin vim in the same place.
The following script will symlink ~/.vim and ~/.vimrc to their respective Windows counterparts.
CYG_USERPROFILE="`cygpath "$USERPROFILE"`"
VIMRC="$CYG_USERPROFILE/_vimrc"
VIMFILES="$CYG_USERPROFILE/vimfiles"
if [ -e ~/.vim -o -e ~/.vimrc ]; then
echo Move any existing ~/.vim or ~/.vimrc out of the way and try again.
else
ln -s "$VIMRC" ~/.vimrc &&
ln -s "$VIMFILES" ~/.vim
fi
Not an editor command: ^M
While loading scripts in the Windows dirs, vim in Cygwin may complain
E492: Not an editor command: ^M
This is a line endings issue. Windows gvim doesn't mind LF, but Cygwin can't do CRLF. Open the offending script and change the line endings:
:set ff=unix :wq
Conditionals
Conditionals can be used to run whatever code is necessary for the current platform.
if has('win32') || has('win64')
call SomeFunctionForWindowsNativeVim()
elseif has('win32unix')
call SomeFunctionForCygwinVim()
else
call SomeOtherFunction()
endif
See :help has
, :help feature-list
.
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
:set paste
- Paste some code
:set nopaste
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
Use like less
(but with syntax colors)
some-script-gen | vim -c 'set ft=somelanguage' -R -
It will actually work with as little as vim -
, but:
-c 'set ft=somelanguage'
(e.g.-c 'set ft=perl'
) causes the text to be properly highlighted for the given language-R
sets readonly mode so that you can quit with:q
instead of:q!
Cheat sheets
If these pages vanish from the web, I've also seen them in the Wayback Machine, so no worries.