FANDOM


(How to Comment/Uncomment in Clojure:)
(Language fascism. Unpersonaized. Proper code tags. Using <Leader>.)
Line 1: Line 1:
Put any tip related to the Clojure language
+
Tips related to the Clojure language
 
Here's one:
 
 
   
 
== How to Comment / Uncomment in Clojure: ==
 
== How to Comment / Uncomment in Clojure: ==
   
  +
The comment leader for Clojure is the <tt>;</tt> character. Here's a snippet you can use to comment and uncomment lines in Clojure files
   
The comment string is the character ;
+
<pre>
 
Here's a little script I put in my ~/.vimrc
 
 
 
function! ClojureCommentUncomment()
 
function! ClojureCommentUncomment()
"did you find the character ; at the beginning of the line?
+
"search for the ; character at the beginning of the line
  +
let search_saved = @/ " save the contents of the search register
 
if getline(".") =~ '\;'
 
if getline(".") =~ '\;'
let hls=@/ "take care of highlighting
+
s/^\;// " remove the ; at the beginning of the line
s/^\;// "remove the ; at the beginning of the line
 
let @/=hls
 
 
else
 
else
let hls=@/
 
 
s/^/\;/ "add a ; at the beginning of the line
 
s/^/\;/ "add a ; at the beginning of the line
let @/=hls
 
 
endif
 
endif
  +
let @/ = search_saved " restore the contents of the search register
 
endfunction
 
endfunction
map ,. :call ClojureCommentUncomment()<CR>
+
map <Leader>. :call ClojureCommentUncomment()<CR>
  +
</pre>
  +
  +
Typing <tt><Leader>.</tt> {{help|mapleader}} will now add a <tt>;</tt> at the beginning of the current line, if there isn't already one, or remove it, if there is one. You could make this more restrictive by only adding the mapping for files of the Clojure filetype, or by adding it to <tt>~/.vim/after/ftplugin/clojure.vim</tt>.
   
Restart Vim and type ,. on any file (not just Clojure files)
+
==References==
You could make this more restrictive by adding a filetype check.
+
* {{help|:map}}
  +
* {{help|mapleader}}
  +
* {{help|filetype}}
  +
* {{help|FileType}}
  +
* {{help|after-directory}}
   
----
+
==Comments==

Revision as of 22:14, September 4, 2009

Tips related to the Clojure language

How to Comment / Uncomment in Clojure:

The comment leader for Clojure is the ; character. Here's a snippet you can use to comment and uncomment lines in Clojure files

function! ClojureCommentUncomment()
  "search for the ; character at the beginning of the line	
  let search_saved = @/        " save the contents of the search register
  if getline(".") =~ '\;'
    s/^\;//                    " remove the ; at the beginning of the line
  else
    s/^/\;/ "add a ; at the beginning of the line
  endif
  let @/ = search_saved        " restore the contents of the search register
endfunction
map <Leader>. :call ClojureCommentUncomment()<CR>

Typing <Leader>. :help mapleader will now add a ; at the beginning of the current line, if there isn't already one, or remove it, if there is one. You could make this more restrictive by only adding the mapping for files of the Clojure filetype, or by adding it to ~/.vim/after/ftplugin/clojure.vim.

References

Comments

Community content is available under CC-BY-SA unless otherwise noted.