Archive

Archive for the ‘vim’ Category

[vim] run current file with Python

Problem
You use (neo)vim for editing your Python code and you want to execute the source code in your editor. The output of the script should appear in the editor.

Solution
I came up with a dynamic solution, i.e. the interpreter is taken from the first line of the code. If you specified “#!/usr/bin/env python2“, then python2 is used; if you have “#!/usr/bin/env python3“, then python3 is used.

But what if you use Anaconda and you have for instance “#!/opt/anaconda3/bin/python3” in the first line? Then simply this interpreter is used.

Here is the snippet from my config file:

" run python script {{{
    function! RunWithPython()
        let first = getline(1)
        let first = substitute(first, "^#!", "", "")
        let first = substitute(first, "\n", "", "")
        let exe = ""    " the Python binary to call

        if first =~ "/usr/bin/env "
            let exe = split(first)[-1]
        elseif first == "/opt/anaconda3/bin/python3"
            let exe = first
        endif
        if exe == ""
            echo "Error: unknown Python interpreter in the first line."
            return
        endif
        " echo exe
        echo system(exe . " " . expand('%'))
    endfunction

    au FileType python nnoremap <buffer> <F9> :call RunWithPython()<cr>
" }}}

If you want to use Anaconda, then simply customize line 10.

Categories: python, vim Tags:

how to install Neovim under Ubuntu

Problem
Under Manjaro I installed neovim with yaourt. It installed version 0.1.4 and it works fine. For Ubuntu, the home page of Neovim suggests a PPA but it installed for me the version 0.1.5-dev, which is a development version.

How to install a specific version under Ubuntu (namely 0.1.4 in this case)?

Solution
Visit https://github.com/neovim/neovim and find the tagged version 0.1.4. Download the zip, uncompress it, and enter the project folder.

Install the dependencies:

$ sudo apt-get install libtool autoconf automake cmake libncurses5-dev g++

Build and install the project:

$ make cmake
$ make test
$ sudo make install

It’s a good idea to install the “neovim” Python package too:

$ sudo pip2 install neovim
$ sudo pip3 install neovim

Tips taken from here.

Categories: ubuntu, vim Tags: , , ,

[vim] a concrete repeat.vim example

Problem
In vim you have a custom mapping that consists of a series of commands. You would like to repeat it with the “.” command but it only repeats the last part of the series of commands, not the whole.

Tim Pope has a plugin for this called repeat.vim . However, he doesn’t give a concrete example in the README so it’s very hard to figure out how it works.

Solution
Let’s see the following mapping that surrounds a word with apostrophes:

nnoremap <Leader>' viw<esc>a'<esc>hbi'<esc>lel

Usage: put the cursor on a word and press <Leader>' to surround it.

If you try to repeat it with “.”, it will only repeat the insertion of the second apostrophe.

How to make the whole mapping repeatable:

    nnoremap <silent> <Plug>SurroundWordWithApostrophe  viw<esc>a'<esc>hbi'<esc>lel
        \ :call repeat#set("\<Plug>SurroundWordWithApostrophe", v:count)<cr>                                                                      
    nmap <Leader>'  <Plug>SurroundWordWithApostrophe

Links

Categories: vim Tags: , ,

[vim] I’m back to (neo)vim

May 15, 2016 2 comments

A week ago I returned to vim, more precisely to neovim.

Here is my story…

When I started the university (geez, that was 20 years ago) we got an account to a Unix server. On the first week I logged in and launched Midnight Commander. I wanted to check out the content of a file in my HOME folder so I pressed F3 (view) on it. However, the default editor was “vi” :) I had no idea how to quit this program so I had to ask a senior student in the lab to help me. He was kind and showed me the trick: “:q<Enter>“. So this is how I met vi and after this I avoided it for 3 years. Then I had a Java class where there was a guy who was a big zero in programming. One day he was sitting next to me and he was using Vim! And I was using Joe. He had nice syntax highlighting and he showed me some vim tricks. And I thought “WTF? If this guy could learn it, then I can learn it too.” I asked him about it and he told me that he started it with the command “vimtutor“. I went home and that very afternoon I went through the vimtutor too. I made some notes but I couldn’t memorize all those keyboard shortcuts. In the next few days I went through the tutorial two more times. Then I forced myself to use vim for all my text editing works and I got to like it. When I needed something I looked up how to do it with vim. The commands were logical and soon it was a pain to use any other editor.

Several years passed and I was very satisfied with Vim. I wasn’t a guru and I didn’t use many plugins, but I could solve all my problems with it. Then 2 years ago I heard that Emacs can be vimmified with the Evil plugin. I gave it a try and it was like a “better vim”. It had a nice GUI, I liked its default color scheme, it had a plugin manager, etc. I spent two weeks with its configuration and I could reproduce the majority of my vim settings. It was good.

I have several machines (desktops, laptops) and I used Emacs+Evil everywhere. I keep my settings in Dropbox, so I have the same configuration on all my computers. However, on my older machines Emacs started very slowly. Sometimes I had to wait 20 seconds! So when I wanted to edit a little script, I just launched vim. And it happened more and more often…

