Check vim version and python support from a bash script - bash

I have a bash script that is dependent on vim being at least version 7.4 and is installed with python. I need to check if the above condition matches, if not exit and ask user to update their vim.
So far all I can think of is something like below
has_vim = command -v vim >/dev/null
if ! $has_vim; then
echo "must have vim installed."
exit 1
fi
// Here I want do as the following pseudo code
vim_info = $(vim --version | grep python)
// suggest me if there is another way
vim_version = // find version info from $vim_info
has_python_support = // find python support from $vim_info
if ! $vim_version >= 7.4 && ! has_python_support; then
echo "vim version must be at least 7.4 and must be installed with python support"
fi
// everything is ok. carry on
At the moment all I can think of is checking $vim_info for expected vim version and python support.
To boil down the question into meaningful sentence:
How do I check if vim version is greater or equal to 7.4 and has python support from bash script?

When I ask my vim for vim --version, it spits out something like this:
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 16 2015 08:44:57)
Included patches: 1-872
Compiled by <alexpux#gmail.com>
Huge version without GUI. Features included (+) or not (-):
+acl +farsi +mouse_netterm +syntax
+arabic +file_in_path +mouse_sgr +tag_binary
+autocmd +find_in_path -mouse_sysmouse +tag_old_static
-balloon_eval +float +mouse_urxvt -tag_any_white
-browse +folding +mouse_xterm -tcl
++builtin_terms -footer +multi_byte +terminfo
+byte_offset +fork() +multi_lang +termresponse
+cindent +gettext -mzscheme +textobjects
-clientserver -hangul_input +netbeans_intg +title
+clipboard +iconv +path_extra -toolbar
+cmdline_compl +insert_expand +perl/dyn +user_commands
+cmdline_hist +jumplist +persistent_undo +vertsplit
+cmdline_info +keymap +postscript +virtualedit
+comments +langmap +printer +visual
+conceal +libcall +profile +visualextra
+cryptv +linebreak +python/dyn +viminfo
+cscope +lispindent +python3/dyn +vreplace
+cursorbind +listcmds +quickfix +wildignore
+cursorshape +localmap +reltime +wildmenu
+dialog_con -lua +rightleft +windows
+diff +menu +ruby/dyn +writebackup
+digraphs +mksession +scrollbind -X11
-dnd +modify_fname +signs -xfontset
-ebcdic +mouse +smartindent -xim
+emacs_tags -mouseshape -sniff -xsmp
+eval +mouse_dec +startuptime -xterm_clipboard
+ex_extra -mouse_gpm +statusline -xterm_save
+extra_search -mouse_jsbterm -sun_workshop -xpm
So, parsing the output would be a good bet here.
Find the version number and store it in VIMVERSION:
VIMVERSION=$(vim --version | head -1 | cut -d ' ' -f 5)
From here, check In bash shell script how do I convert a string to an number for how to compare the string result against your minimum needed 7.4.
Check for Python support (HAS_PYTHON will be 0 if Python is not available):
HAS_PYTHON=$(vim --version | grep -c '+python')
Check explicitly for Python 3 (again, HAS_PYTHON3 will be 0 if Python 3 is not available):
HAS_PYTHON3=$(vim --version | grep -c '+python3')
This might be a bit rough but I think you get the idea.

#!/bin/bash
has_vim=$(command -v vim >/dev/null)
if ! $has_vim; then
echo "must have vim installed."
exit 1
fi
# Checking the python support based on the line output received
has_python_support=$(vim --version | grep -c python)
# Matching the decimal pattern from the first line
vim_version=$(vim --version | head -1 | grep -o '[0-9]\.[0-9]')
if [ ! $(echo "$vim_version >= 7.4" | bc -l) ] && [ ! $has_python_support ]; then
echo "vim version must be at least 7.4 and must be installed with python support"
else
echo "vim version is > 7.4"
fi
Should solve your problem

If you have a running Vim instance already, or if you can create one, you can always just ask Vim itself:
$ vim --servername SOME_NAME --remote-expr "has('python')"
1
$ vim --servername SOME_NAME --remote-expr "v:version"
704
But if Vim isn't already running it's probably more straightforward to parse the --version output as suggested in other answers.

Related

Awkward line wrap in Yocto

