Function to see if a vim help file - vim

Would the following function always detect a vim help file? If not, what might be a better pattern or function to set this?
function IsHelpFile()
if trim(getline('$')[0:3]) !=? 'vim'
set filetype=markdown
colorscheme OceanicNext
endif
endfunction
For example, from:

No, this function doesn't detect help files. It detects if last line does (not?) start with pattern '.vim' (where '.' means any character in RegEx).
I think you wanted to check if it does start with it, if so then it should be
if getline('$') =~ '^.vim'
" ...
endif
=~ checks if left side matches regular expression on the right. ^.vim means "line starting with some charater followed by string 'vim'".
But it still wouldn't detect whether the file is help file. It would detect if last line contains something what could be a modeline (:h modeline).
The easiest way to check if file is help page is to check its filetype:
function IsHelpFile()
if &filetype == "help"
set filetype=markdown
colorscheme OceanicNext
endif
endfunction
Note that colorscheme won't apply only to help page, but to whole Vim session
Although I discourage you from changing filetype of help file. It has a lot of special formatting and options to make it readable and easy to navigation. Changing it to Markdown will remove all of that.

Related

How to disable automatic indentation by Slimv for non-Lisp files?

Non-Slimv Behavior
At first, let me show the normal Vim behavior when Slimv is not enabled.
Create a new file: vim foo.c.
Enter insert mode: i
Enter this code: void f()
Press enter
The cursor is now at position 2,1 (2nd row, 1st column).
Slimv Behavior
After installing Slimv, if I perform the steps above, at the end, I find the cursor at position 2,5 (2nd row, 5th column) with four spaces inserted as indentation before the 5th column automatically.
How can I disable this Slimv behavior for non-Lisp files (such as .c files)?
The issue is caused by this line in paredit.vim:
filetype indent on
I added the option g:paredit_disable_ftindent to paredit.vim to disable the loading of indent files, please add this line to your .vimrc when using paredit.vim (or slimv that also contains paredit.vim):
let g:paredit_disable_ftindent=1
First, add to your vimrc file the following two lines:
filetype indent off
filetype plugin indent off
Then you have to hope that it’ll work! But there is a large probability that it won’t…
If the solution doesn’t work, you may have to make quite complicated actions to solve the trouble.
The problem is that many of your vim options are constantly changed by a lot of triggers and auto commands. The only way I found is to mark the important options (the options I don’t want to be changed) with a special symbol, and then to restore them forcibly after any possible impact. So, I did the following in my vimrc:
augroup MyVimrc
autocmd!
augroup END
"The options I want to keep unchanged, I mark with the “❗” symbol.
"The usual exclamation sign “!” will work as well.
"Of course, you’re free using any other symbol you like,
"but don’t forget the lambda-function in the hack below.
"These are the options that may influence on the indent.
set formatoptions=j "❗
set autoindent "❗
set nosmartindent "❗
set indentexpr= "❗
set copyindent "❗
set preserveindent "❗
"And I marked with the same way a number of other
"important (for me) options… (not shown here)
"At the bottom of the vimrc file, I wrote this dirty hack.
function! s:keep_options()
for line in filter(readfile($MYVIMRC), 'v:val =~ ''\v\".*(!|❗)\s*$''')
exe line
endfor
endfunction
command! KeepOptions call <SID>keep_options()
"Note, the “OptionSet” trigger doesn’t work always, so that I preferred “BufEnter”.
autocmd MyVimrc BufEnter * KeepOptions
And all the troubles with unpredictable changes in my settings, at last, have gone.

Why :help ignore :set number in .vimrc?

I :set number in ~/.vimrc to show line numbers. When I type :help to view documentation, the line numbers don't show in the new window.
If I type :setl number?, it prints nonumber. If type :setg number?, it prints number.
I want to know why ~/.vimrc doesn't work. Which script resets the local number option?
I've checked the $VIMRUNTIME/ftplugin/help.vim, but it doesn't reset the number.
Currently, I'm using:
if has('autocmd')
auto FileType help set number
endif
Thanks.
I'm not sure what is the problem you're having. Numbers don't show up in help - yes, if I recall correctly that is a design decision and a feature - a good one, in my opinion, since why would one want line numbers in help files? (Okey, you could say "to quote a particular line from help file" but tags seems sufficient for that).
As far as the other thing go, when I set
setl nonumber
setg number
and open a new buffer in a split, numbers show.
If I start a new vim session, setlocal and setglobal number being nonumber and set number they're both changed.
Is this the behaviour you're having too?
After opening vim, simply type:
:auto FileType
Do you see set number listed under help? If not, your .vimrc is not being read.

VIM: set filetype=txt for every new file [No Name]

