Showing posts with label Vim. Show all posts
Showing posts with label Vim. Show all posts

Wednesday, April 25, 2018

Three Minutes Daily Vim Tip: Highlight Current Cursor Line

In Vim, it is often difficult to locate the current cursor, especially when you jump around. Here is a tip to easily locate the cursor.

Simply add the following line to ~/.vimrc
set cursorline

That's it!

Python IDE with YouCompleteMe

OK, I have tried YouCompleteMe, but then I stopped using it after some time. Well, I am going to give it a try once more. The reason is that I am usually developing on a server through ssh. I could launch GUI-based IDE, such as PyCharm through X-forwarding, but the response is too slow. I realized that editing with Vim on a remote server is probably the best option here.

In any case, I explained how to install YouCompleteMe plugin in my previous post. Here, I will talk about a few more useful tips.

One of the must-have feature in IDE is perhaps GoTo feature, where one can go to the definition of certain method of variable. YouCompleteMe also provides this feature. Formally, this is mapped with
:YcmCompleter GoTo

We can create a shortcut. Add the following in the ~/.vimrc file
nnoremap <C-P> :YcmCompleter GoTo<CR>

In the source file that YouCompleteMe understands, you can simply move the cursor to the symbol and press <Control> + p key to go to the definition. To go back, you can press <Control> + o key.

Also, add the following to ~/.vimrc file
let g:ycm_python_binary_path = 'python'

This will load the default python first found on PATH environment.

Happy coding!

Friday, February 2, 2018

Three Minutes Daily Vim Tip: Display Indentation Levels

It could be just me, but it is difficult for me to sometimes tell the indentation level of multiple indented lines. So here is a tip for someone like me:

IndentLine is an excellent vim plugin that does it for you. For installation, simply use Vundle; insert the following line in your .vimrc
Plugin 'Yggdroot/indentLine'

If you are not too familiar with Vundle, refer to this post where I explained how to use Vundle to install a different plugin.

Monday, October 2, 2017

Three Minutes Daily Vim Tip: Disable a Key

The letter 'J' in vim is mapped to a shortcut to join two lines. Unfortunately, the letter 'j' is used a lot to navigate, and I often mistakenly press shift along with 'j'. This is quite annoying, so I decided to simply disable this shortcut.

To do this, open up ~/.vimrc file and add the following line:
nnoremap J <nop>

That's it!


Wednesday, July 26, 2017

Three Minutes Daily Vim Tip: Displaying Currently Viewing File

This post is based on jmathew's answer here.

To display which file you are currently looking at in vim, simply insert the following lines in ~/.vimrc file:
set statusline=%F%m%r%<\ %=%l,%v\ [%L]\ %p%%
set laststatus=2


Saturday, July 8, 2017

How to Use Vim on Macbook Pro Touch Bar

I really love my new Macbook Pro 15" model, with just one exception: its touch bar. I have no idea why the heck Apple wants to force everyone to use the touch bar in this model. You see, in the 13" Macbook Pro, you actually have an option to choose either the standard function keys or the touch bar, but with the 15" model, there is no such option.

My big complaints about touch bar should probably go into a separate thread, so I will start right into with my solutions to cope with the touch bar. I map F2 and F3 to work as :tabp and :tabn in Vim, but it just won't work with the touch bar, as there no long is standard function keys. What is even worse is that there is no physical ESC key, which is the most essential key in using Vim.

I actually remap CAPS LOCK to work as ESC key. This is supported by Mac OS X by default. To set this, go to Preferences --> Keyboard --> Modifier Keys --> Caps Lock --> Escape. For standard function key shortcuts, I simply remap ;n to Fn where n could be from 1 to 12. That is, for example, I have the following liens in ~/.vimrc:

nnoremap ;2 :tabp<CR>
nnoremap ;3 :tabn<CR>

for switching between tabs with ;2 and ;3 keystrokes. It takes some time to get used to, but at least this remapping makes touch bar usable.

Python Development with VIM

Just some notes regarding my quest for developing python using vim:

1. Never use anaconda. Most vim plugins will not support it

2. Use virtualenv instead.

3. Install python-mode.

First, run
$  mkdir -p ~/.vim/autoload ~/.vim/bundle && curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

Next, edit ~/.vimrc to have:
execute pathogen#infect()
syntax on
filetype plugin indent on

 
Finally, run
$ cd ~/.vim/bundle && git clone https://github.com/klen/python-mode.git

