11

If any of the globs has no match, in zsh, the command will be aborted. This means you need to escape your globs. I suggest you use shellescape() to build your 'grepprg' command. let &grepprg='grep -n -R --exclude=' . shellescape(&wildignore) . ' $*' For more help see: :h 'grepprg' :h shellescape() man zshexpn Alternatives to grep Have you ...


7

From :help todo: Substitute with hex/unicode number "\%xff" and "\%uabcd". Just like "\%uabcd" in search pattern. In other words, this isn't implemented yet. You can do it interactively, in Vim. See :help 24.8. Type :%s/ /, then type Ctrl-V, then x19, then press Enter.


7

There is a better way of accomplishing the task you showed in ex mode, which is by using the :delete command and a basic cmdline-range. To delete all lines between and including the beginning of the file and the search match, you can do this: 1,/dd/delete If you don't want to include the matched line in the deletion, you can do this 1,/dd/-1delete The ...


7

You can use <C-b> to go to the beginning of the line; from :help c_CTRL-B: CTRL-B or <Home> c_CTRL-B c_<Home> c_Home cursor to beginning of command-line If you want, you can use this to map it to <C-a>: :cnoremap <C-a> <C-b> Related: How can I use Readline shortcuts in the ...


6

If you want bulletproof, you probably want something a little more portable than the complicated options above. I would stick to POSIX features of ex and make it stupid-simple: Just remove the line if it's there (regardless of how many times it's there), add it to the end of the file, then save and exit: ex -sc 'g/^export PATH=\~\/\.composer\/vendor\/bin:\$...


6

The commands are executed serially, which means that before the 4pu_ command is executed, a newline has already been added and your previous line 4 is now line 5. There are at least three ways to solve this: Start from the back. This way, previous changes do not affect later changes. As suggested by Christian Brabrandt, you can add newlines based on a ...


5

:help E30 states The command-line is only stored in this register when at least one character of it was typed. Thus it remains unchanged if the command was completed from a mapping. It seems like Vim is not considering commands specified via + (or -c) to be typed, so ": isn't updated. I'm not sure if that's expected behavior or just an oversight.


5

Update: The easiest portable way to do this is: printf '%s\n' %s/e//g x | ex file This avoids error conditions discussed below. Original Answer One of the advantages of ex as opposed to Vim is that ex is guaranteed by POSIX. If you're in the sysadmin world and you need to script automated edits to text files (e.g. configuration files to be edited ...


5

You can use :v, e.g. something like this should work: :v/^Foo/s/.*=\zs\d$/0/g


4

An "ex command" means anything that you type in the commandline after the :, such as :write, it doesn't really have anything to do with "ex mode", as such. ex mode is a "mode" where you only have the commandline, and not the graphical editor ("vi" stands for "visual ex mode"). So it's really as simple as: vim -c ':%s/e//g' -c ':wq' file.txt There's no ...


3

Depending on your configuration you can achieve the desired result with: gq If you want to use this as a ex command you can use: normal! gqq to format the current line. Aslo, it's worth adding the flags a and c to your 'formatoption' configuration: set fo+=ac This will try to preserve the current comment formatting as well as automatically formatting ...


3

You don't need a plugin for that, the functionality is built into Vim: function! RemoveAllBreakPoints() let view = winsaveview() write g/\v^\s+(import pdb|pdb.set_trace)/d call winrestview(view) endfunction Reference: :help winsaveview() :help winrestview()


3

Here is a workaround: ex -sc '1,-10d|x' f.txt 1,-10 select all but last 10 lines d delete x save and close


3

Here is a solution which comes from here. Note that might not be exactly what you're looking for since it's implies to use a visual (or normal) mode mapping instead of just an ex command. The idea is to create the following mapping which will allow you to keep in your buffer only the visually selected lines: vnoremap <key> :<C-U>1,'<-1:...


3

There are some problems here. First ex is made for interactive use. You can't use several -c commands. Second, and this is a specialty of the :g command, you can't separate several commands using the '|'. A :g/127/d|wq command would write and quit after the first match) This makes it effectively useless to add another command after the -c command. What works ...


3