I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work.
This is p.e. what I've read on the web a few times:
au BufRead,BufNewFile *.txt setlocal ft=txt
(to put in _vimrc)
However it doesn't work.
Can anyone help me?
The following line, added to your .vimrc, will set the filetype to text if it is not already set.
autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif
All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set the file-type to any old text, so are you absolutely sure it is not working?
:set filetype=banana
:set filetype?
filetype=banana
Setting the filetype is not going to have any noticable effect unless there is a corresponding file in the ftplugin Vim directory and Vim does not ship with a txt.vim file-type file. You could, of couse, add a txt.vim here but I am not sure what this will gain you over default settings — what special behaviour would you want for text files that you would not want for the default behaviour?
(If you want syntax highlighting (whatever that may mean for text file!) then you will also have to create a txt.vim file in the syntax Vim directory.)
What effect are you trying to achieve?
It's actually way simpler than all this. Just put this as one of the first lines in your .vimrc.
set ft=txt
Whenever opening a new file, the filetype will be set to txt. But if you open a file with an known existing type it will still be overridden no problem.

Vim: How to change text from within an indent script

I recently switched from Eclipse to Vim. I'm loving it. There are a few hangups I'm working on, but one of the ones I'm having lots of trouble with is the PHP doc comments. In eclipse I could type:
/** [enter]
and the next line would auto fill with
*
So I'd have:
/**
* [comment goes here]
I'm wondering if there's anything like this for vim. It seems there are some plugins to autogenerate doc comments by running a command, but I'd love to have it do them as I'm typing.
I was playing around with the PHP indent script (http://www.vim.org/scripts/script.php?script_id=1120) and I got it to recognize when it's inside of a doc comment block, but I can't figure out how to get it to actually change the text and add a " * " after hitting enter when inside the block.
I've tried what I've seen other plugins do:
let #z = ' * '
put! z
tried this too:
exe 'normal!' '"zgp'
but no luck. Is this not possible from an indent script, and if not, how do I actually get Vim to recognize a doc comment block and act accordingly while I'm typing?
Any help would be greatly appreciated!
No need to mess around with the indentation files. Vim's formatoptions will do this for you and in a variety of languages (not just PHP).
Ensure you have r included in your formatoptions:
:setlocal fo+=r "to set
:set fo? "to query
You can include this in your .vimrc or in .vim/ftplugin/php.vim (if you just want to activate this for PHP).
For more information on formatoptions and file-type plugins, see:
:help 'formatoptions'
:help fo-table
:help ftplugins
Would adding the below code to your vimrc do something similar to what you want?
autocmd BufNewFile,BufRead *.php setlocal formatoptions+=r formatoptions+=o
autocmd BufNewFile,BufRead *.php setlocal comments=s1:/*,mb:*,ex:*/,://,:#
I currently can't quite figure out how to make it work without overriding the <!-- ---> commenting, which this does. I.e. this will break auto-indenting with <!-- --> comments.
Edit. Added ://,:# to comments as Johnsyweb's distribution does.
Try adding this to your vimrc:
let g:PHP_autoformatcomment=1
I'm on a Mac and it seems to be enabled by default. Functions exactly how you stated.

How do I turn on search highlighting from a vim script?

If I do either of the following two:
call search("searchString")
exec "/ searchString"
From a script, then vim does the search but does not highlight the results, even though hlsearch. Doing the same searches from outside a script highlights the results.
Just found out the answer myself:
call search(l:searchString)
call matchadd('Search', l:searchString)
The
feedkeys()
function is the key (pun intended):
call feedkeys("/pattern\<CR>")
or cleaner:
" highlights – or doesn’t – according to 'hlsearch' option
function SearcH(pattern)
let #/ = a:pattern
call feedkeys("/\<CR>")
endfunction
I know this is late. However when I searched for the answer to this problem this page came up. So I feel compelled to help fix it.
call search(l:searchString)
call matchadd('Search', l:searchString)
Did not work for me. (when run from inside a function) It did higlight the words I wanted to search for but n/N wouldn't cycle between them. Also when I performed a new search the "l:serachStirng" pattern still remained highlighted. This answer on this link worked much better
Vim search and highlighting control from a script
Which gave me:
let #/ = l:searchString
then run
normal n
outside the funciton (so the highlighting is done immediately without the user needing to press n)
To turn on, press ESC type :set hls
To turn off, press ESC type :set nohls
Found answer here:
http://vim.1045645.n5.nabble.com/highlighting-search-results-from-within-a-function-tt5709191.html#a5709193
```
One solution would be
function! XXXX()
execute '/this'
return #/
endfunction
and to use the following instead of ":call XXXX()".
:let #/ = XXXX()
```
I believe this works from inside a function
(to just enable highlighting and nothing more):
call feedkeys(":\<C-u>set hlsearch \<enter>")
You need to put this in your .vimrc file
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
The .vimrc file is usually located in your home directory, or you can find it using "locate .vimrc"

Resources