Vim

Vim is one of my favorite tools. Whenever you are working and think, hey it would be really useful if I could do this, chances are Vim lets you do just that in a couple of keystrokes.

Installing Vim with my plugins requires Vim with python. The reason for this is so that YCM works. There is an issue with YCM, Vim and Ubuntu 16.04 which is a conflict between python 2 and 3. What we do is configure Vim to compile with python2 and set YCM to also compile with just python2. We then have to run an install script which makes it work. Also, this compilation of Vim gives us the awesome clipboard support that works right out of the box. We also install ruby for the fantastic Command-T plugin to work.

Follow this to compile Vim:

sudo apt-get install cmake libncurses5-dev libgnome2-dev libgnomeui-dev \
    libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
    libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \
    python3-dev ruby-dev lua5.1 lua5.1-dev libperl-dev git
 
sudo apt-get remove Vim Vim-runtime gVim Vim-common

Make sure Vim is removed! When you type Vim you should see nothing

Compiling

To compile execute the following.

cd 
git clone https://github.com/Vim/Vim.git
cd Vim
git checkout
 
./configure --with-features=huge \
     --enable-multibyte \
     --enable-rubyinterp=yes \
     --enable-pythoninterp=yes \
     --with-python-config-dir=/usr/lib/python2.7/config \
     --enable-python3interp=no \
     --with-python3-config-dir=/usr/lib/python3.5/config \
     --enable-perlinterp=yes \
     --enable-luainterp=yes \
     --enable-gui=gtk2 \
     --enable-cscope \
     --prefix=/usr/local
 
make VimRUNTIMEDIR=/usr/local/share/Vim/Vim81 -j8
sudo apt install checkinstall
sudo checkinstall
 
sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/Vim 1
sudo update-alternatives --set editor /usr/local/bin/Vim 
sudo update-alternatives --install /usr/bin/vi vi /usr/local/bin/Vim 1
sudo update-alternatives --set vi /usr/local/bin/Vim

Be sure to change the version when executing the make command. For instance, if the major Vim version is at 8.2, then make line should be:

make VimRUNTIMEDIR=/usr/local/share/Vim/Vim82 -j8

Then grab the Vundle plugin manager.

git clone https://github.com/VundleVim/Vundle.Vim.git ~/.Vim/bundle/Vundle.Vim

Get the .vimrc file from my gitrepo link.

Run Vim and use command

:PluginInstall

YouCompleteMe requires a bit more processing to install:

cd .Vim/bundle/YouCompleteMe 
./install.py --clang-completer

And then do this to get the autostyler to work:

sudo apt install astyle      # this makes my auto styler work

Also disable the unattended updates. If you do not do this, every day Vim will get downgraded to 7.4.1689. To do this, add Vim to the black list, by editing:

/etc/apt/apt.conf.d/50unattended-upgrades

It should look like this:

// List of packages to not update (regexp are supported)
Unattended-Upgrade::Package-Blacklist {
	"Vim";
//	"libc6";
//	"libc6-dev";
//	"libc6-i686";
};

Discussion on the python issue with YCM and Vim link.

:echo has("python")     " this tells you if you have python2 support
:echo has("python3")    " this tells you if you have python3 support
:ruby 1                 " Gives an error if ruby not present.

If Vim looks like it freezes inside tmux, it's because I accidentally pressed <C-s> which disables flow control in the terminal. Just hit <C-q> and everything will be alright.

There are things called internal-variables in Vim.

|buffer-variable|    b:	  Local to the current buffer.
|window-variable|    w:	  Local to the current window.
|tabpage-variable|   t:	  Local to the current tab page.
|global-variable|    g:	  Global.
|local-variable|     l:	  Local to a function.
|script-variable|    s:	  Local to a |:source|'ed Vim script.
|function-argument|  a:	  Function argument (only inside a function).
|Vim-variable|       v:	  Global, predefined by Vim.

The following command shows you all the variables that are set.

:let g:

If you type a variable name after, you will get what it is currently set to.

:let g:CommandTTraverseSCM

The following is info on the plugins I use.

NerdTree

NerdTree is one of my most used plugins. It is fantastic at navigating files, along with moving and deleting them. There are a bunch of really useful key mappings that I forget from time to time. Just remember to press ? in an open NerdTree buffer and you'll get all the shortcuts to refresh yourself with.