I imagine there must be an easier way. Since @: is accessing a special register, you can use registers yourself: $ vim -Nesc ":let @a = '/_exclude/s///p'" +@a +@a +wq foo features[rules_config][] = rule1 features[rules_config][] = rule2


3

Vim In Vim you can match any character including newline with \_.. You can use this to construct a pattern that matches a whole line, any amount of stuff, and then that same line: /\(^.*$\)\_.*\n\1$/ Now you want to delete all lines in a file that match the first, not including the first. The substitution to delete the last line that matches the first is: ...


3

but I don't have the option of piping the output to an external command Actually you do: echo system('wc -l', execute('g/http/')) However, note that execute() prepends a newline to the resulting string, while not appending a newline after it. Hence the result of wc -l remains the same, but it could matter under different circumstances.


3

I'm not aware of any builtin, compact functionality that let's you filter the output of any vim command through any external command. With a little one-time scripting, though, you could get something like this... :MyFilt g/http/ | wc -l To start, put this in your vimrc... func! MyFilt(cmd) let parts = split(a:cmd, '|') let vimcmd = trim(parts[0]) ...


2

A sequence of normal mode commands aborts as soon as one command (e.g. a search) fails. So, if the n jump to the next match fails (with Pattern not found: <h1>), the following vitd isn't executed. That's your condition, and it works for me that way (using Vim in Ex mode via vim -e). Note that you have another bug in your script: By first searching for ...


2

A possible solution is to create a function to do that: function! AddLine() let l:res = 0 g/export PATH=\~\/.composer\/vendor\/bin:\\\$PATH/let l:res = l:res+1 if (l:res == 0) normal G normal oexport PATH=~/.composer/vendor/bin:\$PATH endif endfunction First we create a local variable l:res which will be used to count the ...


2

Try: ex ~/.bashrc -c "if search('export PATH=\~\/.composer\/vendor\/bin:\$PATH')>0 | norm quit | endif | norm Aexport PATH=~/.composer/vendor/bin:$PATH" Note that :append won't work inside if endif-block with Ex mode (see VimDoc) so I have to put norm A... outside.


2

It would appear that the only POSIX way to do this is to use an external filter, such as sed. For example, to delete the 17th line of your file only if it is exactly identical to the 5th line, and otherwise leave it unchanged, you can do the following: :1,17!sed '5h;17{G;/^\(.*\)\n\1$/d;s/\n.*$//;}' (You could run sed on the entire buffer here, or you ...


2

Yes you can do this easily. You just have to use an expression in the replacement part (see the help at :h sub-replace-expression link). So you would do it like this: ex -sc '%s/ /\="\x19"/g|x' hello.txt od -c hello.txt 0000000 h e l l o 031 w o r l d \n See the help at :h expr-quote link


2

append only supports a single line address, not a range. Here is a workaround with global: ex -sc 'g/^/a|BRAVO' -cx alfa.txt and with substitute: ex -sc '%s/$/\rBRAVO/|x' alfa.txt


2

muru’s comment lead me to the answer. According to the docs, argdo requires the listcmds feature at compile time. It appears I do not have it: $ ex --version | grep -e Features -e listcmds Small version without GUI. Features included (+) or not (-): -cursorshape -listcmds -reltime +windows Here is a workaround: for b in *.txt do ex -...


2

In the following range, the offset is calculated from "the current line" which may or may not be a match for /foo: :/foo/,+23# You can use ; to make the first line matching foo the first line specifier of your range: :/foo/;.,+23# which gives you three line specifiers. You can use more semicolons to refine your range: :/foo/;/bar/;/baz/#


2

Almost right below your quote from documentation it also says: When separated with ';' the cursor position will be set to that line before interpreting the next line specifier. So while there seems to be no commands that are able to accept more than two specifiers, they still can be useful to adjust starting position of other specifiers. Example ...


1

You need to enable line numbering by calling :set nu $ printf '%s\n' 'set nu' '%s/Quux/Foo/g' %p 'q!' | ex file.txt 1 Foo Bar 2 Foo Foo Bar Bar 4 Bar Bar Bar 5 Foo Foo


1

Here is a workaround: ex -sc '%s/\v(.*\n){2}/> &/|x' alfa.txt % select all lines s substitute \v turn on magic x save and close


Only top voted, non community-wiki answers of a minimum length are eligible