Skip to content

Instantly share code, notes, and snippets.

@manasthakur
manasthakur / search-no-comments.vim
Last active Jan 13, 2018
Search ignoring commented text
View search-no-comments.vim
" Function to skip commented text while searching
function! SearchNoComments(pattern)
let l:matchpos = getpos('.')
let l:fileOver = []
while search(a:pattern, 'w') > 0
if l:fileOver == []
let l:fileOver = getpos('.')
elseif l:fileOver == getpos('.')
" Break if we are back to where we started
break
@manasthakur
manasthakur / vim.md
Created Aug 7, 2017
A curated list of my Vim plugins and gists
View vim.md
@manasthakur
manasthakur / solus.md
Last active Feb 4, 2018
Solus, I owe you one!
View solus.md

Solus, I owe you one!

I am a relatively experienced Linux user -- started with Ubuntu 8.04 LTS in late 2009 as part of the labs in my undergrad institute. Various technology magazines made me a distro hopper in 2011.

Eight years with Ubuntu

When I stabilized back to Ubuntu, I started getting familiar with bash-script, learnt building unavailable packages, and became an ardent Vimmer. In 2012 I ditched Windows completely and started using Ubuntu 10.04 as the standalone OS on my Dell Studio 1450 laptop. That was a liberating experience -- no startup errors, superfast bootup, no antivirus scans -- awesome! When I got my new Lenovo laptop in 2014, I vowed to always keep my machine a Linux box.

@manasthakur
manasthakur / better-global-searches.md
Last active Mar 26, 2017
Hassle-free buffer-local searches in vim
View better-global-searches.md

Listing pattern-matches in the current file

Option 1: The :global command

We can search for a pattern in the current file and list out the matching lines using the powerful global command:

:g[lobal]/pattern/#
@manasthakur
manasthakur / latex-fold.vim
Last active Mar 20, 2017
Expression-based folding for latex files in vim
View latex-fold.vim
function! MyFoldLevel(lnum)
let cur_line = getline(a:lnum)
" Fold sections at level 1
if cur_line =~ '^\s*\\section'
return '>1'
endif
" Fold subsections at level 2
if cur_line =~ '^\s*\\subsection'
@manasthakur
manasthakur / submodules.md
Last active Apr 19, 2021
Using git submodules to version-control Vim plugins
View submodules.md

Using git-submodules to version-control Vim plugins

If you work across many computers (and even otherwise!), it's a good idea to keep a copy of your setup on the cloud, preferably in a git repository, and clone it on another machine when you need. Thus, you should keep the .vim directory along with your .vimrc version-controlled.

But when you have plugins installed inside .vim/bundle (if you use pathogen), or inside .vim/pack (if you use Vim 8's packages), keeping a copy where you want to be able to update the plugins (individual git repositories), as well as your vim-configuration as a whole, requires you to use git submodules.

Creating the repository

Initialize a git repository inside your .vim directory, add everything (including the vimrc), commit and push to a GitHub/BitBucket/GitLab repository:

cd ~/.vim
@manasthakur
manasthakur / plugins.md
Last active Nov 26, 2021
Managing plugins in Vim
View plugins.md

Managing plugins in Vim: The basics

Let's say the plugin is at a GitHub URL https://github.com/manasthakur/foo. First get the plugin by either cloning it (git clone https://github.com/manasthakur.foo.git) or simply downloading it as a zip (from its GitHub page).

Adding a plugin in Vim is equivalent to adding the plugin's code properly into its runtimepath (includes the $HOME/.vim directory by default). For example, if the layout of a plugin foo is as follows:

foo/autoload/foo.vim
foo/plugin/foo.vim
@manasthakur
manasthakur / grepping.md
Last active Jul 30, 2021
Vim: Creating your own ack.vim/ag.vim
View grepping.md

Creating your own ag.vim

Vim provides built-in mechanisms to search through projects in the form of the grep command. However, on large projects, grep is known to be slow; and hence people have been switching to simpler searchers like ack, and faster, parallel (metal?) searchers like ag and pt. Correspondingly, several plugins have been created that integrate these tools in vim: ack.vim, ag.vim, etc.

However, it's actually very easy to get the functionalities these plugins provide (faster search, results in quickfix-window, jumps, previews, and so on) in vanilla Vim itself; in fact, Vim already populates the grep-search results in a quickfix window. We just need to tell Vim to do the following things (use-case: ag):

  • Use ag as the default grep program
  • Open quickfix window by default
  • Create mappin