Set Filetype and Comment Key Map with .S File

set filetype and comment key map with .s file


Standing on the shoulders of giants

I would highly recommend commenting plugin like commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc. There is no shame in using a plugin. Especially since comment toggling is deceptively more complicated than expected.

Customizing Commentary.vim

Simply set 'commentstring' to your comment style for your filetype. Most often this is already setup for you if Vim recognizes your filetype. See :h 'commentstring'.

Example of changing 'commentstring' for php. Add the following to ~/.vim/ftplugin/asm.vim:

if expand('%:t') =~# '\.s$'
set commentstring=--\ %s
endif

Or if you rather everything in your vimrc file:

autocmd FileType asm if expand('%:t') =~# '\.s$' | set commentstring=--\ %s | endif

You can set the toggle comment command to <f1> like so:

nmap <f1> <Plug>CommentaryLine

However commentary are much closer the the home row and do not conflict with any native mappings.

Note: I use commentary this may not work for other comment plugins. As always, read the documentation.

Syntax

You may need to change you syntax file to show lines starting with -- as comments. Add the following to ~/.vim/after/syntax/asm.vim:

syntax match asmComment /--\_s.*$/

But I don't or can't use a plugin

Sometimes using a plugin isn't an option, but you should know that (de-)commenting is a lot trickier than it first seems. Especially if you need to do it across multiple filetypes. Plugins like commentary.vim let you comment multiple lines at a time or use a motion, e.g. gcip.

The simplest approach. Comment only:

autocmd FileType asm nnoremap <buffer> <F1> gI-- <ESC>j

Now for some toggling with an map-expression:

autocmd FileType asm nnoremap <buffer> <expr> <f1> getline('.') =~ '^\s*--\_s' ? "^3"_x+" : "gI-- \<esc>+"

I have also created a naive commentary.vim-style comment toggle mappings. Put the following in your vimrc file:

nnoremap gcc :<c-u>.,.+<c-r>=v:count<cr>call <SID>toggleComment()<cr>
nnoremap gc :<c-u>set opfunc=<SID>commentOp<cr>g@
xnoremap gc :call <SID>toggleComment()<cr>

function! s:commentOp(...)
'[,']call s:toggleComment()
endfunction