I'm building an embedded Linux system for ARM architecture using Yocto (thud release).
The image builds and I'm able to flash the board. However, when I connect over serial line, I get this strange line wrapping.
You can see the character h overwriting bash prompt
The line keeps wrapping on the first line. Here is the environment:
guest#:ebox~ # printenv
LANG=C
EDITOR=vi
HZ=100
HUSHLOGIN=FALSE
USER=root
PWD=/home/root
HOME=/home/root
SHELL=/bin/sh
TERM=linux
SHLVL=1
LOGNAME=root
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
PS1=guest#:ebox\w \033[96m# \033[0m
_=/bin/printenv
I looked for solution and the best direction seems to be run with clean environment env -i bash --norc --noprofile
And the env:
bash-4.4# printenv
PWD=/home/root
SHLVL=1
_=/bin/printenv
EDIT: Ok I tried the PS1 prompt by enclosing the unprintable characters in brackets. It worked at that time on the booted system. So I changed the configuration in /etc/profile, built and flashed the system.
Unfortunately it’s not working and I get the same issue - the line wrapping.
Here is the content of the /etc/profile:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
PATH="/usr/local/bin:/usr/bin:/bin"
EDITOR="vi" # needed for packages like cron, git-commit
[ "$TERM" ] || TERM="vt100" # Basic terminal capab. For screen etc.
# Add /sbin & co to $PATH for the root user
[ "$HOME" != "/home/root" ] || PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin
# Set the prompt for bash and ash (no other shells known to be in use here)
[ -z "$PS1" ] || PS1="$(cat /etc/device/hwid)#\h:\w \001\033[96m\002# \001\033[0m\002"
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -f $i -a -r $i ]; then
. $i
fi
done
unset i
fi
# Make sure we are on a serial console (i.e. the device used starts with
# /dev/tty[A-z]), otherwise we confuse e.g. the eclipse launcher which tries do
# use ssh
case $(tty 2>/dev/null) in
/dev/tty[A-z]*) [ -x /usr/bin/resize ] && /usr/bin/resize >/dev/null;;
esac
export PATH PS1 OPIEDIR QPEDIR QTDIR EDITOR TERM
umask 022
I also turned on checkwinsize using shopt but didn’t work.
I also include the output of stty (over serial):
speed 115200 baud; line = 0;
-brkint ixoff iutf8
-iexten
And over SSH:
speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?;
-brkint ixany iutf8
I connect to the board over serial using screen on Mac. In minicom I get the same issue as do my colleagues on Windows using putty or screen on Linux. Same happens over SSH.
I also checked that the terming package and those files are installed correctly in /etc/terminfo.
EDIT:
I tried to change the size of the shell by using stty cols 100 rows 40 and different sizes but the amount of characters I get on the line stays at 81 followed by carrige return. However if I record the session with script I get the correct amount of characters per line.
Here is my stty -a output:
speed 115200 baud; rows 24; columns 100; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>;
swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O;
min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixoff -iuclc -ixany imaxbel
iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon -iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho
-extproc
EDIT:
So I tested the behavior on HW and in QEMU. The reported issue is till present in bash on both platforms. However, when I run busybox i don't see such issue only resizing doesn't change the line width and if make the terminal smaller I get on the 80th character new line (in middle of text).
So the problem is probably tight to term being set to fixed size.

How to make shortcut beginning of line ( ctrl-a ) work in kali linux?

Distribution: SMP Debian 4.9.6-3kali2 (2017-01-30)
Shell: bash
Program: Terminal emulator
Problem:
Ctrl + a Go to the beginning of the line (Home) is doing a select all
instead .
Ctrl + e Go to the End of the line (End) is working fine.
What I have tried:
stty - change and print terminal line settings
exemple: stty intr "your_new_shortcut"
$ stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ;
eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt =^R;
werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke -flusho -extproc
Solution
The only solution that I found, at this moment is to use the Xfce terminal include in my distro.
The ctrl-a beginning of the line works fine.
The only solution that I found, at this moment is to use the Xfce terminal include in my distro.
The ctrl-a beginning of the line works fine.
I know what you mean, the terminal has the ability to move to the first character and end of line, so why wouldn't the rest of the distro be able to?
I have found a half solution with the text editor named Atom, this is just for source code management. Perhaps if you're using a more advanced source code editor like RubyMine or any of the Jetbrains suites, they'll have a different way to configure them.
For Atom:
Press the hotkey Ctrl-Comma
Navigate to Keybindings
Find the built in link called your keymap file at the beginning of the file.
Copy and paste this into there:'atom-text-editor':
'atom-text-editor':
'ctrl-e': 'editor:move-to-end-of-screen-line'
'atom-workspace atom-text-editor':
'ctrl-a': 'editor:move-to-first-character-of-line'
'ctrl-shift-a': 'core:select-all'
these controls will be more similar to MacOS. Which I assume anyone asking this question will be making the switch over from there...

