Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

linux - set filetype and comment key map with .s file

Im woking with .s file. Although this file is asm file, but it is not same as .asm file (comment begins by --, not #).

So when I open a .s file, it shows as asm file. How can I change filetype to display correctly (blue color after -- sign)?

And also, I have mapped F1 in vimrc to comment as below:

if (@% =~ ".s") "if file is .s
   map <F1> :<ESC>0i-- <ESC>j
endif

This method works well with other files, but not with .s file. The comment sign is # because I map F1 is # previously.

Can you help me?

Thank you very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 = '12'
  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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...