Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
vim [2019/01/28 16:02]
paul [Programming]
vim [2020/10/19 17:24] (current)
Line 1: Line 1:
 ====== Vim ====== ====== Vim ======
  
-===== Movement =====+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 ===== 
 + 
 +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: 
 + 
 +<code bash> 
 +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 
 +</code> 
 + 
 +Make sure Vim is removed! When you type vim you should see nothing 
 + 
 +==== Compiling ==== 
 + 
 +To compile execute the following.  
 + 
 +<code bash> 
 +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 
 +</code> 
 + 
 +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: 
 +<code bash> 
 +make vimRUNTIMEDIR=/usr/local/share/vim/vim82 -j8 
 +</code> 
 + 
 +Then grab the Vundle plugin manager. 
 +<code bash> 
 +git clone https://github.com/Vundlevim/Vundle.vim.git ~/.vim/bundle/Vundle.vim 
 +</code> 
 + 
 +Get the ''%%.vimrc%%'' file from my git repo [[https://github.com/paulsammut/paul_scripts/blob/master/install/res/.vimrc|link]]. 
 + 
 +Run Vim and use command <code vim>:PluginInstall</code>  
 + 
 +YouCompleteMe requires a bit more processing to install: 
 +  
 +<code bash> 
 +cd .vim/bundle/YouCompleteMe  
 +./install.py --clang-completer 
 +</code> 
 + 
 +And then do this to get the autostyler to work: 
 +<code bash> 
 +sudo apt install astyle      # this makes my auto styler work 
 +</code> 
 + 
 +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: 
 + 
 +<code bash> 
 +/etc/apt/apt.conf.d/50unattended-upgrades 
 +</code> 
 + 
 +It should look like this:  
 +<code bash> 
 +// List of packages to not update (regexp are supported) 
 +Unattended-Upgrade::Package-Blacklist { 
 + "vim"; 
 +// "libc6"; 
 +// "libc6-dev"; 
 +// "libc6-i686"; 
 +}; 
 +</code> 
 +      
 +===== General Notes ===== 
 + 
 +Discussion on the python issue with YCM and Vim 
 +[[https://github.com/Valloric/YouCompleteMe/issues/2136|link]]. 
 +      
 +<code vim> 
 +: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. 
 +</code> 
 + 
 +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. 
  
 <code> <code>
-]] Jump forward to begin of next toplevel +|buffer-variable|    b:   Local to the current buffer. 
-[[ Jump backwards to begin of current toplevel (if already there, previous toplevel) +|window-variable|    w:   Local to the current window. 
-]m Jump forward to begin of next method/scope +|tabpage-variable|   t:   Local to the current tab page. 
-[m Jump backwords to begin of previous method/scope+|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. 
 +</code>
  
-][ Jump forward to end of current toplevel +The following command shows you all the variables that are set. 
-[] Jump backward to end of previous of toplevel +<code vim> 
-]M Jump forward to end of current method/scope +:let g:
-[M Jump backward to end of previous method/scope+
 </code> </code>
  
-===== Commenting =====+If you type a variable name after, you will get what it is currently set to.  
 +<code vim> 
 +:let g:CommandTTraverseSCM 
 +</code>
  
-I use plugin [[https://github.com/tomtom/tcomment_vim|tcomment]] for commenting linesKeybindings are:+To go to a tab, do ''{count}gt'' where count is the tab number (first is one). 
 + 
 +Send one command while in insert mode: ''CTRL-O'' 
 + 
 +Open a link in mac with the cursor on it: ''gx .'' 
 + 
 +===== Snippets ===== 
 + 
 +To use snippets for repetive code, put that code in a folder. Then use the ''r: 
 +./location/of/snippet'' command to dump the text in the active buffer. Easy 
 +Pease! 
 + 
 +==== Editing Remote Files ==== 
 + 
 +To edit a remote file run the following command: 
 +<code bash> 
 +vim scp://user@myserver[:port]//path/to/file.txt 
 +</code> 
 + 
 +===== Plugins ===== 
 + 
 +The following is info on the plugins I use
 + 
 +==== Airline ==== 
 + 
 +It took me so long to figure this stupid plugin out. It doesn't work out of the box. You need to dig through all kinds of documentation. 
 + 
 +=== For MacOS === 
 + 
 +Install the patched font, then select the font in terminal. 
 + 
 +{{:screen_shot_2019-12-26_at_9.45.42_pm.png?400|}} 
 +==== 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: 
 + 
 +<code vim> 
 +let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py' 
 +</code> 
 + 
 +This is a good write up about YCM [[https://jonasdevlieghere.com/a-better-youcompleteme-config/|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 ((https://github.com/arximboldi/dotfiles/blob/master/emacs/.ycm_extra_conf.py)). 
 + 
 + 
 +=== 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'
 +worry about thatWe use cterm colors, and there are ctermfg and ctermbg. 
  
 <code> <code>
-gc{motion}          :: Toggle comments  +gui == gvim. 
-gc<Count>c{motion}  :: Toggle comment with count argument                    +cterm == terminal. 
-gcc                 :: Toggle comment for the current line +fg == foreground 
-ctrl - -            :: Toggle comment for the current line+bg == background 
 +</code> 
 + 
 +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:  
 + 
 +<code vim> 
 +highlight YcmWarningSection ctermfg=XTERMCOLORNUMBER 
 +</code> 
 + 
 +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 [[https://github.com/wincent/command-t/blob/master/doc/command-t.txt|link]].  
 + 
 +<code> 
 +Leader - t  to open search 
 +<C-c>       to cancel (dismiss a file listing) 
 +<C-v>       opens file in new split window 
 +</code> 
 + 
 +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 ===== 
 + 
 +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  
 + 
 +<code vim> 
 +:help unimpared 
 +</code> 
 + 
 +Some of the ones I use are: 
 +<code> 
 +]e    move line up 
 +[e    move line down 
 +</code> 
 + 
 + 
 +===== vim Operations ===== 
 + 
 +==== Exuberant Ctags ==== 
 + 
 +Vim has native c-tags functionality but requires you to install a program and 
 +run it in the project folder. 
 + 
 +<code bash> 
 +sudo apt install exuberant-ctags 
 +</code> 
 + 
 +Then run this in the project directory to generate the tags. 
 + 
 +<code bash> 
 +ctags -R *  
 +</code> 
 + 
 +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: 
 + 
 +<code vim> 
 +:%s/\%Vselection/replace/
 +</code> 
 + 
 +==== Movement ==== 
 + 
 +<code vim> 
 +]]  " 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 
 +</code> 
 + 
 +==== Commenting ==== 
 + 
 +I use plugin [[https://github.com/tomtom/tcomment_vim|tcomment]] for commenting lines. Keybindings are: 
 + 
 +<code vim> 
 +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
  </code>  </code>
  
-===== Programming =====+==== Programming ==== 
 +<code vim> 
 +<leader> r          " Toggle true/false 
 +</code> 
 + 
 +==== Ordered Number List ==== 
 + 
 +Make a list of 1's, then block select the second one, and then press g <cnrl-a>
 + 
 +==== Quickfix ==== 
 + 
 +{{ :quickfix.png?400 |}} 
 + 
 +To navigate to the quick fix, <C-w> j.  
 + 
 +==== Vidir ==== 
 + 
 +To edit filenames in a directory using Vim, use the linux executable ''%%vidir%%'' from the ''%%moreutils%%'' package. Changes take effect after exiting. 
 +===== Programming C/C++ in Vim ===== 
 + 
 +I use YCM and ctags extensively while coding in C/C++. 
 + 
 +=== Vim Shortcuts ===
 <code> <code>
-<leader         :: Toggle true/false+ 
 +<C-wgf    Opens file under cursor in new tab 
 + 
 +K           Get documentation info pop up its man page.  
 + 
 +[i          Pressing on a function gives its signature.
 </code> </code>
 +
 +==== 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.
 +
 +===== Troubleshooting =====
 +
 +If Vim is slow in tmux:
 +
 +Link: [[https://github.com/christoomey/dotfiles/issues/13|Problem]]
 +Link: [[http://usevim.com/2012/04/18/startuptime/|Helpful]]
 +
 +Issue is because of xsmp init. Vim is trying to connect
 +
 +
  • vim.1548691337.txt.gz
  • Last modified: 2019/01/28 16:02
  • by paul