:0r! grep -rn $pattern $path_to_directory
or any other command that outputs lines containing "$path_to_file:$line_no" (eg most linters).With that output read into a buffer, placing the cursor anywhere in the "path:line" part and hitting ^wF will open that file in a new split window, and jump the cursor straight to the indicated line - or you can do ^wgF to do the same in a new tab page.
It's a very handy way to work through a big list of work items spread across many files. You do need to coax the right format for it out of your tools, but fortunately "path:line" is pretty de-facto standard.
(Incidentally, I do ":0r" so it inserts the read-in lines at the very top of the file - just ":r" in an empty buffer leads to a leading blank line, which I find irrationally annoying.)
The trick I described is more for when you're dealing with some unusual program that wouldn't work with :grep - at least not without fiddling with 'grepprg' to invoke it, and/or 'grepformat' to correctly interpret its output.
:cfile
:cbuffer
one can turn that file/buffer directly into a quickfix list, where simply enter will take one to the relevant position. Messages with column positions are recognized in quickfix lists (:help errorformat). :cex system('git grep -n foo | grep bar') | copen
then :cn and :cp to step through the list easily.I don't find myself using ! commands when I know there are multiple commands that I need to run (like your last example). That's usually when I'll suspend and then use the command line.
Could you elaborate on "wreak havoc on your vim session?" I wasn't aware that suspending the process and bringing it back into the fg would harm anything.
You can even use :w in combination with !. For example
:w !xclip
to write the vim buffer contents to the clipboard. You could visually highlight part of the buffer and do the same thing for just the text you want. "*y
"+yOne advantage of the method you describe is that it can be limited to just part of a line. Piping output to xclip is limited to complete lines of text due to the nature of :w !.
That's when I run !bash, enter my list of commands, then exit.
:shell1G (to go to top of file if not already there)
!Gsort (to filter all the lines in the file through the sort command, and replace the file contents with the sorted lines)
which says: filter all the lines would be traversed over as a result of running the G command (which goes to the last line in the file) through the sort command, and use the output of the sort to replace all the lines that were filtered;
sort can be replaced with any other command that is a filter, e.g. fmt, grep, sed, many more;
(note: no need of colon in the above command, just have to be in command mode, not input mode, which you get into by pressing Esc).
Very powerful, just that feature, particularly in programming.
have a look at :cw[indow]
:r !git status -v
And at the to of the result, type my formatted git commit message, visually highlight the message I just typed, and then use the following vim command :r !git commit -F -
Which reads the commit message from standard input.Try emacs with magit. It's really great.
vim can be thought of as a visual and interactive helper for your text-processing utilities. The "intended scope" of vim is text editing, and while you're editing you may want to run commands on the text in your buffer, or just parts of it, and have the results apply immediately without writing and reloading the file, or exiting.
For example, in vim, you may be writing a list somewhere in your document, and you realize you want it sorted alphabetically. You can visually select the range of lines ('<,'>) that comprise the list and then sort them by calling the sort utility [1]:
:'<,'>!sort
And then you can continue adding to the list.What would your workflow be for the above? Some like this...?
1. write the file you are working on in your EDITOR
2. make a mental note of the absolute line numbers for the list in the document
3. open new terminal
4. call an awk command that uses `sort`, or some other script, passing in your line numbers, and the file name
5. switch back to EDITOR and reload file
----
[1] Of course vim has its own `sort` command, so you can leave out the '!' in that example to do the same.
vi did not have its own `sort`, so people used the external `sort`. vi also didn't have the visual selection, so people could pick lines using ranges:
:.,+10!sort
i.e. sort from the current line to the line 10 lines down, inclusiveOne interesting feature many people may not know about is that it's possible to address lines using a regular expression.
For instance, if you had the following text in a vim buffer:
one
two
three
four
five
and you wanted to delete the lines between "two" and "four" inclusive, you could run the following command in vim: :/two/,/four/d
You could also do the same thing in sed sed '/two/,/four/d' filenameIMO, :make is (usually) better, because it adds errors to the quickfix list, and put your cursor on the first error location.
https://vi.stackexchange.com/questions/6679/terminal-setup-w...
For example, when reading xml I use xmllint to clean it up:
:%!xmllint --format -I am an avid emacs user though. I really should check out vim.
For that reason I just jump out with a ctrl+z and when I'm done I'm back in with a fg!
:make
nowadays rather than :!makeIs there a difference?
For example
will execute ls and show you the result (press enter to return) will read the result of ls in for youMore usefully
will read lines 5-10 from that other file.However you will most often want to