Vim commands: Difference between revisions

From wikinotes
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Documentation =
<blockquote>
{| class="wikitable"
| <code>:h cmdline</code> || https://neovim.io/doc/user/cmdline.html
|-
| <code>:h wildmenu</code> || https://neovim.io/doc/user/options.html#'wildmenu'
|-
| <code>:h index</code> (builtin commands) || https://vimhelp.org/index.txt.html#index
|-
| <code>:map</code> (user/plugin commands) || n/a
|}
</blockquote><!-- Documentation -->
= Command Mode =
= Command Mode =
<blockquote>
<blockquote>
Line 17: Line 30:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Command Mode -->
</blockquote><!-- Command Mode -->
= Command Completion =
<blockquote>
Command mode completion controlled by the <code>wildmenu</code>.<br>
See <code>:h wildmenu</code>.
Hotkeys
<syntaxhighlight lang="vim">
CTRL-Y, SPACE  " accept completion
CTRL-E          " abort completion
CTRL-N, RIGHT  " prev completion
CTRL-P, LEFT    " next completion
" if completing filepaths
UP              " complete parent dir
DOWN            " complete child dirs
</syntaxhighlight>
Configuration
<syntaxhighlight lang="vim">
set ignorecase  " ignore case when completing vim-commands
</syntaxhighlight>
</blockquote><!-- Command Completion -->


= Commands =
= Commands =
<blockquote>
== Search ==
<blockquote>
<blockquote>
== vim[grep] ==
== vim[grep] ==
Line 37: Line 76:
grep lets you search with the cli grep (faster).
grep lets you search with the cli grep (faster).
<syntaxhighlight lang="vim">
<syntaxhighlight lang="vim">
:grep searchterm **/*.md                                     " search files glob-matched
:grep searchterm **/*.md                                   " search files glob-matched
:grep searchterm `find . -type f '*.rb' -not -path 'test/*'` " search files provided by `find`
:grep searchterm `find . -type f '*.rb' -not -path 'test/*'` " search files provided by `find`
</syntaxhighlight>
</syntaxhighlight>


use cli grep to create reports
Or directly cli grep to create repors (cannot jump results)
<syntaxhighlight lang="vim">
<syntaxhighlight lang="vim">
:!grep '^[a-zA-Z].*{' % | awk -F' ' '{ print $1 }' | sort | uniq
:!grep '^[a-zA-Z].*{' % | awk -F' ' '{ print $1 }' | sort | uniq
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- grep -->
</blockquote><!-- grep -->
== cfdo ==
<blockquote>
<code>cfdo</code> will execute a command on all files in the quickfix list (<code>:copen</code>).
<syntaxhighlight lang="vim">
" cfdo execute in each file in quickfix
" (ex: search-replace all sourcefiles in project)
:grep foo
:cfdo %s/foo/bar/g | write
" cdo execute for each match in quickfix
" (ex: unfold all modules in ruby file)
vim '\(module \|class \)' % | cdo :normal zo
</syntaxhighlight>
</blockquote><!-- cfdo -->
</blockquote><!-- Search -->
</blockquote><!-- Commands -->
</blockquote><!-- Commands -->

Latest revision as of 17:18, 18 February 2023

Documentation

:h cmdline https://neovim.io/doc/user/cmdline.html
:h wildmenu https://neovim.io/doc/user/options.html#'wildmenu'
:h index (builtin commands) https://vimhelp.org/index.txt.html#index
:map (user/plugin commands) n/a

Command Mode

  • : enters the command mode interpreter
  • ctrl-f from within command mode opens command/history in a buffer
:                       "enter commandmode
<c-f>                   "edit command in buffer (w/ vim navigation etc)

!<command>              "run shell command
:<command>:<command>    "each command-mode command is prefixed by a ':'
:%s/search/replace/gc   "search/replace text in entire file (confirming before replacing for each occurrence)
:%s/search/replace/g    "search/replace text in entire file
0-9<command>            "prefix a command with a number to perform command N times
u                       "undo
<c-r>                   "redo

Command Completion

Command mode completion controlled by the wildmenu.
See :h wildmenu.

Hotkeys

CTRL-Y, SPACE   " accept completion
CTRL-E          " abort completion

CTRL-N, RIGHT   " prev completion
CTRL-P, LEFT    " next completion

" if completing filepaths
UP              " complete parent dir
DOWN            " complete child dirs

Configuration

set ignorecase  " ignore case when completing vim-commands

Commands

Search

vim[grep]

vimgrep lets you search files

:vim[grep] test %   " find all occurrences of 'test' in current file (%)

:copen              " list occurrences
:cnext              " jump to next occurrence
:cprev              " jump to prev occurrence

grep

grep lets you search with the cli grep (faster).

:grep searchterm **/*.md                                    " search files glob-matched
:grep searchterm `find . -type f '*.rb' -not -path 'test/*'` " search files provided by `find`

Or directly cli grep to create repors (cannot jump results)

:!grep '^[a-zA-Z].*{' % | awk -F' ' '{ print $1 }' | sort | uniq

cfdo

cfdo will execute a command on all files in the quickfix list (:copen).

" cfdo execute in each file in quickfix
" (ex: search-replace all sourcefiles in project)
:grep foo
:cfdo %s/foo/bar/g | write

" cdo execute for each match in quickfix
" (ex: unfold all modules in ruby file)
vim '\(module \|class \)' % | cdo :normal zo