get only a COMMAND column in cygwin for ps command

In Cygwin, I can get the list of running processes by following command:
PID PPID PGID WINPID TTY UID STIME COMMAND
13160 1 13160 13160 ? 197609 13:42:18 /usr/bin/mintty
S 15404 2852 15404 16776 pty2 197609 13:59:29 /usr/bin/vi
2852 12912 2852 11244 pty2 197609 13:42:54 /usr/bin/bash
9864 1 9864 9864 ? 197609 13:11:32 /usr/bin/mintty
S 10500 2852 1692 1452 pty2 197609 14:09:42 /usr/bin/less
S 17644 2852 17644 11880 pty2 197609 14:00:15 /usr/bin/vi
12912 1 12912 12912 ? 197609 13:42:54 /usr/bin/mintty
8432 2852 8432 12020 pty2 197609 14:10:05 /usr/bin/ps
17092 13160 17092 14720 pty1 197609 13:42:18 /usr/bin/bash
However, I just want the COMMAND column but not all columns like this:
COMMAND
/usr/bin/mintty
/usr/bin/vi
/usr/bin/bash
/usr/bin/mintty
/usr/bin/less
/usr/bin/vi
/usr/bin/mintty
/usr/bin/ps
/usr/bin/bash
In MAC, I can do this by the following command:
ps -o command
The same command is not working for cygwin in windows because the -o option is not there in ps for cygwin.
you can use awk to print only the selected column.
$ ps |awk '{ if (NF==8) {print $8} else {print $9}}'
COMMAND
/usr/bin/ps
/usr/bin/bash
/usr/bin/mintty
For more learning:
http://www.grymoire.com/Unix/Awk.html#uh-17

Can't type 'a' symbol in terminal

