Nvim :help
pages, generated
from source
using the tree-sitter-vimdoc parser.
:%s/four/4/gThe "%" range means to replace in all lines. The "g" flag at the end causes all words in a line to be replaced. This will not do the right thing if your file also contains "thirtyfour". It would be replaced with "thirty4". To avoid this, use the "\<" item to match the start of a word:
:%s/\<four/4/gObviously, this still goes wrong on "fourteen". Use "\>" to match the end of a word:
:%s/\<four\>/4/gIf you are programming, you might want to replace "four" in comments, but not in the code. Since this is difficult to specify, add the "c" flag to have the substitute command prompt you for each replacement:
:%s/\<four\>/4/gc
*.cpp
Start Vim, defining the argument list to
contain all the C++ files. You are now in the
first file.
qq Start recording into the q register
:%s/\<GetResp\>/GetAnswer/g
Do the replacements in the first file.
:wnext Write this file and move to the next one.
q Stop recording.
@q Execute the q register. This will replay the
substitution and ":wnext". You can verify
that this doesn't produce an error message.
999@q Execute the q register on the remaining files.:%s/\<GetResp\>/GetAnswer/geThe "e" flag tells ":substitute" that not finding a match is not an error.
:%s/\([^,]*\), \(.*\)/\2 \1/Let's break this down in parts. Obviously it starts with a substitute command. The "%" is the line range, which stands for the whole file. Thus the substitution is done in every line in the file. The arguments for the substitute command are "/from/to/". The slashes separate the "from" pattern and the "to" string. This is what the "from" pattern contains:
/^OBJS j :.,/^$/-1!sortThis goes to the first line, where "OBJS" is the first thing in the line. Then it goes one line down and filters the lines until the next empty line. You could also select the lines in Visual mode and then use "!sort". That's easier to type, but more work when there are many lines. The result is this:
:global/^/move 0Abbreviated:
:g/^/m 0The "^" regular expression matches the beginning of the line (even if the line is blank). The :move command moves the matching line to after the imaginary zeroth line, so the current matching line becomes the first line of the file. As the :global command is not confused by the changing line numbering, :global proceeds to match all remaining lines of the file and puts each as the first.
:'t+1,.g/^/m 't
g CTRL-GDo not type a space after the g, this is just used here to make the command easy to read. The output looks like this:
CTRL-G
", move to the end of the text, type "g CTRL-G
" again, and then
use your brain to compute the difference in the word position. That's a good
exercise, but there is an easier way. With Visual mode, select the text you
want to count words in. Then type g CTRL-G
. The result:KNvim will run :Man on the word. If the man page is found, it is displayed. You can also use the :Man command to open a window on a man page:
:Man cshYou can scroll around and the text is highlighted. This allows you to find the help you were looking for. Use
CTRL-W
w to jump to the window with the
text you were working on.
To find a man page in a specific section, put the section number first.
For example, to look in section 3 for "echo"::Man 3 echoTo jump to another man page, which is in the text with the typical form "word(1)", press
CTRL-]
on it. Further ":Man" commands will use the same
window.KFor example, you want to know the return value of "strstr()" while editing this line:
:%s/\s\+$//The line range "%" is used, thus this works on the whole file. The pattern that the ":substitute" command matches with is "\s\+$". This finds white space characters (\s), 1 or more of them (\+), before the end-of-line ($). Later will be explained how you write patterns like this, see usr_27.txt. The "to" part of the substitute command is empty: "//". Thus it replaces with nothing, effectively deleting the matched white space.
/You cannot see it, but there is a space before a tab in this command. Thus it's "/<Space><Tab>". Now use "x" to delete the space and check that the amount of white space doesn't change. You might have to insert a tab if it does change. Type "n" to find the next match. Repeat this until no more matches can be found.
vim `grep -l frame_counter *.c`Let's look at this command in detail. The grep command searches through a set of files for a given word. Because the -l argument is specified, the command will only list the files containing the word and not print the matching lines. The word it is searching for is "frame_counter". Actually, this can be any regular expression. (Note: What grep uses for regular expressions is not exactly the same as what Vim uses.) The entire command is enclosed in backticks (). This tells the Unix shell to run this command and pretend that the results were typed on the command line. So what happens is that the grep command is run and produces a list of files, these files are put on the Vim command line. This results in Vim editing the file list that is the output of grep. You can then use commands like ":next" and ":first" to browse through the files.
:grep error_string *.cThis causes Vim to search for the string "error_string" in all the specified files (
*.c
). The editor will now open the first file where a match is found
and position the cursor on the first matching line. To go to the next
matching line (no matter in what file it is), use the ":cnext" command. To go
to the previous match, use the ":cprev" command. Use ":clist" to see all the
matches and where they are.
The ":grep" command uses the external commands grep (on Unix) or findstr
(on Windows). You can change this by setting the option 'grepprg'.