I work with quite a bit of multi-platform C/C++ code, separated by common #defines (#if WIN, #if UNIX, etc). It would be nice if I could have vim automatically fold the sections I'm currently not interested in when I open a file. I've searched through the vim script archives, but I haven't found anything useful. Any suggestions? Places to start?
Just add a folding region to your syntax
http://vim.wikia.com/wiki/Syntax_folding_of_Vim_scripts#Syntax_definitions
:syn region myFold start="\#IF" end="\#ENDIF" transparent fold
:syn sync fromstart
:set foldmethod=syntax
To add to #hometoasts answer, you can add that command as a comment in the first ten or last ten lines of the file and vim will automatically use it for that file.
/* vim: syn region regionName start="regex" end="regex": */
A quick addition to Denton's addition: to use the new syntax rule with any C or C++ code, add it to a file at $VIMRUNTIME/syntax/c.vim and cpp.vim. ($VIMRUNTIME is where your local Vim code lives: ~/.vim on Unix.) Also, the values for start and end in the syntax definition are regular expressions, so you can use ^#if and ^#endif to ensure they only match those strings at the start of a line.
I've always used forldmethod=marker and defined my own fold tags placed within comments.
this is for defining the characters that define the open and close folds. in this case open is "<(" and close is ")>" replace these with whatever you'd like.
set foldmethod=marker
set foldmarker=<(,)>
This is my custom function to decide what to display of the folded text:
set foldtext=GetCustomFoldText()
function GetCustomFoldText()
let preline = substitute(getline(v:foldstart),'<(','<(+)','')
let line = substitute(preline,"\t",' ','g')
let nextLnNum = v:foldstart + 1
let nextline = getline(nextLnNum)
let foldTtl = v:foldend - v:foldstart
return line . ' | ' . nextline . ' (' . foldTtl . ' lines)>'
endfunction
Hope that helps.
I have a huge code base and so a large number of #defines. Each file has numerous #ifdef's
and most of the times they are nested. I tried many of the vim scripts but they always
used to run into some error with the code I have. So in the end I put all my defines in
a header file and included it in the file that I wanted to work with and did a gcc on it
like this
gcc -E -C -P source.cpp > output.cpp
The -E command gets gcc to run only the pre-processor on the file, so all the unwanted
code within the undefined #ifdef's are removed.
The -C option retains the comments in the file.
The -P option inhibits generation of linemarkers in the output from the preprocessor.
Related
In vi and vim, % can be used to find a matching symbol e.g.:
/** <- With the cursor on / ...
* Some comment
*
...
*
*/ <- % moves the cursor to the / on this line
This works with matching {}, () [] pairs, and also works with c-conditionals like #ifdef.
Is there a setting to make this work with git conflict markers?
#! /usr/bin/env ruby
def hello
<<<<<<< HEAD <- With the cursor on this line
puts 'hola world'
=======
puts 'hello mundo'
>>>>>>> mundo <- % moves the cursor to this line
end
Can (and how) vi and/or vim be configured to do that?
In your startup files (eg $HOME/.vimrc) you can add:
packadd! matchit
let b:match_words = '<<<<<<<:=======:>>>>>>>'
packadd! enables the optional plugin, and match_words specifies the match pattern. Note that this will override any previous assignment of match_words, and this sort of thing is typically better suited to store in a filetype plugin. Adding those two lines directly in the main startup file should work, but may not be the best solution. (eg, if you're merging a file whose type is recognized, the filetype plugins may override the setting of match_words)
Assuming you are actually talking about Vim (which makes this configurable) and not vi (which doesn't).
The functionality is documented under :help %:
% Find the next item in this line after or under the
cursor and jump to its match. |inclusive| motion.
Items can be:
([{}]) parenthesis or (curly/square) brackets
(this can be changed with the
'matchpairs' option)
/* */ start or end of C-style comment
#if, #ifdef, #else, #elif, #endif
C preprocessor conditionals (when the
cursor is on the # or no ([{
is following)
For other items the matchit plugin can be used, see
|matchit-install|. This plugin also helps to skip
matches in comments.
From there, you can follow the 'matchpairs' tag to :help 'matchpairs', which won't help you because you can only add pairs of single characters like <:>.
You can then follow the promising matchit-install tag, which should put you on the right path.
Try harder.
I’m trying to set up an abbreviation in my .vimrc that will insert a comment template for heading-level comments in my CSS files.
The comment I want to insert is:
/* ==========================================================================
#
========================================================================== */
I will then jump back to the # and add my title there (e.g. BUTTONS).
The abbreviation I have attempted to set up looks like this:
iab comsec·
\/* ==========================================================================
\<Cr>#
\<Cr>========================================================================== */
(Where · represents a trailing space.)
Right away this feels pretty crude, but the specific problem is that if try and drop a comsec in my CSS, it starts wrapping it in more comments. The output looks like this:
/* ==========================================================================
* #
* ========================================================================== */
Notice the two * at the beginnings of lines 2 and 3?
Is there a way to tell vim not to try and be clever and to just drop in exactly what I’ve told it? A way to prevent vim from trying to wrap comments around the comment?
I’m not a particularly hardcore vim user, so there’s every chance I’m overcomplicating things, or missing something obvious, or using the wrong tool for the job.
Thanks!
If you are the type of person who can keep track of your personal utilities, this isn't so fancy but works. You can import the output of an external command into your buffer, so I put a mapping like this in my .vimrc file:
"bc = block comment
map ,bc :read! python ~/my_personal_utils/insert_css_comment.py
So, I just have to type ",bc" to add the comment. On my Mac at least, this leaves me hanging in command mode, so that my cursor is after '.py' and I can quickly type an argument like BUTTONS (i.e. the python script takes an optional argument).
Here is a function to do that.
:function! Comment()
:normal! i/*
:normal! 80a=
:normal! o#
:normal! o
:normal! 80i=
:normal! a*/
:endfunction
You can put this in vimrc file and create a command or map for this.
Command
:cmap comsec call Comment()
You can keep the cursor on a line and then call this command.
Or an in insert mode mapping
:imap comsec <ESC>:comsec<CR>
As alternatives I'd suggest nerdcommenter for commenting/uncommenting with a few key strokes.
Or, even better, ultisnips. In which you can easily make your own template for those headings:
open a .css file
exec command : UltiSnipsEdit
create your own snip:
snippet heading "heading comments"
/* ===================================
* ${1}
* =================================== */
endsnippet
Here is a better and simple way to to insert your comment which check everytime if the line is surrounded by the comment template.
All you have to do is to write your comment on new line and then press ; during the insert mode. (you can change the character ; by
any combination you want.)
The comment template is set by the variable l:start, l:begin, l:end
so you can change the number of spaces or = as you like.
If you would like to change the template completely keep in mind that you need to change also the regular expressions for the variables l:matchprev, l:matchhier, l:matchnext .
inoremap <silent> ; <Esc>mx:call Comment()<cr>i<Right>
function! Comment()
let l:start= "/* ====="
let l:begin=" # "
let l:end= " ==== */"
let l:next=getline(line(".")+1)
let l:hier=getline(line("."))
let l:prev=getline(line(".")-1)
let l:matchnext= matchstr( l:next , '^\s*=\+\s*\*/\s*$')
let l:matchhier= matchstr( l:hier , '^\s*#\s*.*$')
let l:matchprev= matchstr( l:prev , '^\s*/\*\s*=\+\s*$')
if l:matchnext != '' && l:matchprev != '' && l:matchhier != ''
return 0
else
execute ":s:^.*$:".l:start."\r".l:begin."&\r".l:end."\r:"
"the number 3 is the length of the variable l:begin
normal! `xj3l
endif
endfunction
write this code in another file scriptname and then you can use the mapping in any css file by typing in the command mode first :so scriptname
Another alternative is to put all that simply in your .vimrc file
I am trying to create a macro to use it globaly. I have inserted this command to my .vimrc file
let #c='<esc>:r c.c'
or
let #c=':r c.c'
but in both cases when I use "#c" on any file it only prints 'c>:r c.c' on the the file
Try adding a '^M' at the end of your macro, then "#c" should work. Else ':#c' should work as mentioned by ebenezer. You should use Ctrl+VEnter to insert '^M'.
let #c=':r c.c^M'
Best way would be to record the macro first and then save it to the .vimrc.
If these doesn't work, you can check the content of your register c using "cp and see if there is something missing.
This is described in another answer. Try something like this:
let #c = ':r c.c'
and then, in your file, use
:#c
to execute it.
If you want to include special characters in your macro (like Escape or Enter) then use a non-literal string (double quotes, not single quotes) and the correct syntax. See :help expr-string. You could use raw special characters, as #Amit suggests, but this will make your vimrc file hard to read.
Assuming that you are starting in Normal mode and want to type #c, there is no need to start off with an Escape.
For testing purposes, I tried
:let #c = ":echo 'foo'\<CR>"
and it worked as expected. I even added this line (without the leading :) to my vimrc file just to make sure, and tested it that way. This should work:
:let #c = ":r c.c\<CR>"
I occasionally see very long lines in my code that I need to check if they are the same. Is there a way in vim to select two lines and diff them to show any differences between the two?
For example, given the two lines in vim:
AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(That *is, Overloaded *with, Multiple *different, Parameter *lists);
AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(That *is, Overloaded *with, Multiple *different, Parameter *1ists);
I would like vim to tell me that the two lines are in fact different because each spells "lists" differently. Is this possible, and if so, how do I do it?
A quick and dirty solution is to just select both lines and sort them while removing duplicates:
select lines
":sort u"
if only one line remains, both were equal
if both remain, there most be some difference
An undo recovers everything again.
An alternative to #sehe's approach would not require the use of temp files:
funct! DiffTwoTexts(text1, text2)
new
put =a:text1
normal ggdd
diffthis
new
put =a:text2
normal ggdd
diffthis
endfunct
funct! DiffTwoLines(line1, line2)
let text1 = getline(a:line1)
let text2 = getline(a:line2)
call DiffTwoTexts(text1, text2)
endfunct
comma! DiffWithNext call DiffTwoLines('.', line('.') + 1)
This will still be pretty hard to read, since it keeps everything on a single line, so I came up with this modification:
funct! EvalTextPreprocessor(expr, text)
let text = a:text
return eval(a:expr)
endfunct
comma! -nargs=1 DiffWithNextPre call DiffTwoTexts(
\ EvalTextPreprocessor(<q-args>, getline('.')),
\ EvalTextPreprocessor(<q-args>, getline(line('.') + 1)))
This new command takes a vimscript expression as its argument, wherein the variable text refers to whichever line is being preprocessed. So you can call, e.g.
DiffWithNextPre split(text, '[(,)]\zs')
For your sample data, this gives the two buffers
AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(
That *is,
Overloaded *with,
Multiple *different,
Parameter *lists)
;
and
AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(
That *is,
Overloaded *with,
Multiple *different,
Parameter *1ists)
;
Only the lines that start with Parameter are highlighted.
You can even build up from there, creating a command
comma! DiffTwoCFunctionSigs DiffWithNextPre split(text, '[(,)]\s*\zs')
Notice that I modified the regexp a bit so that it will keep trailing spaces at the end of lines. You could get it to ignore them entirely by moving the \s* to after the \zs. See :help /\zs if you're unfamiliar with what that vim-specific RE atom does.
A nicety would be to make the command take a range (see :help command-range), which you could use by diffing the first line of the range with the last line. So then you just visual-select from the first line to the second and call the command.
I used linediff.vim.
This plugin provides a simple command, ":Linediff", which is used to diff two separate blocks of text.
That is not a feature, however it is easily scripted, e.g. in your vimrc:
function! DiffLineWithNext()
let f1=tempname()
let f2=tempname()
exec ".write " . f1
exec ".+1write " . f2
exec "tabedit " . f1
exec "vert diffsplit " . f2
endfunction
This will open the current and next lines in vertical split in another tab.
Note that this code is a sample
it doesn't check whether next line exists (there are any following lines)
it doesn't cleanup the tempfiles created
a nice improvement would be to take a range, or use the '' mark to select the other line
You can leave off the 'vert' in order to have a horizontal split
Map it to something fancy so you don't have to :call it manually:
:nnoremap <F10> :call DiffLineWithNext()^M
you could also just create a new empty window buffer and copy line, then make command:
:windo diffthis
this should open a new window showing the differences of those 2 lines
Using VIM, it is easy to display line numbers for any given file with:
:set number
However, the line numbering is 1-indexed, this means that the numbering starts at 1.
Normally this is exactly what i want, however a particular tool I am using to compile my code reports zero-indexed line numbers - that is, line numbers start at zero.
How do I change vim's line numbering to compensate so that viewing the numbers in the vim buffer corresponds to the errors provided by the tool, and in addition jumping to a particular number corresponds to that of the tool as well.
That is, if the tool tells me that there is an error on line 98, I want to jump to that line by typing "98G", not "97G", and I want that line (which is really line 97 in vim) to display "98" in the line number list.
Edit:
While I can filter the output of the tools, while fairly simple it is not a trivial task because the tool also outputs hex values that correspond to each line, which has the same zero-indexed form, and this is is output in informational messages as well, not just for errors, and I have many such projects.
I don't think this is possible; however, assuming the output of your external tool is just text, it would probably be fairly easy to filter the output such that the numbering is 1-indexed to match Vim. Can you give us an example of your output?
Edit
Alternatively, if you call the external command from Vim, you could do something like (basically, add a blank line, run the external command and then delete the blank line):
command! RunMyExternalProgramme call RunMyExternalProgramme()
func! RunMyExternalProgramme()
" Save the old setting of makeprg
let s:savedMakePrg = &makeprg
" Save the screen layout
let s:savedView = winsaveview()
if config_file != ''
" Put a blank line at the start of the file
:1put! =''
" Save
write
" Change makeprg and run it
let &makeprg = '/path/to/programme -options etc'
make
" Delete the blank line
1d
" Save
write
endif
" Restore the screen layout (optional)
call winrestview(s:savedView)
" Restore the old setting of 'makeprg'
let &makeprg = s:savedMakePrg
endfunc
I suggest to wrap your tool in a small script. In the script, either copy the source code and add an empty line at the top of the source or use awk to fix the output (parse the error messages and add 1).
The only way to make vim display line numbers starting with 0 is to patch the source and create a new option (say lno for line number offset) and add this value to the line number. You'd need to patch this in quite a few places (display, goto commands, search patterns, etc). Shouldn't take more than one or two years to make it work :) Good luck on getting the author of Vim to accept this as a patch.
set relativenumber or for short "set rnu"
This starts the count from 0