I got into a strange behaviour of my terminal. Both iTerm and basic terminal on my OS X don't work. I can only type 'A' instead of 'a', but not the 'a' letter. If I try bash --noediting then I can type 'a' but it's very uncomfortable to use. I checked my ~/.bashrc, ~/.bash_profile and did't find anything that seemed strange for me. Could anyone help me?
Any feedback appreciated.
Most likely you've somehow introduced a readline binding for a.
As a first step, try this: (The grep argument is $"a", but you can't type that :) )
bind -p | grep $'"\x61"'
It should print this:
"a": self-insert
If it does, then my guess is wrong, and you'll need to look elsewhere. If it doesn't, then you need to fix it, which you can do like this:
bind $'"\x61"':self-insert
Now you need to find where in your bash start-up files the incorrect bind command is. I'd start by grepping for bind. It may well be in a file sourced from one of those files. Good luck.
You should also check the file ~/.inputrc which is used by the readline library to initialize it's bindings.
If the letter a has been mapped to one of the control characters, you can get some funny effects. Try stty -a, except that you'll probably need to type:
s, t, t, y, , -, Control-V, a
to get the -a to the command. This should show you something like:
speed 9600 baud; 65 rows; 135 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
-ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; intr = ^C; kill = ^X; lnext = ^V;
min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
stop = ^S; susp = ^Z; time = 0; werase = ^W;
except that if my suspicion is correct, one of the cchars values is a.

Why ctags doesn't work for me?

I try to work with ctags and it simply doesn't work.
I follow this screencast : http://www.youtube.com/watch?v=4f3AENLrdYo. When I press Ctrl + ], it does not matter.
This is my .vimrc :
" vim:set ts=2 sts=2 sw=2 expandtab:
call pathogen#runtime_append_all_bundles()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" BASIC EDITING CONFIGURATION
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
scriptencoding utf-8
set encoding=utf-8
set fileencoding=utf-8
set number
set nocompatible
" allow unsaved background buffers and remember marks/undo for them
set hidden
" remember more commands and search history
set history=10000
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set laststatus=2
set showmatch
set incsearch
set hlsearch
" make searches case-sensitive only if they contain upper-case characters
set ignorecase smartcase
" highlight current line
set cursorline
set cmdheight=2
set switchbuf=useopen
set numberwidth=5
set showtabline=2
set winwidth=85
set shell=bash
" Prevent Vim from clobbering the scrollback buffer. See
" http://www.shallowsky.com/linux/noaltscreen.html
set t_ti= t_te=
" keep more context when scrolling off the end of a buffer
set scrolloff=3
" Store temporary files in a central spot
set backup
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
" display incomplete commands
set showcmd
" Enable highlighting for syntax
syntax on
" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on
" use emacs-style tab completion when selecting files, etc
set wildmode=longest,list
" make tab completion for files/buffers act like bash
set wildmenu
let mapleader=","
let g:ctrlp_max_height = 40
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CUSTOM AUTOCMDS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
augroup vimrcEx
" Clear all autocmds in the group
autocmd!
autocmd FileType text setlocal textwidth=78
" Jump to last cursor position unless it's invalid or in an event handler
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
"for ruby, autoindent with two spaces, always expand tabs
autocmd FileType ruby,haml,eruby,yaml,html,javascript,sass,cucumber set ai sw=2 sts=2 et
autocmd FileType python set sw=4 sts=4 et
autocmd! BufRead,BufNewFile *.sass setfiletype sass
autocmd BufRead *.mkd set ai formatoptions=tcroqn2 comments=n:>
autocmd BufRead *.markdown set ai formatoptions=tcroqn2 comments=n:>
" Indent p tags
autocmd FileType html,eruby if g:html_indent_tags !~ '\\|p\>' | let g:html_indent_tags .= '\|p\|li\|dt\|dd' | endif
" Don't syntax highlight markdown because it's often wrong
autocmd! FileType mkd setlocal syn=off
augroup END
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" COLOR
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
:set t_Co=256 " 256 colors
:set background=dark
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" STATUS LINE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
:set statusline=%<%f\ (%{&ft})\ %-4(%m%)%=%-19(%3l,%02c%03V%)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" MISC KEY MAPS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <leader>y "*y
" Move around splits with <c-hjkl>
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-h> <c-w>h
nnoremap <c-l> <c-w>l
" Insert a hash rocket with <c-l>
imap <c-l> <space>=><space>
" Can't be bothered to understand ESC vs <c-c> in insert mode
imap <c-c> <esc>
" Clear the search buffer when hitting return
:nnoremap <CR> :nohlsearch<cr>
nnoremap <leader><leader> <c-^>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" ARROW KEYS ARE UNACCEPTABLE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <Left> :echo "no!"<cr>
map <Right> :echo "no!"<cr>
map <Up> :echo "no!"<cr>
map <Down> :echo "no!"<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" MULTIPURPOSE TAB KEY
" Indent if we're at the beginning of a line. Else, do completion.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! InsertTabWrapper()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper()<cr>
inoremap <s-tab> <c-n>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" OPEN FILES IN DIRECTORY OF CURRENT FILE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
cnoremap %% <C-R>=expand('%:h').'/'<cr>
map <leader>e :edit %%
map <leader>v :view %%
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RENAME CURRENT FILE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! RenameFile()
let old_name = expand('%')
let new_name = input('New file name: ', expand('%'), 'file')
if new_name != '' && new_name != old_name
exec ':saveas ' . new_name
exec ':silent !rm ' . old_name
redraw!
endif
endfunction
map <leader>n :call RenameFile()<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" PROMOTE VARIABLE TO RSPEC LET
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! PromoteToLet()
:normal! dd
" :exec '?^\s*it\>'
:normal! P
:.s/\(\w\+\) = \(.*\)$/let(:\1) { \2 }/
:normal ==
endfunction
:command! PromoteToLet :call PromoteToLet()
:map <leader>p :PromoteToLet<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" EXTRACT VARIABLE (SKETCHY)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! ExtractVariable()
let name = input("Variable name: ")
if name == ''
return
endif
" Enter visual mode (not sure why this is needed since we're already in
" visual mode anyway)
normal! gv
" Replace selected text with the variable name
exec "normal c" . name
" Define the variable on the line above
exec "normal! O" . name . " = "
" Paste the original selected text to be the variable value
normal! $p
endfunction
vnoremap <leader>rv :call ExtractVariable()<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" INLINE VARIABLE (SKETCHY)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! InlineVariable()
" Copy the variable under the cursor into the 'a' register
:let l:tmp_a = #a
:normal "ayiw
" Delete variable and equals sign
:normal 2daW
" Delete the expression into the 'b' register
:let l:tmp_b = #b
:normal "bd$
" Delete the remnants of the line
:normal dd
" Go to the end of the previous line so we can start our search for the
" usage of the variable to replace. Doing '0' instead of 'k$' doesn't
" work; I'm not sure why.
normal k$
" Find the next occurence of the variable
exec '/\<' . #a . '\>'
" Replace that occurence with the text we yanked
exec ':.s/\<' . #a . '\>/' . #b
:let #a = l:tmp_a
:let #b = l:tmp_b
endfunction
nnoremap <leader>ri :call InlineVariable()<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" MAPS TO JUMP TO SPECIFIC COMMAND-T TARGETS AND FILES
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <leader>gr :topleft :split config/routes.rb<cr>
function! ShowRoutes()
" Requires 'scratch' plugin
:topleft 100 :split __Routes__
" Make sure Vim doesn't write __Routes__ as a file
:set buftype=nofile
" Delete everything
:normal 1GdG
" Put routes output in buffer
:0r! rake -s routes
" Size window to number of lines (1 plus rake output length)
:exec ":normal " . line("$") . _ "
" Move cursor to bottom
:normal 1GG
" Delete empty trailing line
:normal dd
endfunction
map <leader>gR :call ShowRoutes()<cr>
map <leader>gv :CtrlPClearCache<cr>\|:CtrlP app/views<cr>
map <leader>gc :CtrlPClearCache<cr>\|:CtrlP app/controllers<cr>
map <leader>gm :CtrlPClearCache<cr>\|:CtrlP app/models<cr>
map <leader>gh :CtrlPClearCache<cr>\|:CtrlP app/helpers<cr>
map <leader>gl :CtrlPClearCache<cr>\|:CtrlP lib<cr>
map <leader>gp :CtrlPClearCache<cr>\|:CtrlP public<cr>
map <leader>gs :CtrlPClearCache<cr>\|:CtrlP spec<cr>
map <leader>gf :CtrlPClearCache<cr>\|:CtrlP features<cr>
map <leader>gg :topleft 100 :split Gemfile<cr>
map <leader>gt :CtrlPClearCache<cr>\|:CtrlPTag<cr>
map <leader>f :CtrlPClearCache<cr>\|:CtrlP<cr>
map <leader>F :CtrlPClearCache<cr>\|:CtrlP %%<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" SWITCH BETWEEN TEST AND PRODUCTION CODE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! OpenTestAlternate()
let new_file = AlternateForCurrentFile()
exec ':e ' . new_file
endfunction
function! AlternateForCurrentFile()
let current_file = expand("%")
let new_file = current_file
let in_spec = match(current_file, '^spec/') != -1
let going_to_spec = !in_spec
let in_app = match(current_file, '\<controllers\>') != -1 || match(current_file, '\<models\>') != -1 || match(current_file, '\<views\>') != -1
if going_to_spec
if in_app
let new_file = substitute(new_file, '^app/', '', '')
end
let new_file = substitute(new_file, '\.rb$', '_spec.rb', '')
let new_file = 'spec/' . new_file
else
let new_file = substitute(new_file, '_spec\.rb$', '.rb', '')
let new_file = substitute(new_file, '^spec/', '', '')
if in_app
let new_file = 'app/' . new_file
end
endif
return new_file
endfunction
nnoremap <leader>. :call OpenTestAlternate()<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RUNNING TESTS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! RunTests(filename)
" Write the file and run tests for the given filename
:w
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
if match(a:filename, '\.feature$') != -1
exec ":!script/features " . a:filename
else
if filereadable("script/test")
exec ":!script/test " . a:filename
elseif filereadable("Gemfile")
exec ":!bundle exec rspec --color " . a:filename
else
exec ":!rspec --color " . a:filename
end
end
endfunction
function! SetTestFile()
" Set the spec file that tests will be run for.
let t:grb_test_file=#%
endfunction
function! SetLineNumber()
" set the spec file that tests will be run for.
let t:spec_line_number=line('.')
endfunction
function! RunTestFileWithBacktrace(...)
if a:0
let command_suffix = a:1
else
let command_suffix = ""
endif
" Run the tests for the previously-marked file.
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\)$') != -1
if in_test_file
call SetTestFile()
elseif !exists("t:grb_test_file")
return
end
call RunTests(t:grb_test_file . command_suffix . " -b")
endfunction
function! RunTestFile(...)
if a:0
let command_suffix = a:1
else
let command_suffix = ""
endif
" Run the tests for the previously-marked file.
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\)$') != -1
if in_test_file
call SetTestFile()
elseif !exists("t:grb_test_file")
return
end
call RunTests(t:grb_test_file . command_suffix)
endfunction
function! RunNearestTest()
let in_test_file = match(expand("%"), '\(.feature\|_spec.rb\)$') != -1
if in_test_file
call SetLineNumber()
end
call RunTestFile(":" . t:spec_line_number )
endfunction
function! RunNearestTestWithBacktrace()
let spec_line_number = line('.')
call RunTestFile(":" . spec_line_number )
endfunction
map <leader>T :call RunTestFile()<cr>
map <leader>t :call RunNearestTest()<cr>
map <leader>B :call RunTestFileWithBacktrace()<cr>
map <leader>b :call RunNearestTestWithBacktrace()<cr>
map <leader>a :call RunTests('')<cr>
map <leader>c :w\|:!script/features<cr>
map <leader>w :w\|:!script/features --profile wip<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Md5 COMMAND
" Show the MD5 of the current buffer
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
command! -range Md5 :echo system('echo '.shellescape(join(getline(<line1>, <line2>), '\n')) . '| md5')
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" OpenChangedFiles COMMAND
" Open a split for each dirty file in git
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! OpenChangedFiles()
only " Close all windows, unless they're modified
let status = system('git status -s | grep "^ \?\(M\|A\)" | cut -d " " -f 3')
let filenames = split(status, "\n")
exec "edit " . filenames[0]
for filename in filenames[1:]
exec "sp " . filename
endfor
endfunction
command! OpenChangedFiles :call OpenChangedFiles()
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" InsertTime COMMAND
" Insert the current time
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
command! InsertTime :normal a<c-r>=strftime('%F %H:%M:%S.0 %z')<cr>
And this is my vim version :
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled May 5 2012 20:53:55)
Included patches: 1-154
Compiled by dougui#osacar
Huge version without GUI. Features included (+) or not (-):
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
+conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs
-dnd -ebcdic +emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path
+find_in_path +float +folding -footer +fork() -gettext -hangul_input +iconv
+insert_expand +jumplist +keymap +langmap +libcall +linebreak +lispindent
+listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape
+mouse_dec -mouse_gpm -mouse_jsbterm +mouse_netterm -mouse_sysmouse
+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype
+path_extra -perl +persistent_undo +postscript +printer +profile -python
-python3 +quickfix +reltime +rightleft +ruby +scrollbind +signs +smartindent
-sniff +startuptime +statusline -sun_workshop +syntax +tag_binary
+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
-toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo
+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
-xterm_clipboard -xterm_save
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
user exrc file: "$HOME/.exrc"
fall-back for $VIM: "/usr/local/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -I/usr/local/include -g -O2 -D_FORTIFY_SOURCE=1
Linking: gcc -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -rdynamic -Wl,-export-dynamic -L/usr/local/lib -Wl,--as-needed -o vim -lm -ltinfo -lnsl -ldl -lruby1.8 -lpthread -lrt -ldl -lcrypt -lm
What can I do?
Thanks!
I apologize. It was just a problem with my key board. With my french canadian configuration I must to do Ctrl + Alt Car + ]. I will map this keys.
Some simple tests :
To use tags, you must first generate a tag file (which index all the symbols). This is done by executing ctags -R . from the command line or :!ctags -R .from Vim.
After you have executed the command, check that a tag file has been created in the directory. This is a test file, so you can check if the list of symbols looks sensible.
Lastly, you can only jump on a tag, if Vim find the tag file in the path. This is achieve by setting the tags option in your .vimrc
I use the following option:
" search first in current directory then file directory for tag file
set tags=tags,../tags

Resources