And now it’s present time. Two weeks ago I read about Neovim and I liked its features: a modernized vim, async job control, built-in terminal. I decided to give it a try. I created an empty init.vim file (it’s Neovim’s .vimrc equivalent) and transferred from .vimrc the necessary things. It turned out that my .vimrc had lots of obsolete settings that I could drop. On the forums I looked after the popular plugins. I even started learning vimscript :) Now my settings are better than my old Vim settings. I like Neovim and I am going to use it from now on.

If you are interested, then here is my init.vim configuration file for Neovim. It’s a work in progress but it’s already usable.

Expect some (neo)vim posts in the future :)

Categories: Uncategorized, vim Tags: , , ,

Vim tricks by Damian Conway

December 13, 2013 Leave a comment

At reddit I found an interesting talk given by Damian Conway. In this talk he shares some cool vim tricks.

The assets to the presentation are available here: Instantly_Better_Vim_2013.tar.gz

I found the following tips particularly interesting.

"====[ Make the 81st column stand out ]====================

"    " EITHER the entire 81st column, full-screen...
"    highlight ColorColumn ctermbg=magenta
"    set colorcolumn=81

    " OR ELSE just the 81st column of wide lines...
    highlight ColorColumn ctermbg=magenta
    call matchadd('ColorColumn', '\%81v', 100)

"=====[ Highlight matches when jumping to next ]=============

    " This rewires n and N to do the highlighing...
    nnoremap <silent> n   n:call HLNext(0.4)<cr>
    nnoremap <silent> N   N:call HLNext(0.4)<cr>

    " OR ELSE ring the match in red...
    function! HLNext (blinktime)
        highlight RedOnRed ctermfg=red ctermbg=red
        let [bufnum, lnum, col, off] = getpos('.')
        let matchlen = strlen(matchstr(strpart(getline('.'),col-1),@/))
        echo matchlen
        let ring_pat = (lnum > 1 ? '\%'.(lnum-1).'l\%>'.max([col-4,1]) .'v\%<'.(col+matchlen+3).'v.\|' : '')
                \ . '\%'.lnum.'l\%>'.max([col-4,1]) .'v\%<'.col.'v.'
                \ . '\|'
                \ . '\%'.lnum.'l\%>'.max([col+matchlen-1,1]) .'v\%<'.(col+matchlen+3).'v.'
                \ . '\|'
                \ . '\%'.(lnum+1).'l\%>'.max([col-4,1]) .'v\%<'.(col+matchlen+3).'v.'
        let ring = matchadd('RedOnRed', ring_pat, 101)
        redraw
        exec 'sleep ' . float2nr(a:blinktime * 1000) . 'm'
        call matchdelete(ring)
        redraw
    endfunction

"====[ Make tabs, trailing whitespace, and non-breaking spaces visible ]======

    exec "set listchars=tab:\uBB\uBB,trail:\uB7,nbsp:~"
    set list

"====[ dragvisuals ]======

    runtime plugin/dragvisuals.vim
    vmap  <expr>  h        DVB_Drag('left')
    vmap  <expr>  l        DVB_Drag('right')
    vmap  <expr>  j        DVB_Drag('down')
    vmap  <expr>  k        DVB_Drag('up')
    "vmap  <expr>  D        DVB_Duplicate()

    " Remove any introduced trailing whitespace after moving...
    let g:DVB_TrimWS = 1

The file dragvisuals.vim is in the archive linked above.

Categories: vim Tags:

vim color schemes with dark and light backgrounds

September 12, 2013 Leave a comment

Problem
I’m looking for a nice color scheme in vim for (1) dark background, and for (2) light background. Also, I want to be able to switch between them easily.

Solution
For dark background I prefer the elflord theme, which comes with the standard installation. It can be found here: /usr/share/vim/vim73/colors/elflord.vim.

For finding a nice and simple theme for light background I had to do some research. Finally I came across the trivial256 color scheme. However, I didn’t like that strings were colored red so I changed one line:

hi String       ctermfg=darkgreen

The modified version can be downloaded from here.

Installation
Create the directory .vim/colors in your HOME directory and copy trivial256.vim in there. I created two symlinks too called light.vim and dark.vim:

dark.vim -> /usr/share/vim/vim73/colors/elflord.vim
light.vim -> trivial256.vim

Specify in your ~/.vimrc which one should be the default:

colorscheme dark
"colorscheme light

In vim you can change the color scheme easily:

:colo light
:colo dark

You need to type just one of them. Notice that the extension “.vim” is omitted.

Screenshots
With dark background (click to enlarge):

dark

With light background (click to enlarge):

light

Links

Format text in vim in 5 seconds

April 18, 2012 Leave a comment

Problem
You use vim and you don’t want any lines to be longer than 80 characters. However, when you select a paragraph in the browser and you paste it in vim, the whole paragraph is inserted in one long line. Since you are just as lazy as me, you don’t want to break the line manually. What to do?

Solution
Select the line in visual mode (Shift + V). At the bottom you should see “-- VISUAL LINE --“. Press ! (now at the bottom you should see :'<,'>!), type fmt (thus you have :'<,'>!fmt at the bottom) and press Enter. Done.

What just happened?
We passed the selected text to the external Unix command fmt, which reformatted the text. Vim then inserted the output of fmt to the place of the selected text.

So, if you have a list of words, one word in a line, and you want to sort them, just pass them to the external command sort: select the lines visually, !sort and Enter.

[ reddit discussion ]

Categories: vim Tags: , , ,
Follow

Get every new post delivered to your Inbox.

Join 93 other followers