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/#
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' |
:global
commandWe can search for a pattern in the current file and list out the matching lines using the powerful global
command:
:g[lobal]/pattern/#
" 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 |
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.
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.
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):
:g
command in Vim to behave in a more useful way.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.
Initialize a git repository inside your .vim
directory, add everything (including the vimrc), commit and push to a GitHub/BitBucket/GitLab repository:
cd ~/.vim
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