You are on page 1of 4

How to setup your editor

This a wikipage describing how to properly setup your favourite editor.

Tabs and trailing whitespaces


Take the pre-commit hook into use so that you can avoid server-side reject.

$MGW_GIT_REPO/env/setup.sh

With this sed script trailing whitespaces can be removed from the whole file. It is strongly recommended to do whitespace cleanups in a separate commit.
And not to mix them with functional changes.

sed -i -e 's/\s\+$//' path_source_file/some_source.cc path_source_file/some_other_source.cc

With the script below you can remove all trailing empty lines from a file, then add only one newline at the end.

sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' <filename>


sed -i -e '$a\' <filename>

For multiple files for which you can formulate a find expression, you can run these in one run as:

find <path/to/search> -name <name pattern> -type f -print0 | xargs -0 sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
find <path/to/search> -name <name pattern> -type f -print0 | xargs -0 sed -i -e '$a\'

Here are ways to replace tabs with spaces. Open the files in vim and execute the following command within it. Or run the sed script (note that the
indentation might go wrong).

vim path_source_file/some_source.cc path_source_file/some_other_source.cc


:bufdo retab | w

OR

sed -i -e 's/\t/ /g' path_source_file/some_source.cc path_source_file/some_other_source.cc

It is strongly adviced that the plain git pull is not used. Instead the local repo should be updated like this:

git pull --rebase

OR

git fetch origin


git rebase origin/master

This will rebase your changes to the tip and there will be no merge-commits.

If you need to remove tabs or trailing whitespaces from your previous commit then you need to do this:

... make the changes you want


git add file_that_you_want_to_add
git commit --amend

This will include your new changes to the previous commit.

eclipse
Please see this link.
emacs
Use whitespace-mode
On your .emacs file add the following:

;;Remove whitespaces after save


(add-hook 'before-save-hook 'delete-trailing-whitespace)

You can also invoke that function directly

Trailing white space and tab characters


Get highlight-chars mode and add following lines to your .emacs file:

; Tabs as spaces
(setq-default indent-tabs-mode nil)
(setq tab-width 4)

; Show tabs and trailing white space


(require 'highlight-chars)
(add-hook 'font-lock-mode-hook 'hc-highlight-tabs)
(add-hook 'font-lock-mode-hook 'hc-highlight-trailing-whitespace)

Highlight characters that exceed the line length limit

To mark characters that exceed the agreed-upon standard line length, you can add lines-tail to whitespace-style. Add the below example to ~/.
emacs and substitute 100 for whatever your actual rule is. Substitute the list whitespace-style with the actual whitespace style that you want. This is
just an example, the full list of things available in whitespace-style can be found here.

Set column width

(setq whitespace-line-column 100)


(setq whitespace-style
'(face trailing lines-tail indentation space-after-tab space-before-tab)))

erlang-mode
If you are writing Erlang code, load erlang-mode which is included in the OTP distribution. This will enable syntax highlighting for Erlang as well as the
ability to automatically do correct and "official" Erlang indentation for you.

Add the snippets for the features you want into your ~/.emacs

Installation alternative 1:

Load it by putting the directory from OTP with erlang.el in your load-path.

Loading erlang-mode

; Add erlang-mode to the load path.


(setq load-path (cons "/path/to/erlang/lib/tools-X.Y.Z/emacs/" load-path))
(require 'erlang-start)

Installation alternative 2:

Install erlang-mode using MELPA or your other favourite emacs package archive, with M-x package-install RET erlang-mode RET

This method requires that you have already set up one or more package archives and that you have refreshed the contents so that your emacs knows
which packages exist. If the command didn't work, try running M-x package-refresh-contents RET first, and then run package-install again.

Load erlang-mode automatically


erlang-mode loads automatically when a .erl, .hrl or .yrl file is opened in emacs. You can add more file suffixes that will use erlang-mode like this:

Loading erlang-mode automatically for additional file suffixes

; Add these lines to automatically use erlang-mode for .upg.src and .app.src files
(add-to-list 'auto-mode-alist '("\\.app\\.src\\'" . erlang-mode))
(add-to-list 'auto-mode-alist '("\\.upg\\.src\\'" . erlang-mode))

Clean up whitespaces automatically

You can do cleanup of common whitespace errors, such as removing tabs and trailing whitespaces, automatically. See above under "use whitespace
mode" on how to remove trailing whitespaces automatically upon saving.

You can add the following snippet to replace all tabs with spaces and clean up all incorrect whitespaces, both leading and trailing, automatically upon
saving:

Do automatic whitespace fixes when saving

; Defines the function fix-whitespace-upon-saving which adds before-save hooks for whitespace-cleanup and
untabify to the local buffer.
(defun fix-whitespace-upon-saving ()
(add-hook 'before-save-hook 'whitespace-cleanup nil t)
(add-hook 'before-save-hook (lambda () (untabify (point-min) (point-max))) nil t))

(add-hook 'erlang-mode-hook 'fix-whitespace-upon-saving)

This is added as an erlang-mode-hook which means that it will only be effective when editing in erlang-mode. You can keep it in your .emacs config file
without having to worry about Makefiles or .go files being untabified.

Keep in mind that this can cause big diffs when you change a file that already has a lot of incorrect whitespaces from people who didn't bother setting up
their editors. You might want to invoke the functionality manually instead, in which case a good option could be to bind it to a shortcut.

Do whitespace fixes with a keyboard shortcut

; Defines the full-whitespace-cleanup function and binds it to the keyboard shortcut C-c C-g C-f
(defun full-whitespace-cleanup ()
(interactive)
(whitespace-cleanup)
(untabify (point-min) (point-max)))

(global-set-key (kbd "C-c C-g C-f") 'full-whitespace-cleanup)

nedit
$HOME/.nedit/nedit.rc

! some default the ruled in some projects, use your own settings
nedit.emulateTabs: 2

vim
Lines to add to your .vimrc file:
" 4 spaces indentation, no tabs
set tabstop=4
set shiftwidth=4
set expandtab

" Color trailing whitespace and tabs


highlight ExtraWhitespace ctermbg=red guibg=red
au ColorScheme * highlight ExtraWhitespace guibg=red
au BufEnter * match ExtraWhitespace /\s\+$\|\t/
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$\|\t/
au InsertLeave * match ExtraWhiteSpace /\s\+$\|\t/

You might also like