function! s:toggleComment() range
let comment = substitute(get(b:, 'commentstring', &commentstring), '\s*\(%s\)\s*', '%s', '')
let pattern = '\V' . printf(escape(comment, '\'), '\(\s\{-}\)\s\(\S\.\{-}\)\s\=')
let replace = '\1\2'
if getline('.') !~ pattern
let indent = matchstr(getline('.'), '^\s*')
let pattern = '^' . indent . '\zs\(\s*\)\(\S.*\)'
let replace = printf(comment, '\1 \2' . (comment =~ '%s$' ? '' : ' '))
endif
for lnum in range(a:firstline, a:lastline)
call setline(lnum, substitute(getline(lnum), pattern, replace, ''))
endfor
endfunction

More information

:h 'commentstring'
:h :set
:h filetype
:h new-filetype
:h ftdetect
:h ftplugins
:h after-directory
:h :syn-match
:h :autocmd
:h :map-expression
:h :map-local

Create map for commenting line(s) of code in Vimrc

Yes, there are many possibilities; indeed, several authors have built fully-fledged robust and configurable plugins for commenting out lines:

  • NERD Commenter plugin
  • tComment plugin
  • commentary.vim plugin

So, unless this is for learning purposes, or if you absolutely cannot install plugins (but if you can configure Vim there would be ways to download and install a plugin on each run), I'd highly consider using a plugin for this.

Vim already provides the 'comments' and 'commentstring' options for the filetype-specific comment characters. Plugins use these (at least as a fallback; some also have their own built-in list).

Vim place comment uncomment in very beginning of line shortcut

Why not pick a popular comment plugin? I use nerdcommenter, and I am satisfied with it.

If you want to do the comment/uncomment toggle on your own, you may want to know gI. Like:

nnoremap whatever gI//<esc>

I still recommend the plugin, because even if build your own function check if there is // on BOL, to toggle comment, it adds/removes only //. If you opened a python file, or shell script or vimscript, you cannot use this mapping any longer. The plugin checked the filetype, it is convenient. Well you can of course write all things by yourself, to reinvent the wheel.

dynamic set the vim option by key map,but some thing wrong

It's best to try this out interactively using :echo:

:echo substitute('foobar-w20','w\(\d+\)','\="w" . (submatch(1)+1)','g')
foobar-w20

No, that didn't match. Ah, the multiplier for the \d must be \+ instead of +:

:echo substitute('foobar-w20','w\(\d\+\)','\="w" . (submatch(1)+1)','g')
foobar-w21

We can remove the re-insertion of the w prefix by starting the match only after it with \zs.

:echo substitute('foobar-w20','w\zs\(\d\+\)','\=submatch(1)+1','g')
foobar-w21

The :set command takes a literal string, not an expression to be evaluated. We have to either use :execute, or use the handy fact that :let can be used to modify &options:

:noremap <C-KPlus> :let &guifont=substitute(&guifont,'w\zs\(\d\+\)','\=submatch(1)+1','g')<CR>

Neither :set nor :let allow a range, which would be inserted in visual mode. I think this mapping should only apply to normal mode, so use :nnoremap. And we don't need that long expression shown, so <silent>:

:nnoremap <silent> <C-KPlus> :let &guifont=substitute(&guifont,'w\zs\(\d\+\)','\=submatch(1)+1','g')<CR>

BTW, on Windows, the size seems to be encoded in a h42 suffix, so let's handle that, too. Our previous refactoring now really helps:

:nnoremap <silent> <C-KPlus> :let &guifont=substitute(&guifont,'[hw]\zs\(\d\+\)','\=submatch(1)+1','g')<CR>

How to set default file extension of saving file for specific syntax in Sublime Text 3

The way to change the default file extension shown by the save-as dialog when saving a file set to a specific syntax is to override that syntax's default .sublime-syntax file. In your case that would be the C++.sublime-syntax file.

Of course, as MattDMo points out, the easiest thing to do is to manually type the preferred file extension whenever you save a file. But for those that want to make the change, here's how to do it.

  • Open the Command Palette and select View Package File.
  • Type C++ and then select C++/C++.sublime-syntax. ST will open the file.
  • Near the top you should see file_extensions: and a list like this:
file_extensions:
- cpp
- cc
- cp
- cxx
- c++
...snip...
  • ST uses the 1st item in the file_extensions list as the default file extension for the save-as dialog.
  • To make .cc the default extension instead of .cpp, edit the list order so that - cc is the 1st item and - cpp is 2nd, i.e. swap the top 2 lines around.
  • Now save the file in your ST config Packages directory with this path: ../Packages/C++/C++.sublime-syntax. If you like you can create a C++ directory in Packages and then just use Ctrl+S to save because the path will have been automatically set by ST but the file won't save unless the ../Packages/C++/ directory already exists.
  • Note: The full path on a Linux machine would be like this: /home/user/.config/sublime-text-3/Packages/C++/C++.sublime-syntax
  • The Package Resource Viewer plugin's Open Resource Command Palette command could be used to extract .sublime-syntax files instead of ST's native View Package File command. That plugin will automatically create the appropriate Packages directory when necessary.

Your altered Packages/C++/C++.sublime-syntax file will now override the default version that is shipped with ST. You can reverse this easily simply by deleting the file. It is safe to delete the directory as well if you are not overriding any other files in the same directory.

The only problem with this is when you install a new version of ST. If the newly installed version has an updated C++.sublime-syntax file, the local one you've created will continue to override the new one. To get around this delete your altered Packages/C++/C++.sublime-syntax file when you install a new version of ST and repeat the steps above to restore your preferred default file extension once the installation has been done.

Unfortunately there is no way around this, you can not partially override the C++.sublime-syntax file with just the file_extensions: section. Of course new versions of ST don't come along very frequently and the C++.sublime-syntax file gets updated even less often. So this is not a major issue.

How do I set a MIME type before sending a file in Node.js?

I figured it out!

Thanks to @rdrey's link and this node module I managed to correctly set the MIME type of the response, like this:

function handler(req, res) {
var url = convertURL(req.url);

if (okURL(url)) {
fs.readFile(url, function(err, data) {
if (err) {
res.writeHead(404);
return res.end("File not found.");
}

res.setHeader("Content-Type", mime.lookup(url)); //Solution!
res.writeHead(200);
res.end(data);
});
} else {
res.writeHead(403);
return res.end("Forbidden.");
}
}

How to customise file type to syntax associations in Sublime Text?

In Sublime Text (confirmed in both v2.x and v3.x) there is a menu command:

View -> Syntax -> Open all with current extension as ...



Related Topics



Leave a reply



Submit