Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Tuesday, October 14, 2014

How to paste into vim from external buffer without broking formatting

When I paste to vim some text from external buffer, e.g. using Ctrl-Shift-V in a terminal, the formatting is often broken. Use:
:set paste
Then paste the text. When you are done:
:set nopaste

Wednesday, May 21, 2014

svn diff wrapper for vim with correct file names

#!/bin/sh

# use vimdiff to view diffs
DIFF="/usr/bin/vim -d -R"


NUM=$#  # number of arguments
LEFT=${6} # old file
RIGHT=${7} # new file
L_TITLE=${3} # actual name and revision of old file
R_TITLE=${5} # actual name and revision of new file

L_TITLE=`echo $L_TITLE | tr -s ' ' | sed 's/ /\\\\ /g'`
R_TITLE=`echo $R_TITLE | tr -s ' ' | sed 's/ /\\\\ /g'`

$DIFF $LEFT $RIGHT -c "setl stl=$L_TITLE | wincmd W | setl stl=$R_TITLE"

Thursday, March 27, 2014

Vim: Edit a file in the directory of the current file

:e %:p:h/filename
See also Set_working_directory_to_the_current_file.

Bullet lists autoformatting for text files in vim

In ~/.vim/after/ftplugin create a file text.vim containing
" Vim filetype plugin file
" Language: text
" Maintainer: Alexander Lobov 
" Last Change: 2014 Mar 27

" Only do this when not done yet for this buffer

if exists("b:did_ftplugin")
  finish
endif

" Don't load another plugin for this buffer
let b:did_ftplugin = 1

setlocal autoindent
setlocal formatoptions=tcroqln
The main strings are
setlocal autoindent
setlocal formatoptions=tcroqln
You .vimrc file should have
  filetype plugin indent on
line.

Tuesday, October 22, 2013

ctag and csope for java

The same script as in this post, but for Java:
$ cat ~/bin/javatags
#!/bin/sh
workdir=`pwd`
workdir=${workdir/\/c/c:}
echo Work directory: $workdir

if [ ! -d cscope ]
then
   echo Creating cscope directory
   mkdir cscope
fi

echo Generating ctags
cd $workdir
ctags -R --language-force=java .

echo Generating cscope
cd /
find $workdir -name "*.java" > $workdir/cscope/cscope.files
cd $workdir/cscope
cscope -bq

echo Complete

Sunday, October 20, 2013

Use :tjump instead of :tag in vim

Often when you press CTRL-] in vim there are several options to jump on. By default CTRL-] is mapped to :tag command, and you need to use :tselect after CTRL-] to see all alternatives. It is more convenient to see the list right away after pressing CTRL-]. vim already has such a command: :tjump. You just need to remap CTRL-] to :tjump instead of :tag:
nnoremap <c-]> g<c-]>
vnoremap <c-]> g<c-]>

nnoremap g<c-]> <c-]>
vnoremap g<c-]> <c-]>
Source.

Saturday, October 19, 2013

Not using of syntax highlighting

About a month ago I turned off syntax highlighting in vim. And it turns out that syntax highlighting does not matter. The code has not lost any readability. Moreover, nothing distracts me and I can concentrate on the code, not on the highlighting. So bravely remove or comment out the following lines from your .vimrc:
"if &t_Co > 2 || has("gui_running")
  " syntax on
  " set hlsearch
"endif
or just
syntax off
set bg=dark
If you want your gvim looking like vim in a terminal, try xterm16 colorscheme.
if has("gui_running")
    colo xterm16
endif

Thursday, November 15, 2012

Generating ctags and cscope tags for vim

I use this script for generate ctags and cscope tags. This MinGW version is to be run in source code directory.

#!/bin/sh
workdir=`pwd`
workdir=${workdir/\/c/c:}
echo Work directory: $workdir

if [ ! -d cscope ]
then
   echo Creating cscope directory
   mkdir cscope
fi

echo Generating ctags
cd $workdir
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

echo Generating cscope
cd /
find $workdir -name "*.cpp" -or -name "*.h" > $workdir/cscope/cscope.files
cd $workdir/cscope
cscope -bq

echo Complete
For running on Linux remove
workdir=${workdir/\/c/c:}

Sunday, November 11, 2012

My .vimrc file

I use default vimrc file as a base for my own one. From vim you can get it with

:e $VIMRUNTIME/vimrc_example.vim

I always use tab expanding. So, usually a part of my .vimrc file looks like:

set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set textwidth=80
Specific values for tabstop, shiftwidth and textwidth depend on project.

It is useful, especially during code review, to see not expanded tabs and trailing spaces. Leaving trailing spaces in code is bad idea, because they often mess diffs. To print these characters:

set listchars=trail:~,tab:>-
set list

Set colorschemes for GUI and console:

if has("gui_running")
    colorscheme wombat
else
    colorscheme wombat256
endif

Remove superfluous GUI elements

" remove toolbar
set guioptions-=T
" remove menu bar
set go-=m
" remove right-hand scroll bar
set go-=r

You can use second key mapping and switch between keymaps with CTRL-^ in insert mode, for example:

" remove toolbar
set guioptions-=T
" remove menu bar
set go-=m
" remove right-hand scroll bar
set go-=r

Set font for GUI:

set guifont=Ubuntu\ Mono\ 12
I use Ubuntu Mono or Monospace on Linux and Consolas on Windows.

256 colors in console vim

To use 256 colors in vim it is needed to correctly set $TERM variable. For gnome-terminal it is gnome-256color. vim uses $TERM variable and terminfo files to determine terminal properties. To determine correct value of $TERM you can add to your .bashrc this code from vim.wikia.com:

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

Also you have to have corresponding terminfo files. On Ubuntu you can install additional terminfo definitions:

sudo apt-get install ncurses-term
Now you have gnome-256color and other terminal definitions in /usr/share/terminfo (directory depends on system). You can use such themes as desert256, wombat256 etc.

To set colorscheme I use this code in my .vimrc:

if has("gui_running")
    colorscheme wombat
else
    colorscheme wombat256
endif

You can check how many colors are used inside vim by examining t_Co variable:

set t_Co
t_Co=256
I meet a difficulty with setting $TERM on one of Solaris servers I use. I had not colors in vim at all on this server. tput colors said me -1. There are not any terminfo file like *-256color and I have not root priviledges to add needed terminfo files. $TERM was set to xterm. But I found xtermc in terminfo directory. After setting $TERM to xtermc tput colors gives 8, and I can use syntax highlighting in vim, although not 256 colors.