YCM

YCM uses a python script to tell it what to do in terms of syntax highlighting and error/warning display. You have to tell it where to use this script by adding this in the .vimrc file:

let g:ycm_global_ycm_extra_conf = '~/.Vim/.ycm_extra_conf.py'

This is a good write up about YCM link

I used this .ycm_extra_conf.py file because it had the new -std flags and didn't give me a stupid warning that obscures everything 1).

YCM Colors

Ok so YCM uses highlighting colors to change the colors of errors. Default they are red, and they cover any text making it super annoying. SO. You have to change the color. THIS IS NOT EASY as there are like a bajillion different colors. There are cterm colors and gui colors. Gui colors are for gVim, which we don't use so don't worry about that. We use cterm colors, and there are ctermfg and ctermbg.

gui == gVim.
cterm == terminal.
fg == foreground
bg == background

You can also style the line that has the warning/error with these groups:

YcmErrorLine which falls back to group SyntasticErrorLine if it exists YcmWarningLine which falls back to group SyntasticWarningLine if it exists Note that the line highlighting groups only work when gutter signs are turned on.

The syntax groups used to highlight regions of text with errors/warnings:

YcmErrorSection which falls back to group SyntasticError if it exists and then SpellBad YcmWarningSection which falls back to group SyntasticWarning if it exists and then SpellCap

So to change the warning highlight color, I would say:

highlight YcmWarningSection ctermfg=XTERMCOLORNUMBER

The xterm color numbers are 3 digit numbers. Look em up.

Command-T

For the command-t plugin, you need to go to ~/.vim/bundle/command-t and type in rake make. This is to fix the command-t “Vim could not load the c extension” error.

The documenation here has a great how-to section link.

Leader - t  to open search
<C-c>       to cancel (dismiss a file listing)
<C-v>       opens file in new split window

I always got confused with Command T's starting search directory. And I had good reason! It uses what is called SCM traversal, which finds the git repo the file you're working with is in and uses that as the search directory. Smart for stuff where the main project is in one git repo, but sucky for stuff that is in a bigger repo. SO. I just had to set it the traversal to “pwd” which is the current directory the file is in. Ok no more wasting time!!!

unimpared is a Tim Pope plugin that provides a set of key mappings paired with brackets. There are lots of ones I use regularly and to refresh do

:help unimpared

Some of the ones I use are:

]e    move line up
[e    move line down

Exuberant Ctags

Vim has native c-tags functionality but requires you to install a program and run it in the project folder.

sudo apt install exuberant-ctags

Then run this in the project directory to generate the tags.

ctags -R * 

Then in Vim just go over any text you want and press <C-]> to go to the token.

General Operations

To search and replace within a visual selection within a line you must use the %V atom. Make a selection and use the following command:

:%s/\%Vselection/replace/g

Movement

]]  " Jump forward to begin of next toplevel
[[  " Jump backwards to begin of current toplevel (if already there, previous toplevel)
]m  " Jump forward to begin of next method/scope
[m  " Jump backwords to begin of previous method/scope
 
][  " Jump forward to end of current toplevel
[]  " Jump backward to end of previous of toplevel
]M  " Jump forward to end of current method/scope
[M  " Jump backward to end of previous method/scope

Commenting

I use plugin tcomment for commenting lines. Keybindings are:

gc{motion}          " Toggle comments 
gc<Count>c{motion}  " Toggle comment with count argument                   
gcc                 " Toggle comment for the current line
ctrl - -            " Toggle comment for the current line
 

Programming

<leader> r          " Toggle true/false

Quickfix

To navigate to the quick fix, <C-w> j.

Vidir

To edit filenames in a dir using Vim, use the linux executable vidir from the moreutils package. Changes take effect after exiting.

I use YCM and ctags extensively while coding in C/C++.

Vim Shortcuts

<C-w> gf    Opens file under cursor in new tab

K           Get documentation info pop up its man page. 

[i          Pressing on a function gives its signature.

File Management

:checkpath shows you a list of included files not found in the path.

$locate [header] gives you the location of the system header file.

If Vim is slow in tmux:

Link: Problem Link: Helpful

Issue is because of xsmp init. Vim is trying to connect


Enter your comment. Wiki syntax is allowed:
 
  • vim.txt
  • Last modified: 2019/03/18 01:50
  • by paul