.vimrc for C developers - c

There is a question How to set .vimrc for c programs?, but nothing especially interesting in there.
By what .vimrc options do you facilitate your C development in Linux? (e.g. for building, ctags, tabs...) Any ideas welcome, especially for "external building with make".

how about this?
https://mislav.net/2011/12/vim-revisited/
set nocompatible " choose no compatibility with legacy vi
syntax enable
set encoding=utf-8
set showcmd " display incomplete commands
filetype plugin indent on " load file type plugins + indentation
"" Whitespace
set nowrap " don't wrap lines
set tabstop=2 shiftwidth=2 " a tab is two spaces (or set this to 4)
set expandtab " use spaces, not tabs (optional)
set backspace=indent,eol,start " backspace through everything in insert mode
"" Searching
set hlsearch " highlight matches
set incsearch " incremental searching
set ignorecase " searches are case insensitive...
set smartcase " ... unless they contain at least one capital letter

https://github.com/jslim89/dotfiles
This is my repo. Inside already got a few type of vim plugins including c.vim, ctags, autocomplete, etc.

Along with options in plan9assembler's answer,
Run make from inside vim, you can just use :make but that won't automatically open the quickfix window with you're errors. To get that to happen, add a second :Make command [1]:
command! -nargs=* Make write | make! <args> | cwindow
Another thing I have is a recursive search for my ctags file. The following will use the tags file in the current directory, then search one directory above recursively till it finds a tag file [2]:
set tags=./tags;

Related

Remove characters from certain place in string

I have this var (source) which looks like this:
\\xxxxx\xxxx\xxx\Companies\test\in\Th FIle Test - Copy - Copy.mp4
I would like to remove everything before "\" character. So it would look like this:
Th FIle Test - Copy - Copy.mp4
I looked at the trim function but not joy. I am using Batch, so I have a bat file.
I believe you can just use regular expressions. I'm unsure of the exact syntax for whatever program you're using, but the regex to search and replace should be relatively easy.
Replace .*\\ with '' (nothing)
https://regex101.com/r/hQ4hB9/1 <-- you can play around with this. Good website for testing regular expressions.
The easiest way to get the leaf of the path is by using enhanced for variable references.
set "var=\\xxxxx\xxxx\xxx\Companies\test\in\Th FIle Test - Copy - Copy.mp4"
for %%I in ("%var%") do set "filename=%%~nxI"
echo %filename%
See the last couple of pages of help for in a console window for more information.

Manage shortcuts arguments for NSIS CreateShortCut method via CMake

