If we want to store the line numbers of highlighted words along with their corresponding lines in gvim (similar to :g/), we can add the following code to ~/.gvimrc. Then, use :F to open the [Scratch] File.
command! -nargs=? F let @a='' | execute 'redir @a | silent g//print' | redir END | silent execute 'new | setlocal bt=nofile | put=split(@a, ''\n'') | set syntax=vera | 1d'
command! -nargs=? F: This defines a custom command named "F" that can take an optional argument (-nargs=? specifies that the command can take zero or more arguments).
let @a='': This clears the content of register "a". The register "@" is commonly used for yanking and pasting text in Vim.
execute 'redir @a | silent g//print': This part of the command executes the :global command (g//print) with the provided argument(s). The :global command is used to perform an operation on lines that match a given pattern. In this case, it's searching for lines that match the specified pattern (given by ) and printing them. The redir @a part redirects the output of the command to register "a".
redir END: This stops the redirection of output, ending the :redir command started previously. It ensures that subsequent output is not redirected.
silent execute 'new | setlocal bt=nofile | put=split(@a, ''\n'') | set syntax=vera | 1d': This executes a series of commands silently:
new: This opens a new buffer.
setlocal bt=nofile: This sets the buffer type (bt) to "nofile", which means it's not associated with any file.
put=split(@a, '\n'): This splits the content of register "a" into lines and puts it into the buffer. The split() function is used to split a string into a list based on a delimiter ('\n' represents a newline character).
set syntax=vera: This sets the syntax highlighting of the buffer to "vera".
1d: This deletes the first line of the buffer.
So, in summary, the command searches for lines matching a pattern, captures those lines, opens a new scratch buffer, populates it with the captured lines, sets the syntax highlighting, and then deletes the first line (which typically contains the pattern used for the search).
grep
just use/pattern
and then traverse it forward pressingN
or backward pressingshift+N
– Opposition:g//p#
to see grep like output for the whole buffer – Expand:g/pid/ normal >>
will indent these lines,:v/pid/d
deletes all other lines; – Expand