4. Install ctags.
$ sudo apt-get install ctags

Now I am happy with it for now.

Wednesday, March 22, 2017

How to Install YouCompleteMe Vim Plugin for Linux

***If you are like me, using vim as the main editor for coding, then I highly recommend that you read this post.

 Here, I will go through a step by step guide for installing YouCompleteMe plugin for vim with C-family support, i.e., C/C++/Objective-C/Objective-C++. Detailed instruction is given in its Github repo, but it took me quite a while to figure out how to do this right, so I just think it will be helpful to other people as well.

First, you will need to install Vundle. Run the following:
$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Next, copy and paste the following at the top of ~/.vimrc file:
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'Valloric/YouCompleteMe'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line



Next, install Vundle and YouCompleteMe in vim. To do this, open up vim and install plugins:
$ vim
:PluginInstall

This command within vim will install Vundle and YouCompleteMe plugins directly from the Github repositories, so make sure that you have Internet connection. Wait until it says "Done" at the bottom of vim.

You are not done yet. You will need to install necessary packages to configure YouCompleteMe.
$ sudo apt-get install build-essential cmake python-dev python3-dev

Next, you will need to change directory to where the YouCompleteMe is installed and setup clang-completion for C-family:
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py --clang-completer

This will take a while, so be patient. When this is done, you are done with the installation, but you are not done with C-family auto-completion features just yet. For more info, you will need to read the official documentation regarding the part.

Basically, you will need to let clang know how to compile your project, so that it can suggest auto-completion methods or fields. If your simply want to skip all and see it in action, then create ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py with the following:
def FlagsForFile( filename, **kwargs ):
  return {
    'flags': [ '-x', 'c++', '-Wall', '-Wextra', '-Werror' ],
  }


Also, append the following line to ~/.vimrc
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'

Now, start editing a C++ file in the same directory with vim
$ vim hello.cpp

As you edit, you will see YouCompleteMe in action with auto-completion for C++ functions!

For Python development, I have a dedicated post here too. By the way, you may wish to read here when using Python modules. It explains how to set the python executable path.

Also, if you are a Mac user, you may want to checkout this post by Oliver.


Wednesday, October 26, 2016

Three Minutes Daily Vim Tip: Paste without Strange Indentation Behavior

I may have encountered a strange behavior in vim when you try to paste some codes with system-generic method (i.e., Ctrl+V) rather than vim method (i.e., yy and p). Here is how to fix it:

:set paste

This option will let vim know that you will be pasting text, and vim is to paste the text as is, without trying to auto-indent. When you are done pasting, simply set

:set nopaste

Happy vimming!

Tuesday, October 25, 2016

Three Minutes Daily Vim Tip: Disable F1 Key

<F1> is by default mapped to vim help, but this is rather annoying, especially when I have mapped <F2> and <F3> for switching between tabs. Here is how to disable <F1> key mapping in vim.

Simply add these two lines of codes into ~/.vimrc:
nmap <F1> :echo<CR>
imap <F1> <C-o>:echo<CR>

Note: if you using GNOME, then F1 is snatched by GNOME before vim, in which case you will likely get some help menu even if you have disabled F1 from vim. IN this case, simply disable F1 from the terminal --> preferences --> shortcuts --> help --> backspace.

Thursday, October 20, 2016

Three Minutes Daily Vim Tip: Setting to Preloaded Colors

Vim comes with some color schemes. It will save some hassle if you can find one that looks good. Here is how to try them!

First, locate pre-loaded colors. Usually the files are in /usr/share/vim/vim73/colors/ directory, assuming you have vim7.4. Try
$ find / -name desert.vim 2>/dev/null

/usr/share/vim/vim73/colors/desert.vim