First hi to everyone !
I want to create a shortcut to a batch file that does not prompt the DOS window. For that I have seen that the following command works very well:
wscript.exe invisible.vbs my_batch_file.bat
My problem is that I would like to create the shortcut with this command via CMake and NSIS. My problem is that it seems I cannot give more than one parameter after "wscript.exe" in the following command in the CMakeLists.txt file:
list(APPEND CPACK_NSIS_CREATE_ICONS "
CreateShortCut '$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\link.lnk' 'wscript.exe' 'invisible.vbs my_batch_file.bat' icon.ico 0 SW_SHOWMINIMIZED
")
And the space between "invisible.vbs" and "my_batch_file.bat" is not parsed as I expected (i.e. as a space...). Could anyone help me with this ? Thanks a lot for every comment (method or code hint) !
i don't now anything about NSIS, but maybe this is a hint in the right direction:
list(APPEND CPACK_NSIS_CREATE_ICONS "
CreateShortCut '$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\link.lnk' 'wscript.exe' 'invisible.vbs my_batch_file.bat' icon.ico 0 SW_SHOWMINIMIZED
")
also maybe, that 'invisible.vbs my_batch_file.bat' has to be ' invisible.vbs my_batch_file.bat' - starting with a space after '.
edit:
have you tried using " instead of '?
I solved my problem using the "NSIS.template.in" file, in which I created the following macro
!macro CreateShortcutBat link bat_file
CreateShortCut '$SMPROGRAMS\\$STARTMENU_FOLDER\\${link}' 'wscript.exe' 'invisible.vbs ${bat_file}' icon.ico 0 SW_SHOWMINIMIZED
!macroend
Then in my CMakeLists.txt file, I only need to call the macro this way:
list(APPEND CPACK_NSIS_CREATE_ICONS "
!insertmacro CreateShortcutBat 'Shortcut.lnk' 'my_batch_file.bat'
")
This short article shows exactly how to do what you want. It uses CPACK_NSIS_CREATE_ICONS_EXTRA and CPACK_NSIS_DELETE_ICONS_EXTRA to achieve the result. The example used there has two command line arguments, so you should be able to use the code there with simple modification. It also shows how to clean up after yourself and have the uninstaller remove the shortcut for you too.

Why does Applescript run in Script Editor, but error when saved as Application?

My applescript needs to detect its own filename, and the following runs fine on Snow Leopard (10.6)
set my_name to name of me as string
display dialog "Name: " & my_name
It displays "Name: AppleScript Editor" when I run it from AppleScript Editor, and it displays "Name: NewTest" when I save it as an application called NewTest.
When I run it on a Leopare (10.5) machine, it complains "Can't make name of <> into type string." When I remove the "as string" portion, it runs under Script Editor, returning "Name: Script Editor", but when saved as an application, it errors and says, "Can't get name."
What is different about running in script editor and saving as application under 10.5?
Here's another thought although I haven't checked. One thing that can cause problems is the command "get". In general when you run a command like "name of me" the command get is implied so you're really running "get name of me". The problem is that the implied "get" is not always the case. So sometimes you have to explicitly say "get". Whenever I have a problem like yours the first thing I try is to add "get" to the command... it's become habit because you just never know. Note that you can always use the word get and never have that issue. As such, try changing your command to "set my_name to (get name of me)". I'd be interested to know if that fixes your 10.5 problem. Also note that a name is already a string so there's no need to coerce the result to a string.
EDIT:
I looked through some of my older scripts. I used the following code to get the name. In my notes I have these comments...
-- this will get the name of the application or script without any file extension
-- it is done using the path because when a script is run from the script menu, and you write set myName to name of me, then the result is "applescript runner" instead of the actual name
-- also it assures that you're getting the name as it appears in the Finder because sometimes the system events process name is different than the Finder name
on getMyName()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
return myName
end getMyName
An Applescript application isn't an "application" in the truest sense of the word. A lot of contexts change, like "get path to me" will be different when run as a script or as an application, because they are still good ol' wonky Applescript as opposed to a Carbon or Cocoa-based application. Running similar code against the Finder...
tell application "Finder"
set my_name to name as string
display dialog "Finder: " & my_name
end tell
...behaves as expected because the Finder is a Carbon/Cocoa-based application.
I don't have a real answer other than to say it sounds like there was a change made to the OS relative to the Applescript framework in 10.6 that makes the call to "me" behave more as expected.
I would recommend reading the section in the Applescript guide about the me and it keywords to gain more insight into how me works.

Where can I modify (at least find the files) completions in Vim?

I turned on completion in Vim:
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete
Where can I modify those completions (e.g. CSS completion).
Where is the file that has those completions?
On unix, it's most likely at:
/usr/share/vim/vim<version>/autoload/<language>complete.vim
and the like.
On my system it's:
/usr/share/vim/vim72/autoload/pythoncomplete.vim
On some systems it might be /usr/local/share/vim instead. In Windows look in your GVim install folder.
There's also the chance that it's in ~/.vim/autoload.
You might also want to know about vim's magnificent general purpose "Where the hell is that set?" command :verbose eg. to solve this problem you could have gone.
:verbose fun pythoncomplete#Complete (fun is because it is a function)
....
Last set from ~/.vim/plugin/pythoncomplete.vim
....
This can be used for a commands, maps, autocommands etc. eg.
:verbose map \t
should list where all key maps starting with \t
Look in your /home/.vim directory and the /home/.vimrc file.
There seems to be several ways of implementing this.
Here's one.

Changing Vim indentation behavior by file type

Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces.
You can add .vim files to be executed whenever vim switches to a particular filetype.
For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:
setlocal shiftwidth=2
setlocal tabstop=2
Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).
This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.
Use ftplugins or autocommands to set options.
ftplugin
In ~/.vim/ftplugin/python.vim:
setlocal shiftwidth=2 softtabstop=2 expandtab
And don't forget to turn them on in ~/.vimrc:
filetype plugin indent on
(:h ftplugin for more information)
autocommand
In ~/.vimrc:
autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab
I would also suggest learning the difference between tabstop and softtabstop. A lot of people don't know about softtabstop.
edit your ~/.vimrc, and add different file types for different indents,e.g. I want html/rb indent for 2 spaces, and js/coffee files indent for 4 spaces:
" by default, the indent is 2 spaces.
set shiftwidth=2
set softtabstop=2
set tabstop=2
" for html/rb files, 2 spaces
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
" for js/coffee/jade files, 4 spaces
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype coffeescript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype jade setlocal ts=4 sw=4 sts=0 expandtab
refer to: Setting Vim whitespace preferences by filetype
Put autocmd commands based on the file suffix in your ~/.vimrc
autocmd BufRead,BufNewFile *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile *.pl syntax on
The commands you're looking for are probably ts= and sw=
I usually work with expandtab set, but that's bad for makefiles. I recently added:
:autocmd FileType make set noexpandtab
to the end of my .vimrc file and it recognizes Makefile, makefile, and *.mk as makefiles and does not expand tabs. Presumably, you can extend this.
Personally, I use these settings in .vimrc:
autocmd FileType python set tabstop=8|set shiftwidth=2|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab
This might be known by most of us, but anyway (I was puzzled my first time):
Doing :set et (:set expandtabs) does not change the tabs already existing in the file, one has to do :retab.
For example:
:set et
:retab
and the tabs in the file are replaced by enough spaces. To have tabs back simply do:
:set noet
:retab
For those using autocmd, it is a best practice to group those together. If a grouping is related to file-type detection, you might have something like this:
augroup filetype_c
autocmd!
:autocmd FileType c setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
:autocmd FileType c nnoremap <buffer> <localleader>c I/*<space><esc><s-a><space>*/<esc>
augroup end
Groupings help keep the .vimrc organized especially once a filetype has multiple rules associated with it. In the above example, a comment shortcut specific to .c files is defined.
The initial call to autocmd! tells vim to delete any previously defined autocommands in said grouping. This will prevent duplicate definition if .vimrc is sourced again. See the :help augroup for more info.
Today, you could try editorconfig, there is also a vim plugin for it. With this, you are able not only change indentation size in vim, but in many other editors, keep consistent coding styles.
Below is a simple editorconfig, as you can see, the python files will have 4 spaces for indentation, and pug template files will only have 2.
# 4 space indentation for python files
[*.py]
indent_style = space
indent_size = 4
# 2 space indentation for pug templates
[*.pug]
indent_size = 2
While you can configure Vim's indentation just fine using the indent plugin or manually using the settings, I recommend using a python script called Vindect that automatically sets the relevant settings for you when you open a python file. Use this tip to make using Vindect even more effective. When I first started editing python files created by others with various indentation styles (tab vs space and number of spaces), it was incredibly frustrating. But Vindect along with this indent file
Also recommend:
pythonhelper
python_match
python_ifold
In Lua (for Neovim users) you can use RUNTIMEPATH/ftplugin/*yourfiletype*.lua with options like:
vim.opt_local.shiftwidth = 2
vim.opt_local.tabstop = 2
Just be sure to use string values in quotes. For example:
vim.opt_local.foldmethod = 'marker'
I use a utility that I wrote in C called autotab. It analyzes the first few thousand lines of a file which you load and determines values for the Vim parameters shiftwidth, tabstop and expandtab.
This is compiled using, for instance, gcc -O autotab.c -o autotab. Instructions for integrating with Vim are in the comment header at the top.
Autotab is fairly clever, but can get confused from time to time, in particular by that have been inconsistently maintained using different indentation styles.
If a file evidently uses tabs, or a combination of tabs and spaces, for indentation, Autotab will figure out what tab size is being used by considering factors like alignment of internal elements across successive lines, such as comments.
It works for a variety of programming languages, and is forgiving for "out of band" elements which do not obey indentation increments, such as C preprocessing directives, C statement labels, not to mention the obvious blank lines.

Resources