typora-issues icon indicating copy to clipboard operation
typora-issues copied to clipboard

Code fence auto indent and Tab indent

Open iamswf opened this issue 9 years ago • 14 comments

Hi. I found that when coding in javascript in code fence, the auto indent seems to be 2 spaces. How can I customize the auto indent to be 4 spaces? And another question, how can I set the Tab indent to be 4 spaces? Thanks.

iamswf avatar Jan 18 '17 13:01 iamswf

Hi bro, have you figure out this issue?

I found the same case in Java code fence.

According to CodeMirror

indentUnit: integer How many spaces a block (whatever that means in the edited language) should be indented. The default is 2.

So I think this could be figured by modify a specific preference file. I tried to search any possible file to change the default Tab indent and auto indent.

However, nothing help yet.

bill0871 avatar Sep 16 '17 10:09 bill0871

We should expose the "indent side" option of codemirror in future updates.

abnerlee avatar Sep 17 '17 04:09 abnerlee

I am now having this issue with PHP and Go code as well. This is especially problematic for Go since the language requires tabs, not spaces, and the PHP standard for many projects is tabs as well. (This is important for me since I'm actually extracting runnable code from my code blocks, and use Typora to write it as well.)

pjeby avatar Dec 27 '17 04:12 pjeby

It looks like these settings in CodeMirror are what determine the indentation. (Specifically, indentUnit, smartIndent, tabSize, and indentWithTabs.) Exposing them -- even if only via the "advanced" JSON-based settings -- would allow those of us who do use Typora as a code editor to tune these settings to our needs.

pjeby avatar Dec 27 '17 05:12 pjeby

Added option for "Indent Size for Code"

abnerlee avatar Feb 23 '18 14:02 abnerlee

Hi, has this function been implemented?

vinqin avatar Jul 25 '18 04:07 vinqin

Yes, but only for code fences and source code mode

abnerlee avatar Jul 31 '18 08:07 abnerlee

Hi, can you guys add an option for tab indentation instead of space indentation?

alaneuler avatar Feb 04 '19 06:02 alaneuler

Yes, but only for code fences and source code mode

I would love being able to change tab size for 'plain text' too, is it possible? Sometimes working with subtopics in my notes leaves a lot of empty and usuless spaces. (current Tab Size for #write is 8 characters)

image

thiagodiascano avatar Mar 04 '19 21:03 thiagodiascano

Could we set tab size greater than 5 in code block? Like 8?

image

rhiswell avatar Apr 19 '20 14:04 rhiswell

Yes, but only for code fences and source code mode

But I still got tabs rather than spaces after "Tab" pressed.

scruel avatar Aug 30 '22 10:08 scruel

put into #579

abnerlee avatar Sep 06 '22 15:09 abnerlee

我找到一个解决办法:利用 expand 命令,支持 Linux 和 macOS 在 Git 提交变动的时候,自动将文本的 tab => 4个空格。

chmod +x .git/hooks/pre-commit
#!/bin/sh

# 01、得到项目根目录,Get project root directory
PROJECT_HOME=$(
  cd "$(dirname "$0")" || exit
  cd ../../
  pwd
)
# echo "${PROJECT_HOME}" >>~/Desktop/git_hook.log

# 02、获取修改的文件的子路径,Get the sub path of the modified file
STAGE_FILES=$(git diff HEAD --name-only --diff-filter=ACM -- '*.md' '*.py')   # 限制后缀文件类型 
# echo "${STAGE_FILES}" >>~/Desktop/git_hook.log

TMPFILE=$(mktemp)                                           # 随机临时文件,Random temporary file
# echo "TMPFILE${TMPFILE}" >>~/Desktop/git_hook.log

if test ${#STAGE_FILES} -gt 0; then
  # echo '开始替换 Tab => 4个空格' >>~/Desktop/git_hook.log

  OLDIFS="$IFS"                                             # 备份旧的 IFS 变量
  IFS=$'\n'                                                 # 修改分隔符为换行符

  for FILE in $STAGE_FILES; do
    FULE_PATH="${PROJECT_HOME}/${FILE}"
    # echo "expanding ${FULE_PATH}" >>~/Desktop/git_hook.log
    
    expand -t 4 "${FULE_PATH}" >"${TMPFILE}"                # 利用系统 expand 将 Tab 转成 4个空格,并保存进临时文件
    cp "${TMPFILE}" "${FULE_PATH}"                          # 利用临时文件覆盖,Tip:相同内容不会造成 diff 时,整个文件都变动的,放心用!
    git add "${FULE_PATH}"

    # $? 表示上一个任务的执行结果,正常执行返回 0
    if [ $? == 0 ]; then                                    # $?:Indicates the execution result of the last command
      # echo '替换完毕 ...' >>~/Desktop/git_hook.log
    fi
  done
  IFS="$OLDIFS"                                             # 还原 IFS 变量
fi

exit 0                                                      # Return 0,Indicates normal

lhsaq2009 avatar Sep 22 '22 15:09 lhsaq2009

@lhsaq2009 windows 震怒

scruel avatar Sep 22 '22 17:09 scruel