Now you can search the directory
$ ls /usr/share/vim/vim73/colors/*.vim
/usr/share/vim/vim73/colors/blue.vim
/usr/share/vim/vim73/colors/darkblue.vim
/usr/share/vim/vim73/colors/default.vim
/usr/share/vim/vim73/colors/delek.vim
/usr/share/vim/vim73/colors/desert.vim
/usr/share/vim/vim73/colors/elflord.vim
/usr/share/vim/vim73/colors/evening.vim
/usr/share/vim/vim73/colors/koehler.vim
/usr/share/vim/vim73/colors/morning.vim
/usr/share/vim/vim73/colors/murphy.vim
/usr/share/vim/vim73/colors/pablo.vim
/usr/share/vim/vim73/colors/peachpuff.vim
/usr/share/vim/vim73/colors/ron.vim
/usr/share/vim/vim73/colors/shine.vim
/usr/share/vim/vim73/colors/slate.vim
/usr/share/vim/vim73/colors/torte.vim
/usr/share/vim/vim73/colors/zellner.vim

In vim, set the color by
:color desert

To set it as your default color, simply do
$ echo ":color desert" >> ~/.vimrc

Enjoy vim!

Thursday, September 29, 2016

Three Minutes Daily Vim Tip: Tab Completion

If you want to enable tab-completion feature in vim, just like terminal, here is what you can do.

$ echo "set wildmode=longest,list,full" >> ~/.vimrc
$ echo "set wildmenu" >> ~/.vimrc

Now, when you are opening a new file, use tab key to see the files in the folder:
:e <tab>

Tuesday, September 20, 2016

Three Minutes Daily Vim Tip: Indentation

Say you want to indent a block of code inside curly brackets. There are a couple of ways to do this. Before we run the commands, make sure to place your cursor inside the block of curly brackets.

>i{
>i}

The two commands above will indent the code inside the curly brackets. Specifically > indicates indentation, i indicates inside, and { or } indicates curly brackets.

You could of course make small manipulations, for example use < for de-indentation, and use any one of (){}[]<> after i for corresponding types of brackets/parenthesis.

Another way is to use vim's re-indentation feature, which will automatically adjust indentation by its own. Again, you will need to make sure to place the cursor within the block to run the following:

=i{

The first character = indicates re-indentation, and the rest should be identical to the previous context.

Finally, you may also re-indent the entire file by

gg=G

where gg places your cursor to the beginning of the file, and G indicates all the lines till the end of the file.

Thursday, September 1, 2016

Three Minutes Daily Vim Tip: Ctags

When you are dealing with a large code base, you definitely want to jump back and forth within the code. Let's assume that you are looking at OpenCV library source code. To find functions and jump back and forth between functions and files in Vim, one can use ctags:

First, install ctags
$ sudo apt-get install ctags

Next, create the tags for the source directory
$ cd ~/opencv-3.1.0
$ ctags -R

Finally, use Vim to search for a specific tag
$ vim -t Stitcher

The above command looks for a function, class, or variable named that contains the word Stitcher. There should only be one return, and Vim will open up stitching.hpp file and point to the class Stitcher.

To view the list of previously found tag, run the following within Vim
:ts

To search for tag blend, run
:ts blend

To go to the next or previous tag result in the list, run
:tn
:tp


Now this is where the fun begins! To jump to the tag currently pointed by the cursor, press
<ctrl> + ]

To go back to the caller tag, press
<ctrl> + t

To go back to the previous position, press
<ctrl> + o

Happy Vimming!

Saturday, August 27, 2016

Three Minutes Daily Vim Tip: Open Specific Location

Say you are compiling your code, and the compiler warns you with error in line 77 of your main.c file. You want to go to this specific line of this file. You could do this in two steps, i.e., open up the file and go to the line.

$ vim main.c
:77

Well, there is a a bit easier way to do this in one command:
$ vim +77 main.c

This command will open up main.c file at line 77.

Next, say you want to open up the file and go to your function main(). To do this, you would normally do
$ vim main.c
/main(

Again, there is a single command to do this:
$ vim +/main\( main.c

Note the presence of backslash \, which will tell the shell to input literal parenthesis ( character.

Happy Vimming!

Thursday, August 18, 2016

Three Minutes Daily Vim Tip: Split Views

To open up a split view, type in
:sp file_to_open.c
:vsp file_to_open.c
The first will open up a horizontal split view, while the latter will open up a vertical one.

To move your cursor between views, type in
<Ctrl> [w] + [h]
<Ctrl> [w] + [j]
<Ctrl> [w] + [k]
<Ctrl> [w] + [l]
where h,j,k,l will move into the corresponding directions, i.e., left, down, up, and right.

To change size automatically, type in
<Ctrl> [w] + [=]

For manual size manipulation, type in
:resize 60
:vertical resize 60
where the first will move the horizontal divider, while the second will move the vertical divider.

To quit all views at once, type in
:wqa
:qa!
where the first will save and exit all views, while the latter will quit all views without saving.

Lastly, for easy navigation between split views, append the following into ~/.vimrc file:
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

You should now be able to navigate between split views with
<Ctrl> [h]
<Ctrl> [j]
<Ctrl> [k]
<Ctrl> [l]

Wednesday, August 17, 2016

Three Minutes Daily Vim Tip: Tabs

Vim is a controversial editor: some love it, while some hate it. Vim is indeed difficult at first, requiring very steep learning curve. In order to help people, including myself, learn Vim, I am going to post a series of short Vim tips. Today is the first each the series. If you are clueless with Vim, learn the very basics first.

Starting Vim 7.0, it supports tabs. In Vim's normal mode, type in
:tabe some_file_to_open.c

You will immediately notice that the file is opened up in a new tab. To switch between tabs, type in
:tabn
:tabp

I have to admit that tab switching command is too long. Well, it's Vim: we can always create a one-key shortcut.

$ echo "nnoremap <F2> :tabp<CR>" >> ~/.vimrc
$ echo "nnoremap <F3> :tabn<CR>" >> ~/.vimrc

The two lines will map <F2> and <F3> keys for switching to previous and next tab, respectively.

Saturday, April 2, 2016

How to Use SUDO in Debian

If you are a long time Ubuntu user who wants to try out Debian for fun, like myself, you may encounter
$ sudo apt-get install vim
-bash: sudo: command not found

This is because by default Debian doesn't come with sudo package. What you will need to do then is to use su command
$ su
Password: [root password]

You should now be able to login as root. Let's install sudo package first
# apt-get install sudo -y

Looks all good. Let's log out root and try sudo again
# exit
exit
$ sudo apt-get install vim
[sudo] password for xxx:
xxx is not in the sudoers file.  This incident will be reported.

OK this is a bit scary. The incident has been logged to/var/spool/mail/xxx file.
Well, how do we then add a user to become a sudoer? First, login as root again and then you run the following
$ su
# adduser xxx sudo
where xxx is the username that you'd like to add to the sudo group.

When done, you will need to re-login with xxx in order for this to take effect. Let's just reboot the system.
# reboot

After the reboot, you should be able to now type in sudo command to do administrative tasks
$ sudo apt-get install vim

By the way, in case you are wondering how to disable local root login in Debian system, just like Ubuntu, this is what you'll need to run the following command as a sudo user
$ sudo passwd -dl root

To enable root account, you will need to
$ sudo passwd root

Saturday, October 10, 2015

Enable vim Coloring, Auto Indentation, Line Numbering, Search Highlighting

If you are using vim as your main editor, you may like to enable some syntax coloring and auto indentation when programming. Here is how you can do it.


Install vim on Ubuntu first. Ubuntu only ships with vi, not vim, so install vim
$ sudo apt-get install vim


Next, set the vim preference so that it loads up every time vim is executed
$ vim ~/.vimrc
This will open up the .vimrc file on your home directory that will be executed whenever vim is loaded up.


To edit the file, press i keystroke, which will switch to the insert mode. Write out
syntax on 
set nu
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set hlsearch


To save, press ESC keystroke, and enter
:wq
which will write (w) the file and quit (q).


That's it. syntax on turn on syntax coloring, set nu will turn on line numbering, and the last line filetype plugin indent on will turn on automatic indentation, which is quite handy when programming.


Let's write helloworld program in c using vim
Create helloworld.c file in the home directory by running the following commands in terminal
$ cd ~
$ vim helloworld.c


When the vim opens up, press i keystroke again to switch to insert mode
Enter the c code
#include <stdio.h>
int main () {
     printf("Hello World!\n");
     return 0;
}
Notice the auto indentation feature!



Again, press ESC key, and enter the following to save and quit vim
:wq


Let's compile using gcc and run it
$ gcc helloworld.c
$ ./a.out


Great to see it works well. Note that the default compiled binary from gcc is a.out. In order to execute the file in the current directory, you simply enter.
$ ./<binary_file_name>


** If you are a Mac OS X user ***
To install vim on Mac OS X, you will first install homebrew and run
$ brew install vim

Also, if you are using a non-Apple keyboard, you probably want to change the configuration of the backspace by appending the following line to ~/.vimrc
set backspace=indent,eol,start