Sato Katsura
  • Member for 6 years, 7 months
  • Last seen more than a month ago
Why doesn't Vim regex allow more than 9 capture groups?
Accepted answer
25 votes

The obvious reason is that groups with two or more digits are ambiguous: should \12 be taken as group 12, or as group 1 followed by the string 2? There are other reasons related to efficiency (...

View answer
How to enable/disable an augroup on the fly?
Accepted answer
21 votes

Building on your answer: you don't need a variable to keep state of the augroup, you can use exists() for that, provided that you know at least one of the autocmds that are part of the group: ...

View answer
How to share vim spellchecking additions between multiple machines?
Accepted answer
17 votes

Vim uses the spl file to do the checks, and the spl file is generated from the add file. We can speculate on whether the spl file is portable across different machines and Vim versions, but it's ...

View answer
How to jump to function call?
14 votes

You can't jump to function calls with ctags. For C/C++ and Java there is cscope, and it integrates nicely with Vim (see :help cscope). For other languages you might be able to find a tool that ...

View answer
What does `1 do?
Accepted answer
13 votes

You can jump to numbered marks, but you can't set them directly. According to :h mark-motions: Numbered marks '0 to '9 are quite different. They can not be set directly. They are only present ...

View answer
What are the advantages of using colon for commands instead of semicolon?
11 votes

You know the story behind the hjkl keys in Vi? Well, : was unshifted on Vi author's ADM-3A terminal. No idea if that's the real explanation in this particular case, but it seems at least as ...

View answer
Can a text file be recovered only from the .*.swp file?
Accepted answer
11 votes

Edit the same file again. Vim will prompt you with a few options; say you want to Recover. Then save the file, quit Vim, and remove the swap file (otherwise Vim will prompt you again the next time ...

View answer
Substitute second occurence on line
11 votes

This is much easier done with sed: sed 's/\.temp\././2' With Vim you need non-greedy matching, and it isn't easy to extend the method to replacing the 3rd, 4th, etc. occurrence of temp. But it can ...

View answer
Sort every few lines
Accepted answer
10 votes

The easy way: join the lines in the logical blocks before sorting. In detail: mark the lines join the indented lines to the lines above: :'<,'>s/\n /^A/ mark the lines again: gv sort them: :'&...

View answer
How to pass current file name with a different path to the command line
10 votes

Special characters (see :h cmdline-special) and filename modifiers (see :h filename-modifiers) are expanded on the command line, so: :diffsplit Path/to/other/directory/%:t

View answer
Merge blocks by interleaving lines
Accepted answer
9 votes

There is no dedicated way to do that (as far as I know), but yeah, it can be done with a few commands: function! Interleave(start, end, where) if a:start < a:where for i in range(0, a:...

View answer
How to set a key mapping in the modeline?
Accepted answer
8 votes

Moving to answers, as requested. Modelines are run in a restricted sandbox, there is only a limited number of things they can do, and defining keyboard maps is not among those. You can however set a ...

View answer
When exactly does <afile> differ from <amatch>?
Accepted answer
8 votes

<amatch> is exactly what is matched against the autocmd pattern. For autocmds that match filenames, symlinks are resolved, the result is canonicalized, and the full path is returned. <afile&...

View answer
Print an error message without "Error detected while processing function"
Accepted answer
7 votes

The solution is described in the manual: If you just want a highlighted message use :echohl. And to get a beep: :exe "normal \<Esc>" Thus: function! PrintError(msg) abort execute '...

View answer
Is there anything like matchaddpos() but stick the highlight with the character instead of grid?
7 votes

You can't do that with matchadpos(). This is a known problem with Vim highlighting, and it isn't likely to be solved any time soon. The best you can do is to use matcadd() with a regexp that matches ...

View answer
How can I keep the gutter width constant when using YouCompleteMe?
Accepted answer
6 votes

Install Vim 8 (released a few hours ago), and set signcolumn: set signcolumn=yes This is a actually available in Vim 7.4.2201 and later, but you really should go for the major release instead.

View answer
What is the rationale for \r and \n meaning different things in s command?
6 votes

A NUL byte is a string terminator in C, and for this reason Vim uses this convention, described in the manual at :h NL-used-for-Nul: <Nul> characters in the file are stored as <NL> in memory. ...

View answer
Is it possible to open a new file that starts with '+'?
Accepted answer
6 votes

From vim --help: Arguments: -- Only file names after this ... So this works on the command line: vim -- +models/File.m From inside Vim you need to escape the +: :e \+models/...

View answer
Append lines of text after pattern using global command
6 votes

One way to do it is to use the widely underappreciated \zs: :%s/report.*\zs/\r break;/ What this does is, look at lines containing report, find end of line, and add a new line with break. You ...

View answer
Is there any point in using C-u in an nmap?
Accepted answer
6 votes

The point of <C-u> is to make the macro not bail out when used with a count. If you press, say, 3: in normal mode you'll get .,.+2 inserted automatically on the command line. This seldom makes ...

View answer
understand backslash in search pattern in case of "very nomagic" mode
Accepted answer
5 votes

According to the manual: Use of "\V" means that in the pattern after it only the backslash and the terminating character (/ or ?) has a special meaning. "very nomagic" The phrasing is somewhat ...

View answer
How to ignore a column when visually selecting a paragraph
5 votes

Using Kana Natsuno's textobj-user: install textobj-user, then put the following in a file textobj-comment-lines/plugin/textobj/comment_lines.vim: if exists('g:loaded_textobj_comment_lines') finish ...

View answer
Is there a version of "strpart" that is aware of characters, rather than bytes
Accepted answer
5 votes

Something like this should work: function! Strpartw(src, start, ...) return a:0 ? \ join(split(a:src, '.\zs')[a:start : (a:start + a:1 - 1)], '') : \ join(split(a:src, '.\zs')[a:...

View answer
Why is make running against a wrong file?
Accepted answer
5 votes

The right-hand side of let &makeprg = ... is evaluated only once. If you put that command in your .vimrc as it is, %expands to /home/sporty/.vimrc and you get the interesting effect you describe. ...

View answer
How to generate patterns for comments?
5 votes

This is a perfect use case for ultisnips with Python interpolation. A sketch of a snippet for the first header might look like this: global !p from math import floor, ceil def left(lng, fill, s): ...

View answer
Calculating with python and appending result to file or register
5 votes

That's what pyeval() is good for. The only (minor) inconvenience is that you have to quote the expression you want to evaluate: let @a = pyeval('sum([x^2 for x in range(100)])') It works for all ...

View answer
Aditional SQLite syntax highlighting in VIM
Accepted answer
5 votes

As far as I know Vim doesn't have special support for SQLite, but mysql should come pretty close for this purpose: :SQLSetType mysql For more information: :help ft_sql.txt.

View answer
Vim compiled with python3 but has('python') returns 0
Accepted answer
4 votes

This is the expected result. There are no tests has('python/dyn') and has('python3/dyn'), only has('python') and has('python3').

View answer
how to change python interpreter used by jedi-vim
4 votes

You can't. The :python and :python3 commands always use the compiled-in interpreters. If you have a recent version of Vim with python and / or python3 loaded dynamically (see :h python-dynamic) you ...

View answer
Calling vimgrep so that results from the current file are on top?
Accepted answer
4 votes

You can sort the resulting quickfix list to give priority to current buffer, then run uniq() on it. Sadly you need to handle sorting quickfix items almost completely. For example the following will ...

View answer